json_object_2

Function json_object_2 

Source
pub fn json_object_2<K1: NotBlob<IsNull = NotNull>, V1: NotBlob, K2: NotBlob<IsNull = NotNull>, V2: NotBlob, key_1, value_1, key_2, value_2>(
    key_1: key_1,
    value_1: value_1,
    key_2: key_2,
    value_2: value_2,
) -> json_object_2<K1, V1, K2, V2, key_1, value_1, key_2, value_2>
where key_1: AsExpression<K1>, value_1: AsExpression<V1>, key_2: AsExpression<K2>, value_2: AsExpression<V2>,
Available on crate feature sqlite only.
Expand description

The json_object() SQL function accepts zero or more pairs of arguments and returns a well-formed JSON object composed from those pairs. The first argument of each pair is the label (key) and the second argument is the value. If any argument to json_object() is a BLOB then an error is thrown.

An argument with SQL type TEXT is normally converted into a quoted JSON string even if the input text is well-formed JSON. However, if the argument is the direct result from another JSON function, then it is treated as JSON and all of its JSON type information and substructure is preserved. This allows calls to json_object() and json_array() to be nested. The json() function can also be used to force strings to be recognized as JSON.

This function requires at least SQLite 3.38 or newer

§Examples

let result = diesel::select(json_object_0()).get_result::<serde_json::Value>(connection)?;
assert_eq!(json!({}), result);

let result = diesel::select(json_object_1::<Text, Integer, _, _>("a", 2))
    .get_result::<serde_json::Value>(connection)?;
assert_eq!(json!({"a": 2}), result);

let result = diesel::select(
    json_object_2::<Text, Integer, Text, Text, _, _, _, _>("a", 2, "c", "{e:5}")
)
.get_result::<serde_json::Value>(connection)?;
assert_eq!(json!({"a": 2, "c": "{e:5}"}), result);

let result = diesel::select(
    json_object_2::<Text, Integer, Text, Json, _, _, _, _>("a", 2, "c", json_object_1::<Text, Integer, _, _>("e", 5))
)
.get_result::<serde_json::Value>(connection)?;
assert_eq!(json!({"a": 2, "c": {"e": 5}}), result);

§Variadic functions

This function is variadic in SQL, so there’s a family of functions on a diesel side:

json_object_0, json_object_1, … json_object_n

Here, the postfix number indicates repetitions of variadic arguments. To use this function, the appropriate version with the correct argument count must be selected.

§Controlling the generation of variadic function variants

By default, only variants with 0, 1, and 2 repetitions of variadic arguments are generated. To generate more variants, set the DIESEL_VARIADIC_FUNCTION_ARGS environment variable to the desired number of variants.

For a greater convenience this environment variable can also be set in a .cargo/config.toml file as described in the cargo documentation.