json-rust icon indicating copy to clipboard operation
json-rust copied to clipboard

Non-existent keys should not return null

Open An-dz opened this issue 4 years ago • 0 comments

One thing that caught my attention right after reading the documentation is that trying to access a key that does not exist returns null. And thus this code works without panicking:

fn main() {
    let null_test = json::object! {
        bar: null,
    };

    assert!(null_test["foo"] == null_test["bar"]);
}

There's a distinction between something that is undefined and a key with a null value but this crate does not have it.

This causes implications like introducing a bug where the wrong key name is used but the code won't complain because it will constantly accept that the value was null, even though the function may fail and thus not report. As an example think that if bar exists I know that part worked and returned nothing, while the service foo failed completely and thus was not appended.

An-dz avatar Aug 12 '20 20:08 An-dz