Expand description
Strict JSON parsing and mapping.
jstrict implements JSON exactly as specified by
RFC 8259 and
ECMA-404,
and preserves everything the standard leaves in the document: entry order,
duplicate keys, and the lexical form of numbers. Parsing also produces a
CodeMap recording the byte span of every value fragment, so errors and
diagnostics can point back at the source text.
§Features
- Strict by default; the
parse::Optionstype relaxes surrogate and codepoint handling for documents that do not adhere to the standard. - No stack overflow: nesting depth is bounded by available memory, not by the call stack.
- Numbers are stored in lexical form, so precision is never lost. See
Number. - Duplicate entries are preserved: an
Objectis a list of entries in definition order, with an index forO(1)lookup. - Short strings, keys and numbers live on the stack (see
SMALL_STRING_CAPACITY). - Highly configurable printing, see
Printandprint::Options. json!builds any value from a JSON literal.
§Usage
use jstrict::{Parse, Print, Value};
let (value, code_map) = Value::parse_str(r#"{ "a": 0, "b": [1, 2] }"#)?;
let object = value.as_object().unwrap();
assert_eq!(object.get_unique("a").unwrap().unwrap().to_string(), "0");
// Fragment 0 is the whole object; fragment 3 is the `0` bound to `"a"`.
assert_eq!(code_map[3].span.start(), 7);
println!("{}", value.pretty_print());When the code map is not needed, parse::parse_str_value takes the
faster path that skips span tracking entirely:
let value = jstrict::parse::parse_str_value("[1, 2, 3]")?;
assert_eq!(value.as_array().unwrap().len(), 3);§Feature flags
| Flag | Default | Enables |
|---|---|---|
simdutf8 | yes | SIMD-accelerated UTF-8 validation in the slice parser |
canonicalize | JSON Canonicalization Scheme, RFC 8785 | |
serde | Serialize / Deserialize, plus to_value / from_value | |
serde_json | Conversion from/to serde_json::Value | |
sonic-rs | Conversion from/to sonic_rs::Value | |
contextual | contextual::WithContext printing adapters |
Re-exports§
pub use crate::number::InvalidNumber;pub use crate::number::Number;pub use crate::number::Sign;pub use crate::number::TryFromFloatError;pub use code_map::CodeMap;pub use parse::Parse;pub use print::Print;pub use kind::Kind;pub use kind::KindSet;pub use array::Array;pub use object::Object;
Modules§
- array
- JSON arrays.
- code_
map - Mapping between JSON value fragments and the source text.
- kind
- JSON value kinds.
- number
- JSON number parsing and storage.
- object
- JSON objects.
- parse
- JSON parsing.
- JSON printing.
Macros§
- json
- Constructs a
jstrict::Valuefrom a JSON literal.
Structs§
- KeySerializer
serde - Serializer for object keys.
- Number
Type - Marker naming the numeric type a conversion was attempted into.
- Serialize
Array serde - Serializes a sequence, tuple or tuple struct into a JSON array.
- Serialize
Struct Variant serde - Serializes a struct enum variant into a single-entry object mapping the variant name to an object.
- Serialize
Tuple Variant serde - Serializes a tuple enum variant into a single-entry object mapping the variant name to an array.
- Serializer
serde Valueserializer.- String
Number Serializer serde - Serializer accepting only a number in string form.
- Traverse
- Depth-first iterator over every fragment of a
Value, with its index. - Unexpected
- Unexpected JSON value kind error.
- Unordered
- Wrapper to view a value without considering the order of the objects entries.
Enums§
- Deserialize
Error serde - Error returned when deserializing a type from a
Value. - Fragment
Ref - Reference to a fragment of a JSON value.
- Serialize
Error serde - Error returned when serializing a type into a
Value. - Serialize
Map serde - Serializes a map or struct into a JSON object.
- SubFragments
- Iterator over the direct sub-fragments of a
FragmentRef. - TryInto
Number Error - Error returned when converting a JSON value into a number type.
- Value
- JSON Value.
Constants§
- NUMBER_
CAPACITY - Number buffer stack capacity.
- SMALL_
STRING_ CAPACITY - String stack capacity.
Traits§
- Borrow
Unordered - Free conversion from
&Tto&Unordered<T>. - TryFrom
Json - Conversion from JSON syntax, with code mapping info.
- TryFrom
Json Object - Conversion from JSON syntax object, with code mapping info.
- Unordered
Eq - Marker trait stating that
UnorderedPartialEqis an equivalence relation, mirroringEqoverPartialEq. - Unordered
Hash - Hashing that ignores the order of object entries.
- Unordered
Partial Eq - Equality that ignores the order of object entries.
Functions§
- from_
value serde - Deserializes the JSON
valueinto an instance of typeT. - get_
array_ fragment - Returns the fragment of
arrayat the givenindex, in traversal order. - to_
value serde - Serializes the given
valueinto a JSONValue.