Open
Description
I have a code like this
fn main() {
let obj = b"{\"key\":\"Small JSON-file with object.\"}";
let mut json_obj_repr: serde_json::Value = serde_json::from_slice(obj).unwrap();
let missing_key = json_obj_repr["Hello World!"].take();
println!("From object I took {:?}", missing_key);
let string = b"\"There is just a string in valid JSON-file.\"";
let mut json_str_repr: serde_json::Value = serde_json::from_slice(string).unwrap();
let invalid_operation = json_str_repr["Hello World!"].take();
println!("Never reach this {:?}", invalid_operation);
}
First println!
Shows that taking from missing key returns Value::Null variant. And this is what we all expect from Value::take(&mut self)
.
Second take()
panics
thread 'main' panicked at /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/serde_json-1.0.135/src/value/index.rs:102:18:
cannot access key "Hello World!" in JSON string
And second println!
is never reached.
My toolchain is stable-x86_64-unknown-linux-gnu - rustc 1.84.0 (9fc6b4312 2025-01-07)
.
Metadata
Metadata
Assignees
Labels
No labels