Skip to main content

Crate jstrict

Crate jstrict 

Source
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::Options type 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 Object is a list of entries in definition order, with an index for O(1) lookup.
  • Short strings, keys and numbers live on the stack (see SMALL_STRING_CAPACITY).
  • Highly configurable printing, see Print and print::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

FlagDefaultEnables
simdutf8yesSIMD-accelerated UTF-8 validation in the slice parser
canonicalizeJSON Canonicalization Scheme, RFC 8785
serdeSerialize / Deserialize, plus to_value / from_value
serde_jsonConversion from/to serde_json::Value
sonic-rsConversion from/to sonic_rs::Value
contextualcontextual::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.
print
JSON printing.

Macros§

json
Constructs a jstrict::Value from a JSON literal.

Structs§

KeySerializerserde
Serializer for object keys.
NumberType
Marker naming the numeric type a conversion was attempted into.
SerializeArrayserde
Serializes a sequence, tuple or tuple struct into a JSON array.
SerializeStructVariantserde
Serializes a struct enum variant into a single-entry object mapping the variant name to an object.
SerializeTupleVariantserde
Serializes a tuple enum variant into a single-entry object mapping the variant name to an array.
Serializerserde
Value serializer.
StringNumberSerializerserde
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§

DeserializeErrorserde
Error returned when deserializing a type from a Value.
FragmentRef
Reference to a fragment of a JSON value.
SerializeErrorserde
Error returned when serializing a type into a Value.
SerializeMapserde
Serializes a map or struct into a JSON object.
SubFragments
Iterator over the direct sub-fragments of a FragmentRef.
TryIntoNumberError
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§

BorrowUnordered
Free conversion from &T to &Unordered<T>.
TryFromJson
Conversion from JSON syntax, with code mapping info.
TryFromJsonObject
Conversion from JSON syntax object, with code mapping info.
UnorderedEq
Marker trait stating that UnorderedPartialEq is an equivalence relation, mirroring Eq over PartialEq.
UnorderedHash
Hashing that ignores the order of object entries.
UnorderedPartialEq
Equality that ignores the order of object entries.

Functions§

from_valueserde
Deserializes the JSON value into an instance of type T.
get_array_fragment
Returns the fragment of array at the given index, in traversal order.
to_valueserde
Serializes the given value into a JSON Value.

Type Aliases§

NumberBuf
Number buffer.
String
String.