json_patch

Function json_patch 

Source
pub fn json_patch<T: JsonOrNullableJsonOrJsonbOrNullableJsonb + SingleValue, P: JsonOrNullableJsonOrJsonbOrNullableJsonb + SingleValue + CombinedNullableValue<T, Json>, target, patch>(
    target: target,
    patch: patch,
) -> json_patch<T, P, target, patch>
where target: AsExpression<T>, patch: AsExpression<P>,
Available on crate feature sqlite only.
Expand description

Applies an RFC 7396 MergePatch patch to the input JSON target and returns the patched JSON value.

MergePatch can add, modify, or delete elements of a JSON object. Arrays are treated as atomic values: they can only be inserted, replaced, or deleted as a whole, not modified element-wise.

ยงExamples


let result = diesel::select(json_patch::<Json, Json, _, _>(
    json!( {"a":1,"b":2} ),
    json!( {"c":3,"d":4} ),
))
.get_result::<Value>(connection)?;
assert_eq!(json!({"a":1,"b":2,"c":3,"d":4}), result);

let result = diesel::select(json_patch::<Json, Json, _, _>(
    json!( {"a":[1,2],"b":2} ),
    json!( {"a":9} ),
))
.get_result::<Value>(connection)?;
assert_eq!(json!({"a":9,"b":2}), result);

let result = diesel::select(json_patch::<Json, Json, _, _>(
    json!( {"a":[1,2],"b":2} ),
    json!( {"a":null} ),
))
.get_result::<Value>(connection)?;
assert_eq!(json!({"b":2}), result);

let result = diesel::select(json_patch::<Json, Json, _, _>(
    json!( {"a":1,"b":2} ),
    json!( {"a":9,"b":null,"c":8} ),
))
.get_result::<Value>(connection)?;
assert_eq!(json!({"a":9,"c":8}), result);

let result = diesel::select(json_patch::<Json, Json, _, _>(
    json!( {"a":{"x":1,"y":2},"b":3} ),
    json!( {"a":{"y":9},"c":8} ),
))
.get_result::<Value>(connection)?;
assert_eq!(
    json!({"a":{"x":1,"y":9},"b":3,"c":8}),
    result
);

// Nullable input yields nullable output
let result = diesel::select(json_patch::<Nullable<Json>, Json, _, _>(
    None::<Value>,
    json!({}),
))
.get_result::<Option<Value>>(connection)?;
assert!(result.is_none());