pub struct JsonLimits { /* private fields */ }Expand description
Resource limits enforced while parsing.
Limits bound logical decoded data (counts and byte lengths), not exact
allocator memory — real heap use also depends on String/Vec capacity and
the platform. Parsing untrusted input should always go through limits;
crate::parse applies JsonLimits::new by default.
Pick a profile with new, conservative,
or permissive, adjust individual limits with the
with_* builder methods, and read current values with the matching
accessors.
Implementations§
Source§impl JsonLimits
impl JsonLimits
Sourcepub const fn new() -> Self
pub const fn new() -> Self
The default limits: conservative values suitable for untrusted input.
1 MiB input, depth 64, 256 KiB per string, 16 KiB per key,
256 bytes per number, 100_000 array items / object members,
200_000 total nodes, 1 MiB total decoded string bytes.
Sourcepub const fn conservative() -> Self
pub const fn conservative() -> Self
A tighter profile for small, low-trust payloads (e.g. tokens, webhooks).
64 KiB input, depth 32, 16 KiB per string, 1 KiB per key,
64 bytes per number, 4_096 array items / object members,
16_384 total nodes, 64 KiB total decoded string bytes.
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 const fn permissive() -> Self
pub const fn permissive() -> Self
A looser profile for larger trusted documents. Still explicit and finite.
64 MiB input, depth 128, 16 MiB per string, 256 KiB per key,
1_024 bytes per number, 5_000_000 array items / object members,
10_000_000 total nodes, 64 MiB total decoded string bytes.
Sourcepub const fn max_input_bytes(&self) -> usize
pub const fn max_input_bytes(&self) -> usize
Maximum size of the whole input, in bytes.
Sourcepub const fn max_string_bytes(&self) -> usize
pub const fn max_string_bytes(&self) -> usize
Maximum decoded size of a single string value, in bytes.
Sourcepub const fn max_key_bytes(&self) -> usize
pub const fn max_key_bytes(&self) -> usize
Maximum decoded size of a single object key, in bytes.
Sourcepub const fn max_number_bytes(&self) -> usize
pub const fn max_number_bytes(&self) -> usize
Maximum length of a single number token, in bytes.
Sourcepub const fn max_array_items(&self) -> usize
pub const fn max_array_items(&self) -> usize
Maximum number of items in a single array.
Sourcepub const fn max_object_members(&self) -> usize
pub const fn max_object_members(&self) -> usize
Maximum number of members in a single object.
Sourcepub const fn max_total_nodes(&self) -> usize
pub const fn max_total_nodes(&self) -> usize
Maximum total number of values (nodes) in the document.
Sourcepub const fn max_total_decoded_string_bytes(&self) -> usize
pub const fn max_total_decoded_string_bytes(&self) -> usize
Maximum total decoded string bytes across the whole document.
Sourcepub const fn with_max_input_bytes(self, value: usize) -> Self
pub const fn with_max_input_bytes(self, value: usize) -> Self
Sets max_input_bytes.
Sourcepub const fn with_max_depth(self, value: usize) -> Self
pub const fn with_max_depth(self, value: usize) -> Self
Sets max_depth.
Sourcepub const fn with_max_string_bytes(self, value: usize) -> Self
pub const fn with_max_string_bytes(self, value: usize) -> Self
Sets max_string_bytes.
Sourcepub const fn with_max_key_bytes(self, value: usize) -> Self
pub const fn with_max_key_bytes(self, value: usize) -> Self
Sets max_key_bytes.
Sourcepub const fn with_max_number_bytes(self, value: usize) -> Self
pub const fn with_max_number_bytes(self, value: usize) -> Self
Sets max_number_bytes.
Sourcepub const fn with_max_array_items(self, value: usize) -> Self
pub const fn with_max_array_items(self, value: usize) -> Self
Sets max_array_items.
Sourcepub const fn with_max_object_members(self, value: usize) -> Self
pub const fn with_max_object_members(self, value: usize) -> Self
Sets max_object_members.
Sourcepub const fn with_max_total_nodes(self, value: usize) -> Self
pub const fn with_max_total_nodes(self, value: usize) -> Self
Sets max_total_nodes.
Sourcepub const fn with_max_total_decoded_string_bytes(self, value: usize) -> Self
pub const fn with_max_total_decoded_string_bytes(self, value: usize) -> Self
Trait Implementations§
Source§impl Clone for JsonLimits
impl Clone for JsonLimits
Source§fn clone(&self) -> JsonLimits
fn clone(&self) -> JsonLimits
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for JsonLimits
Source§impl Debug for JsonLimits
impl Debug for JsonLimits
Source§impl Default for JsonLimits
impl Default for JsonLimits
impl Eq for JsonLimits
Source§impl PartialEq for JsonLimits
impl PartialEq for JsonLimits
Source§fn eq(&self, other: &JsonLimits) -> bool
fn eq(&self, other: &JsonLimits) -> bool
self and other values to be equal, and is used by ==.