Function pact_consumer::util::strip_null_fields[][src]

pub fn strip_null_fields<V>(json: V) -> Value where
    V: Into<Value>, 
Expand description

Given a serde_json::Value, walk it recursively, removing any null fields, and return the updated value. Because of how this is normally called, it consumes its input value and returns the stripped value.

This function is most useful when serializing Rust types to JSON for use with each_like!, because it follows the pact convention of removing optional fields.

use pact_consumer::prelude::*;

let actual = strip_null_fields(serde_json::json!([
    null,
    { "a": 1, "b": null },
]));
let expected = serde_json::json!([
    null,       // nulls in arrays are left alone.
    { "a": 1 }, // nulls in objects are stripped.
]);
assert_eq!(actual, expected);