pub struct JsonLimits {
pub max_input_bytes: usize,
pub max_depth: usize,
pub max_string_bytes: usize,
pub max_key_bytes: usize,
pub max_number_bytes: usize,
pub max_array_items: usize,
pub max_object_members: usize,
pub max_total_nodes: usize,
pub max_total_decoded_string_bytes: usize,
}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.
Fields§
§max_input_bytes: usizeMaximum size of the whole input, in bytes.
max_depth: usizeMaximum nesting depth of arrays and objects.
max_string_bytes: usizeMaximum decoded size of a single string value, in bytes.
max_key_bytes: usizeMaximum decoded size of a single object key, in bytes.
max_number_bytes: usizeMaximum length of a single number token, in bytes.
max_array_items: usizeMaximum number of items in a single array.
max_object_members: usizeMaximum number of members in a single object.
max_total_nodes: usizeMaximum total number of values (nodes) in the document.
max_total_decoded_string_bytes: usizeMaximum total decoded string bytes across the whole document.
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 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_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_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_total_nodes(self, value: usize) -> Self
pub const fn with_max_total_nodes(self, value: usize) -> Self
Sets max_total_nodes.
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 ==.