pub struct JsonObject { /* private fields */ }Expand description
A JSON object: members in insertion order with unique keys.
Keys are guaranteed unique — the parser rejects duplicates, and
insert replaces an existing key in place rather than
adding a second entry. Lookup is linear; object size is bounded by
JsonLimits when parsing untrusted input.
Implementations§
Source§impl JsonObject
impl JsonObject
Sourcepub fn get(&self, key: &str) -> Option<&JsonValue>
pub fn get(&self, key: &str) -> Option<&JsonValue>
Returns the value for key, if present.
Examples found in repository?
examples/basic.rs (line 23)
9fn main() {
10 // Treat this as untrusted input: parse under a conservative limit profile.
11 let input = r#"{ "service": "api", "port": 8080, "tags": ["a", "b"], "enabled": true }"#;
12 let limits = JsonLimits::conservative();
13
14 let value = match parse_with_limits(input.as_bytes(), limits) {
15 Ok(value) => value,
16 Err(err) => {
17 eprintln!("rejected: {err}");
18 return;
19 }
20 };
21
22 if let JsonValue::Object(object) = &value {
23 if let Some(port) = object.get("port").and_then(JsonValue::as_number) {
24 println!("port = {}", port.to_u64().unwrap());
25 }
26 if let Some(tags) = object.get("tags").and_then(JsonValue::as_array) {
27 println!("tags = {}", tags.len());
28 }
29 }
30
31 // Deterministic, member-order-preserving serialization.
32 println!("compact = {}", to_compact_string(&value));
33
34 // Strict by default: these are all rejected.
35 for bad in [r#"{"a":1,"a":2}"#, "{ /* c */ }", "[1,]", "NaN"] {
36 match parse_with_limits(bad.as_bytes(), limits) {
37 Ok(_) => println!("accepted (unexpected): {bad}"),
38 Err(err) => println!("rejected {bad:?}: {}", err.kind_str()),
39 }
40 }
41}Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Returns true if key is present.
Sourcepub fn insert(&mut self, key: String, value: JsonValue) -> Option<JsonValue>
pub fn insert(&mut self, key: String, value: JsonValue) -> Option<JsonValue>
Inserts or replaces key. If the key already exists its value is
replaced in place (preserving position) and the old value is returned;
otherwise the member is appended.
Sourcepub fn iter(&self) -> Iter<'_, JsonMember>
pub fn iter(&self) -> Iter<'_, JsonMember>
Iterates over members in insertion order.
Trait Implementations§
Source§impl Clone for JsonObject
impl Clone for JsonObject
Source§fn clone(&self) -> JsonObject
fn clone(&self) -> JsonObject
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for JsonObject
impl Debug for JsonObject
Source§impl Default for JsonObject
impl Default for JsonObject
Source§fn default() -> JsonObject
fn default() -> JsonObject
Returns the “default value” for a type. Read more
Source§impl PartialEq for JsonObject
impl PartialEq for JsonObject
Source§fn eq(&self, other: &JsonObject) -> bool
fn eq(&self, other: &JsonObject) -> bool
Tests for
self and other values to be equal, and is used by ==.impl StructuralPartialEq for JsonObject
Auto Trait Implementations§
impl Freeze for JsonObject
impl RefUnwindSafe for JsonObject
impl Send for JsonObject
impl Sync for JsonObject
impl Unpin for JsonObject
impl UnsafeUnpin for JsonObject
impl UnwindSafe for JsonObject
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more