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_str_as<'a, T>(&'a self, key: &str) -> Result<T, JsonExtractError>
pub fn get_str_as<'a, T>(&'a self, key: &str) -> Result<T, JsonExtractError>
Extracts member key as a string-backed primitive T, validating it
through T’s TryFrom<&str> constructor.
Returns JsonExtractErrorKind::Missing if the key is absent,
JsonExtractErrorKind::WrongType if the member is not a JSON string,
and JsonExtractErrorKind::Invalid (wrapping the PrimitiveError) if
the string fails validation. The error carries the $.key path.
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?
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
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
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
Source§impl PartialEq for JsonObject
impl PartialEq for JsonObject
Source§fn eq(&self, other: &JsonObject) -> bool
fn eq(&self, other: &JsonObject) -> bool
self and other values to be equal, and is used by ==.