Skip to main content

JsonLimits

Struct JsonLimits 

Source
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: usize

Maximum size of the whole input, in bytes.

§max_depth: usize

Maximum nesting depth of arrays and objects.

§max_string_bytes: usize

Maximum decoded size of a single string value, in bytes.

§max_key_bytes: usize

Maximum decoded size of a single object key, in bytes.

§max_number_bytes: usize

Maximum length of a single number token, in bytes.

§max_array_items: usize

Maximum number of items in a single array.

§max_object_members: usize

Maximum number of members in a single object.

§max_total_nodes: usize

Maximum total number of values (nodes) in the document.

§max_total_decoded_string_bytes: usize

Maximum total decoded string bytes across the whole document.

Implementations§

Source§

impl JsonLimits

Source

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.

Source

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?
examples/basic.rs (line 12)
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}
Source

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.

Source

pub const fn with_max_depth(self, value: usize) -> Self

Sets max_depth.

Source

pub const fn with_max_input_bytes(self, value: usize) -> Self

Source

pub const fn with_max_string_bytes(self, value: usize) -> Self

Source

pub const fn with_max_total_nodes(self, value: usize) -> Self

Trait Implementations§

Source§

impl Clone for JsonLimits

Source§

fn clone(&self) -> JsonLimits

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for JsonLimits

Source§

impl Debug for JsonLimits

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for JsonLimits

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for JsonLimits

Source§

impl PartialEq for JsonLimits

Source§

fn eq(&self, other: &JsonLimits) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for JsonLimits

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.