Miles to go ...

Operations on JSON

This write up aims to define common arithmetic, bit-wise, logical and compare operations that can be applied on JSON values. Note that JSON values can be of mixed types, in other words, operands in a binary operation not necessarily belong to same JSON type - so as to implement computational operations for schema-less documents.

A reference implementation of this proposal is provided by the Rust library jsondata.

List of operations that we are going to study here:

Arithmetic operation

lhs rhs result description
Null (any) (any) identity operation
(any) Null (any) identity operation
Integer Integer Integer integral addition
Float Float Float floating point addition
Integer Float Float integer shall be converted to float
Float Integer Float integer shall be converted to float
String String String string concatenation
"hello" + "world" = "helloworld"
Array Array Array [1,2] + [2,1] = [1,2,2,1]
Object Object Object {"a": 10, "b": 11} + {"b": 20} = {"a":10, "b":20}
lhs rhs result description
Null (any) (any) identity operation
(any) Null (any) identity operation
Integer Integer Integer integral subtraction
Float Float Float floating point subtraction
Integer Float Float integer shall be converted to float
Float Integer Float integer shall be converted to float
Array Array Array [1,1,2,2,2] - [2,2,1] = [1,2]
Object Object Object {"a":10, "b":20} - {"b":20} = {"a":10}
lhs rhs result description
Null (any) Null anything multiplied by Null is Null
(any) Null Null anything multiplied by Null is Null
Integer Integer Integer integral multiplication
Float Float Float floating point multiplication
Integer Float Float integer shall be converted to float
Float Integer Float integer shall be converted to float
String Integer String "ok" * 3 = "okokok" and "ok" * 0 = ""
Integer String String 3 * "ok" = "okokok"
lhs rhs result description
Null (any) Null anything divided by Null is Null
(any) Null Null anything divided by Null is Null
Integer Integer Integer integral division
Float Float Float floating point division
Integer Float Float integer shall be converted to float
Float Integer Float integer shall be converted to float
lhs rhs result description
Null (any) Null anything reminder of Null is Null
(any) Null Null anything reminder of Null is Null
Integer Integer Integer integral reminder
Float Float Float floating point reminder
Integer Float Float integer shall be converted to float
Float Integer Float integer shall be converted to float
unary operand result description
Null Null negation of Null is Null
Integer Integer integral negation
Float Float floating point negation

Bitwise operations

lhs rhs result description
Integer Integer Integer Bit-wise left shift, silent overflow. For all rhs, where
(2^(rhs+1)) >= max(int)
result shall be ZERO
lhs rhs result description
Integer Integer Integer Bit-wise right shift, sign bit moves right. For all rhs, where
(2^(rhs+1)) >= max(int)
result shall be ZERO, for positive integer
result shall be -1, for negative integer
lhs rhs result description
Integer Integer Integer Bit-wise AND operation between two signed integers
lhs rhs result description
Integer Integer Integer Bit-wise OR operation between two signed integers
lhs rhs result description
Integer Integer Integer Bit-wise XOR operation between two signed integers

Logical operations

For the purpose of logical operations, following JSON types/values shall be coerced to true or false.

With types and values reduced to either true or false, results of a logical operator simply follows the Boolean truth table.

lhs rhs result
true true true
true false false
false true false
false false false
lhs rhs result
true true true
true false true
false true true
false false false
unary operand result
true false
false true

Compare operation

Gist of rules for compare operation:

A more detailed description can be found here: sort order for json

Index operation

Index operation is applicable only on array and object types.

Range operation

Range operation is applicable only on array type, uses similar syntax as index operation but selects a range of values within the array. For example array arr = [1, 2, true, null, 3.4, [1,2]],

Package jsondata

In jsondata the Assign variant of Arithmetic and Bitwise operations are implemented as well. And specifically it does not implement the following operations that are defined by std::ops: