### Advanced JSON Reading Source: https://github.com/marcono1234/struson/blob/main/README.md Example of reading JSON using the lower-level JsonStreamReader. ```rust use struson::reader::* // In this example JSON data comes from a string; // normally it would come from a file or a network connection let json = r#"{"a": [1, true]}"#; let mut json_reader = JsonStreamReader::new(json.as_bytes()); json_reader.begin_object()?; assert_eq!(json_reader.next_name()?, "a"); json_reader.begin_array()?; assert_eq!(json_reader.next_number::()??, 1); assert_eq!(json_reader.next_bool()?, true); json_reader.end_array()?; json_reader.end_object()?; // Ensures that there is no trailing data json_reader.consume_trailing_whitespace()?; ``` -------------------------------- ### Advanced JSON Writing Source: https://github.com/marcono1234/struson/blob/main/README.md Example of writing JSON using the lower-level JsonStreamWriter. ```rust use struson::writer::* // In this example JSON bytes are stored in a Vec; // normally they would be written to a file or network connection let mut writer = Vec::::new(); let mut json_writer = JsonStreamWriter::new(&mut writer); json_writer.begin_object()?; json_writer.name("a")?; json_writer.begin_array()?; json_writer.number_value(1)?; json_writer.bool_value(true)?; json_writer.end_array()?; json_writer.end_object()?; // Ensures that the JSON document is complete and flushes the buffer json_writer.finish_document()?; let json = String::from_utf8(writer)?; assert_eq!(json, r#"{"a":[1,true]}"#); ``` -------------------------------- ### Writing JSON Object Source: https://github.com/marcono1234/struson/blob/main/README.md Example of writing a simple JSON object using SimpleJsonWriter. ```rust use struson::writer::simple::* // In this example JSON bytes are stored in a Vec; // normally they would be written to a file or network connection let mut writer = Vec::::new(); let json_writer = SimpleJsonWriter::new(&mut writer); json_writer.write_object(|object_writer| { object_writer.write_number_member("a", 1)?; object_writer.write_bool_member("b", true)?; Ok(()) })?; let json = String::from_utf8(writer)?; assert_eq!(json, r#"{"a":1,"b":true}"#); ``` -------------------------------- ### Reading JSON Array Source: https://github.com/marcono1234/struson/blob/main/README.md Example of reading an array of strings from JSON using SimpleJsonReader. ```rust use struson::reader::simple::* // In this example JSON data comes from a string; // normally it would come from a file or a network connection let json_reader = SimpleJsonReader::new(r#"["a", "short", "example"]"#.as_bytes()); let mut words = Vec::::new(); json_reader.read_array_items(|item_reader| { let word = item_reader.read_string()?; words.push(word); Ok(()) })?; assert_eq!(words, vec!["a", "short", "example"]); ``` -------------------------------- ### Reading Nested JSON Values Source: https://github.com/marcono1234/struson/blob/main/README.md Example of reading nested values using read_seeked_multi with a JSON path. ```rust use struson::reader::simple::* use struson::reader::simple::multi_json_path::multi_json_path // In this example JSON data comes from a string; // normally it would come from a file or a network connection let json = r#"{ "users": [ {"name": "John", "age": 32}, {"name": "Jane", "age": 41} ] }"#; let json_reader = SimpleJsonReader::new(json.as_bytes()); let mut ages = Vec::::new(); // Select the ages of all users let json_path = multi_json_path!["users", [*], "age"]; json_reader.read_seeked_multi(&json_path, false, |value_reader| { let age = value_reader.read_number()??; ages.push(age); Ok(()) })?; assert_eq!(ages, vec![32, 41]); ``` -------------------------------- ### Building with cargo-make Source: https://github.com/marcono1234/struson/blob/main/README.md Command to build the project using cargo-make. ```sh cargo make ``` -------------------------------- ### Enable Simple API Source: https://github.com/marcono1234/struson/blob/main/README.md To use the experimental simple API of Struson, you need to enable the `simple-api` feature in your `Cargo.toml` file. ```toml [dependencies] struson = { version = "...", features = ["simple-api"] } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.