deephaven.json

The deephaven JSON module presents a declarative and composable configuration layer for describing the structure of a JSON (https://www.json.org) value. Most commonly, this will be used to model the structure for a JSON object. For example, the JSON object

{ "name": "Foo", "age": 42, "location": { "lat": 45.018269, "lon": -93.473892 } }

can be modelled with the dictionary

{ "name": str, "age": int, "location": { "lat": float, "lon": float } }

Notice that this allows for the nested modelling of JSON values. Other common constructions involve the modelling of JSON arrays. For example, a variable-length JSON array where the elements are the same type

[42, 31, ..., 12345]

can be modelled with a single-element list containing the element type

[ int ]

If the JSON array is a fixed size and each elements’ type is known, for example

["Foo", 42, [45.018269, -93.473892]]

can be modelled with a tuple containing each type

(str, int, (float, float))

Notice again that this allows for the nested modelling of JSON values. Of course, these constructions can be all be used together. For example, the JSON object

{
  "name": "Foo",
  "locations": [
    [45.018269, -93.473892],
    ...,
    [40.730610, -73.935242]
  ]
}

can be modelled as

{"name": str, "locations": [(float, float)]}

See the methods in this module more more details on modelling JSON values.

class JsonValue(j_value)[source]

Bases: JObjectWrapper

The JSON Value type.

j_object_type

alias of Value

JsonValueType

The JSON value alias

alias of Union[JsonValue, DType, type, Dict[str, Union[JsonValueType, ObjectField]], List[JsonValueType], Tuple[JsonValueType, …]]

class ObjectField(value_type, aliases=<factory>, repeated_behavior=RepeatedFieldBehavior.ERROR, case_sensitive=True)[source]

Bases: object

The object field options.

In contexts where the user needs to create an object field value and isn’t changing any default values, the user can simplify by just using the JsonValueType. For example,

{
    "name": ObjectField(str),
    "age": ObjectField(int),
}

could be simplified to

{
    "name": str,
    "age": int,
}
aliases

The field name aliases. By default, is an empty list.

case_sensitive = True

If the field name and aliases should be compared using case-sensitive equality. By default, is True.

repeated_behavior = io.deephaven.json.ObjectField$RepeatedBehavior(objectRef=0x5565f354b60a)

The repeated field behavior. By default, is RepeatedFieldBehavior.ERROR.

value_type

The json value type

class RepeatedFieldBehavior(value)[source]

Bases: Enum

The behavior to use when a repeated field is encountered in a JSON object. For example,

{
  "foo": 42,
  "foo": 43
}
ERROR = io.deephaven.json.ObjectField$RepeatedBehavior(objectRef=0x5565f354b60a)

Raise an error

USE_FIRST = io.deephaven.json.ObjectField$RepeatedBehavior(objectRef=0x5565f354b612)

Use the first field

any_val()[source]

Creates an “any” value. The resulting type is implementation dependant.

Return type:

JsonValue

Returns:

the “any” value

array_val(element, allow_missing=True, allow_null=True)[source]

Creates a “typed array”, where all elements of the array have the same element type. For example, the JSON array

[1, 42, 43, 13]

might be modelled as an array of ints

array_val(int)

In contexts where the user needs to create a JsonValueType and isn’t changing any default values, the user can simplify by using a list with a single element type. For example,

some_method(array_val(element))

could be simplified to

some_method([element])
Parameters:
  • element (JsonValueType) – the array element type

  • allow_missing (bool) – if the array is allowed to be missing, by default is True

  • allow_null (bool) – if the array is allowed to be a JSON null type, by default is True

Return type:

JsonValue

Returns:

the array value

big_decimal_val(allow_string=False, allow_missing=True, allow_null=True, on_missing=None, on_null=None)[source]

Creates a BigDecimal value. For example, the JSON decimal

123456789012345678901.42

might be modelled as the BigDecimal type

big_decimal_val()
Parameters:
  • allow_string (bool) – if the BigDecimal value is allowed to be a JSON string type, default is False.

  • allow_missing (bool) – if the BigDecimal value is allowed to be missing, default is True

  • allow_null (bool) – if the BigDecimal value is allowed to be a JSON null type, default is True

  • on_missing (Optional[Union[float, str]]) – the value to use when the JSON value is missing and allow_missing is True, default is None.

  • on_null (Optional[Union[float, str]]) – the value to use when the JSON value is null and allow_null is True, default is None.

Return type:

JsonValue

Returns:

the BigDecimal value

big_integer_val(allow_string=False, allow_decimal=False, allow_missing=True, allow_null=True, on_missing=None, on_null=None)[source]

Creates a BigInteger value. For example, the JSON integer

123456789012345678901

might be modelled as the BigInteger type

big_integer_val()
Parameters:
  • allow_string (bool) – if the BigInteger value is allowed to be a JSON string type, default is False.

  • allow_decimal (bool) – if the BigInteger value is allowed to be a JSON decimal type, default is False.

  • allow_missing (bool) – if the BigInteger value is allowed to be missing, default is True

  • allow_null (bool) – if the BigInteger value is allowed to be a JSON null type, default is True

  • on_missing (Optional[Union[int, str]]) – the value to use when the JSON value is missing and allow_missing is True, default is None.

  • on_null (Optional[Union[int, str]]) – the value to use when the JSON value is null and allow_null is True, default is None.

Return type:

JsonValue

Returns:

the BigInteger value

bool_val(allow_string=False, allow_missing=True, allow_null=True, on_missing=None, on_null=None)[source]

Creates a bool value. For example, the JSON boolean

True

might be modelled as the bool type

bool_val()

In contexts where the user needs to create a JsonValueType and isn’t changing any default values, the user can simplify by using the python built-in bool type. For example,

some_method(bool_val())

could be simplified to

some_method(bool)
Parameters:
  • allow_string (bool) – if the bool value is allowed to be a JSON string type, default is False

  • allow_missing (bool) – if the bool value is allowed to be missing, default is True

  • allow_null (bool) – if the bool value is allowed to be a JSON null type, default is True

  • on_missing (Optional[bool]) – the value to use when the JSON value is missing and allow_missing is True, default is None

  • on_null (Optional[bool]) – the value to use when the JSON value is null and allow_null is True, default is None

Return type:

JsonValue

Returns:

the bool value

byte_val(allow_decimal=False, allow_string=False, allow_missing=True, allow_null=True, on_missing=None, on_null=None)[source]

Creates a byte (signed 8-bit) value. For example, the JSON integer

42

might be modelled as the byte type

byte_val()
Parameters:
  • allow_decimal (bool) – if the byte value is allowed to be a JSON decimal type, default is False

  • allow_string (bool) – if the byte value is allowed to be a JSON string type, default is False

  • allow_missing (bool) – if the byte value is allowed to be missing, default is True

  • allow_null (bool) – if the byte value is allowed to be a JSON null type, default is True

  • on_missing (Optional[int]) – the value to use when the JSON value is missing and allow_missing is True, default is None.

  • on_null (Optional[int]) – the value to use when the JSON value is null and allow_null is True, default is None.

Return type:

JsonValue

Returns:

the byte value

char_val(allow_missing=True, allow_null=True, on_missing=None, on_null=None)[source]

Creates a char value. For example, the JSON string

"F"

might be modelled as the char type

char_val()
Parameters:
  • allow_missing (bool) – if the char value is allowed to be missing, default is True

  • allow_null (bool) – if the char value is allowed to be a JSON null type, default is True

  • on_missing (Optional[str]) – the value to use when the JSON value is missing and allow_missing is True, default is None. If specified, must be a single character.

  • on_null (Optional[str]) – the value to use when the JSON value is null and allow_null is True, default is None. If specified, must be a single character.

Return type:

JsonValue

Returns:

the char value

double_val(allow_string=False, allow_missing=True, allow_null=True, on_missing=None, on_null=None)[source]

Creates a double (signed 64-bit) value. For example, the JSON decimal

42.42424242

might be modelled as the double type

double_val()

In contexts where the user needs to create a JsonValueType and isn’t changing any default values, the user can simplify by using the python built-in float type. For example,

some_method(double_val())

could be simplified to

some_method(float)
Parameters:
  • allow_string (bool) – if the double value is allowed to be a JSON string type, default is False

  • allow_missing (bool) – if the double value is allowed to be missing, default is True

  • allow_null (bool) – if the double value is allowed to be a JSON null type, default is True

  • on_missing (Optional[int]) – the value to use when the JSON value is missing and allow_missing is True, default is None.

  • on_null (Optional[int]) – the value to use when the JSON value is null and allow_null is True, default is None.

Return type:

JsonValue

Returns:

the double value

float_val(allow_string=False, allow_missing=True, allow_null=True, on_missing=None, on_null=None)[source]

Creates a float (signed 32-bit) value. For example, the JSON decimal

42.42

might be modelled as the float type

float_val()
Parameters:
  • allow_string (bool) – if the float value is allowed to be a JSON string type, default is False

  • allow_missing (bool) – if the float value is allowed to be missing, default is True

  • allow_null (bool) – if the float value is allowed to be a JSON null type, default is True

  • on_missing (Optional[float]) – the value to use when the JSON value is missing and allow_missing is True, default is None.

  • on_null (Optional[float]) – the value to use when the JSON value is null and allow_null is True, default is None.

Return type:

JsonValue

Returns:

the float value

instant_val(allow_missing=True, allow_null=True, number_format=None, allow_decimal=False, on_missing=None, on_null=None)[source]

Creates an Instant value. For example, the JSON string

"2009-02-13T23:31:30.123456789Z"

might be modelled as the Instant type

instant_val()

In another example, the JSON decimal

1234567890.123456789

might be modelled as the Instant type

instant_val(number_format="s", allow_decimal=True)

In contexts where the user needs to create a JsonValueType and isn’t changing any default values, the user can simplify by using the python datetime.datetime type. For example,

some_method(instant_val())

could be simplified to

some_method(datetime.datetime)
Parameters:
  • allow_missing (bool) – if the Instant value is allowed to be missing, default is True

  • allow_null (bool) – if the Instant value is allowed to be a JSON null type, default is True

  • number_format (Literal[None, "s", "ms", "us", "ns"]) – when set, signifies that a JSON numeric type is expected. “s” is for seconds, “ms” is for milliseconds, “us” is for microseconds, and “ns” is for nanoseconds since the epoch. When not set, a JSON string in the ISO-8601 format is expected.

  • allow_decimal (bool) – if the Instant value is allowed to be a JSON decimal type, default is False. Only valid when number_format is specified.

  • on_missing (Optional[InstantLike]) – the value to use when the JSON value is missing and allow_missing is True, default is None.

  • on_null (Optional[InstantLike]) – the value to use when the JSON value is null and allow_null is True, default is None.

Return type:

JsonValue

Returns:

the Instant value

int_val(allow_decimal=False, allow_string=False, allow_missing=True, allow_null=True, on_missing=None, on_null=None)[source]

Creates an int (signed 32-bit) value. For example, the JSON integer

100000

might be modelled as the int type

int_val()
Parameters:
  • allow_decimal (bool) – if the int value is allowed to be a JSON decimal type, default is False

  • allow_string (bool) – if the int value is allowed to be a JSON string type, default is False

  • allow_missing (bool) – if the int value is allowed to be missing, default is True

  • allow_null (bool) – if the int value is allowed to be a JSON null type, default is True

  • on_missing (Optional[int]) – the value to use when the JSON value is missing and allow_missing is True, default is None.

  • on_null (Optional[int]) – the value to use when the JSON value is null and allow_null is True, default is None.

Return type:

JsonValue

Returns:

the int value

json_val(json_value_type)[source]

Creates a JsonValue from a JsonValueType.

  • JsonValue is returned unchanged

  • bool returns bool_val()

  • int returns long_val()

  • float returns double_val()

  • str returns string_val()

  • datetime.datetime returns instant_val()

  • object returns any_val()

  • Dictionaries returns object_val(json_value_type)

  • Lists of length 1 returns array_val(json_value_type[0]) (Lists of other sizes are not supported)

  • Tuples returns tuple_val(json_value_type)

Parameters:

json_value_type (JsonValueType) – the JSON value type

Return type:

JsonValue

Returns:

the JSON value

long_val(allow_decimal=False, allow_string=False, allow_missing=True, allow_null=True, on_missing=None, on_null=None)[source]

Creates a long (signed 64-bit) value. For example, the JSON integer

8000000000

might be modelled as the long type

long_val()

In contexts where the user needs to create a JsonValueType and isn’t changing any default values, the user can simplify by using the python built-in long type. For example,

some_method(long_val())

could be simplified to

some_method(int)
Parameters:
  • allow_decimal (bool) – if the long value is allowed to be a JSON decimal type, default is False

  • allow_string (bool) – if the long value is allowed to be a JSON string type, default is False

  • allow_missing (bool) – if the long value is allowed to be missing, default is True

  • allow_null (bool) – if the long value is allowed to be a JSON null type, default is True

  • on_missing (Optional[int]) – the value to use when the JSON value is missing and allow_missing is True, default is None.

  • on_null (Optional[int]) – the value to use when the JSON value is null and allow_null is True, default is None.

Return type:

JsonValue

Returns:

the long value

object_entries_val(value_type, key_type=<class 'str'>, allow_missing=True, allow_null=True)[source]

Creates an object entries value. This is used in situations where the number of fields in an object is variable and all the values types are the same. For example, the JSON object

{
    "foo": 1,
    "bar": 42,
    "baz": 3,
    ...
    "xyz": 100
}

might be modelled as the object kv type

object_entries_val(int)
Parameters:
  • value_type (JsonValueType) – the value type element, required

  • key_type (JsonValueType) – the key type element, by default is type str

  • allow_missing (bool) – if the object is allowed to be missing, by default is True

  • allow_null (bool) – if the object is allowed to be a JSON null type, by default is True

Return type:

JsonValue

Returns:

the object entries value

object_val(fields, allow_unknown_fields=True, allow_missing=True, allow_null=True, repeated_field_behavior=RepeatedFieldBehavior.ERROR, case_sensitive=True)[source]

Creates an object value. For example, the JSON object

{ "name": "foo", "age": 42 }

might be modelled as the object type

object_val({ "name": str, "age": int })

In contexts where the user needs to create a JsonValueType and isn’t changing any default values, the user can simplify by using a Dict[str, Union[JsonValueType, ObjectField]]. For example,

some_method(object_val({ "name": str, "age": int }))

could be simplified to

some_method({ "name": str, "age": int })
Parameters:
  • fields (Dict[str, Union[JsonValueType, ObjectField]]) – the fields

  • allow_unknown_fields (bool) – if unknown fields are allow, by default is True

  • allow_missing (bool) – if the object is allowed to be missing, by default is True

  • allow_null (bool) – if the object is allowed to be a JSON null type, by default is True

  • repeated_field_behavior (RepeatedFieldBehavior) – the default repeated field behavior, only used for fields that are specified using JsonValueType, by default is RepeatedFieldBehavior.ERROR

  • case_sensitive (bool) – if the field name and aliases should be compared using case-sensitive equality, only used for fields that are specified using JsonValueType, by default is True

Return type:

JsonValue

Returns:

the object value

short_val(allow_decimal=False, allow_string=False, allow_missing=True, allow_null=True, on_missing=None, on_null=None)[source]

Creates a short (signed 16-bit) value. For example, the JSON integer

30000

might be modelled as the short type

short_val()
Parameters:
  • allow_decimal (bool) – if the short value is allowed to be a JSON decimal type, default is False

  • allow_string (bool) – if the short value is allowed to be a JSON string type, default is False

  • allow_missing (bool) – if the short value is allowed to be missing, default is True

  • allow_null (bool) – if the short value is allowed to be a JSON null type, default is True

  • on_missing (Optional[int]) – the value to use when the JSON value is missing and allow_missing is True, default is None.

  • on_null (Optional[int]) – the value to use when the JSON value is null and allow_null is True, default is None.

Return type:

JsonValue

Returns:

the short value

skip_val(allow_missing=None, allow_null=None, allow_int=None, allow_decimal=None, allow_string=None, allow_bool=None, allow_object=None, allow_array=None, allow_by_default=True)[source]

Creates a “skip” value. No resulting type will be returned, but the JSON types will be validated as configured. This may be useful in combination with an object type where allow_unknown_fields=False. For example, the JSON object

{ "name": "foo", "age": 42 }

might be modelled as the object type

object_val({ "name": str, "age": skip_val() }, allow_unknown_fields=False)
Parameters:
  • allow_missing (Optional[bool]) – if a missing JSON value is allowed, by default is None

  • allow_null (Optional[bool]) – if a JSON null type is allowed, by default is None

  • allow_int (Optional[bool]) – if a JSON integer type is allowed, by default is None

  • allow_decimal (Optional[bool]) – if a JSON decimal type is allowed, by default is None

  • allow_string (Optional[bool]) – if a JSON string type is allowed, by default is None

  • allow_bool (Optional[bool]) – if a JSON boolean type is allowed, by default is None

  • allow_object (Optional[bool]) – if a JSON object type is allowed, by default is None

  • allow_array (Optional[bool]) – if a JSON array type is allowed, by default is None

  • allow_by_default (bool) – the default behavior for the other arguments when they are set to None, by default is True

Return type:

JsonValue

Returns:

the “skip” value

string_val(allow_int=False, allow_decimal=False, allow_bool=False, allow_missing=True, allow_null=True, on_missing=None, on_null=None)[source]

Creates a String value. For example, the JSON string

"Hello, world!"

might be modelled as the string type

string_val()

In contexts where the user needs to create a JsonValueType and isn’t changing any default values, the user can simplify by using the python built-in str type. For example,

some_method(string_val())

could be simplified to

some_method(str)
Parameters:
  • allow_int (bool) – if the string value is allowed to be a JSON integer type, default is False

  • allow_decimal (bool) – if the string value is allowed to be a JSON decimal type, default is False

  • allow_bool (bool) – if the string value is allowed to be a JSON boolean type, default is False

  • allow_missing (bool) – if the double value is allowed to be missing, default is True

  • allow_null (bool) – if the double value is allowed to be a JSON null type, default is True

  • on_missing (Optional[str]) – the value to use when the JSON value is missing and allow_missing is True, default is None.

  • on_null (Optional[str]) – the value to use when the JSON value is null and allow_null is True, default is None.

Return type:

JsonValue

Returns:

the String value

tuple_val(values, allow_missing=True, allow_null=True)[source]

Creates a tuple value. For example, the JSON array

["foo", 42, 5.72]

might be modelled as the tuple type

tuple_val((str, int, float))

To provide meaningful names, a dictionary can be used:

tuple_val({"name": str, "age": int, "height": float})

otherwise, default names based on the indexes of the values will be used.

In contexts where the user needs to create a JsonValueType and isn’t changing any default values nor is setting names, the user can simplify passing through a python tuple type. For example,

some_method(tuple_val((tuple_type_1, tuple_type_2)))

could be simplified to

some_method((tuple_type_1, tuple_type_2))
Parameters:
  • values (Union[Tuple[JsonValueType, ...], Dict[str, JsonValueType]]) – the tuple value types

  • allow_missing (bool) – if the array is allowed to be missing, by default is True

  • allow_null (bool) – if the array is allowed to be a JSON null type, by default is True

Return type:

JsonValue

Returns:

the tuple value

typed_object_val(type_field, shared_fields, objects, allow_unknown_types=True, allow_missing=True, allow_null=True, on_missing=None, on_null=None)[source]

Creates a type-discriminated object value. For example, the JSON objects

{ "type": "trade", "symbol": "FOO", "price": 70.03, "size": 42 }
{ "type": "quote", "symbol": "BAR", "bid": 10.01, "ask": 10.05 }

might be modelled as a type-discriminated object with “type” as the type field, “symbol” as a shared field, with a “trade” object containing a “bid” and an “ask” field, and with a “quote” object containing a “price” and a “size” field:

typed_object_val(
    "type",
    {"symbol": str},
    {
        "quote": {
            "price": float,
            "size": int
        },
        "trade": {
            "bid": float,
            "ask": float
        }
    }
)
Parameters:
  • type_field (str) – the type-discriminating field

  • shared_fields (Dict[str, Union[JsonValueType, ObjectField]]) – the shared fields

  • objects (Dict[str, Union[JsonValueType, ObjectField]]) – the individual objects, keyed by their type-discriminated value. The values must be object options.

  • allow_unknown_types (bool) – if unknown types are allow, by default is True

  • allow_missing (bool) – if the object is allowed to be missing, by default is True

  • allow_null (bool) – if the object is allowed to be a JSON null type, by default is True

  • on_missing (Optional[str]) – the type value to use when the JSON value is missing and allow_missing is True, default is None

  • on_null (Optional[str]) – the type value to use when the JSON value is null and allow_null is True, default is None

Return type:

JsonValue

Returns:

the typed object value