Expand description
§NextJson
A dependency-free, no_std + alloc JSON and CBOR library for Rust.
The public native contracts are NsonSerialize::nextencode,
NsonDeserialize::nextdecode_into, nextencode, and nextdecode.
JSON and the JSON-compatible CBOR profile can also be relayed through the
format-neutral cross_format::EventSink protocol without constructing an
intermediate Value tree.
§Quick start
let expected = (7_u64, "NextJson", vec![1_i32, 2, 3]);
let json = nextjson::nextencode(&expected)?;
let actual: (u64, &str, Vec<i32>) = nextjson::nextdecode(&json)?;
assert_eq!(actual, expected);§Cross-format relay
use nextjson::cross_format;
let source = br#"{"name":"NextJson","values":[1,2,3]}"#;
let cbor = cross_format::json_to_cbor(source)?;
let json = cross_format::cbor_to_json(&cbor)?;
let value: nextjson::Value = nextjson::nextdecode(&json)?;
assert_eq!(value["name"], nextjson::Value::from("NextJson"));§Zero-copy boundary
Unescaped JSON strings and definite-length CBOR text strings borrow their input ranges. Escaped JSON and indefinite-length CBOR text must materialize decoded UTF-8. Encoding always writes new output bytes. The library does not describe those required copies as zero-copy.
§Features
std(default): standard I/O adapters and standard-library integrations.derive(default): repository-ownedNsonSerializeandNsonDeserializeprocedural macros.
Disabling default features leaves a core + alloc implementation. The
complete workspace dependency graph contains only nextjson and the local,
optional nextjson-derive crate.
§Architecture
NextJson uses schema-driven derives, a unified token stream, checked decode
slots, and a format-neutral cross_format::EventSink protocol. The whole
workspace build graph contains only its two local crates.
The following properties are implemented directly in this repository and are enforced by its tests and build configuration:
- Direct dual contract -
NsonSerialize::nextencodewrites bytes directly;NsonDeserialize::nextdecode_intodecodes into a caller-provided checked nextdecode slot, supporting memory reuse without a placeholder value. - Compile-time schema - every type carries
const SCHEMA: TypeSchema, a runtime-introspectable metadata tree (usable for JSON Schema generation, validation, and tooling). - Unified token stream - the byte-stream lexer and the content-replay
reader share identical nextdecode primitives, so internally-tagged,
adjacently-tagged, and untagged enums plus
Valueround-trips reuse one engine. - Lazy single-token lookahead - the parser lexes one token at a time; unescaped strings borrow the input with zero allocation; integer parsing is hand-rolled with overflow detection.
- Safety boundary - the library is
#![deny(unsafe_code)], including nextdecode slots and partial-initialization cleanup.no_stdis fully supported: the core uses onlycore+alloc, withstd-only types behind thestdfeature. - Streaming cross-format relay - JSON and the JSON-compatible CBOR
profile exchange borrowed structural events without an intermediate
Value. - One validated event protocol - every format encoder and both
cross-format sinks validate container / key / value ordering through a
single shared state machine, parameterized only by whether the wire
format has explicit array separators (JSON does, CBOR does not). The
byte lexer additionally serves typed scalar reads (
number,string,bool,Optiondispatch) directly from the source byte, so the token stream stays available for content replay without taxing the hot path.
§Safety and resource limits
This crate denies unsafe Rust. Decode slots use checked state, numeric
conversions use checked arithmetic, and decoders cap nesting at 128 by
default.
Applications must still enforce total input bytes, collection sizes, CPU
time, and output quotas. from_slice / from_str operate on a complete
in-memory input; from_reader (std) pulls incrementally from any
std::io::Read source.
See the repository’s [English README], [Chinese README], [safety model], and [benchmark protocol] for the complete supported surface and reproducibility requirements.
Re-exports§
pub use crate::de::DecodeConfig;pub use crate::de::DecodeSlot;pub use crate::de::Decoder;pub use crate::de::FormatDecoder;pub use crate::de::NsonDeserialize;pub use crate::de::OptionTag;pub use crate::encoding::EncodeConfig;pub use crate::encoding::Encoder;pub use crate::encoding::FastEncoder;pub use crate::error::Error;pub use crate::error::FormatError;pub use crate::error::Result;pub use crate::map::Map;pub use crate::stream::StreamDecoder;
Modules§
- cross_
format - Dependency-free streaming interoperability between structured formats.
- de
- Deserialization: a unified token-stream decoder, the
NsonDeserializetrait, and standard-library implementations. - encoding
- Encoder configuration and construction.
- error
- Error model: JSON errors with precise position (line / column / offset).
- formats
- Dependency-free multi-format codec engine.
- map
- Insertion-ordered JSON object map.
- stream
- Streaming JSON decoding from a
std::io::Readsource.
Macros§
Structs§
- Bytes
- A borrowed byte string that round-trips through the dedicated bytes path.
- Enum
Schema - Compile-time description of an enum.
- Field
Schema - Compile-time description of a struct field.
- Struct
Schema - Compile-time description of a struct.
- Variant
Schema - Compile-time description of an enum variant.
Enums§
- Number
- A JSON number.
- Type
Schema - Compile-time description of a type’s structure.
- Value
- A self-describing JSON value.
Traits§
- Format
Encoder - Format-neutral emission contract implemented by every destination codec.
- Nson
Schema - Compile-time schema provider.
- Nson
Serialize - Serialization trait: nextencode
Selfinto anyFormatEncoder. - Write
- A minimal byte sink.
Functions§
- from_
reader - Deserialize from a
std::io::Read(requires thestdfeature). - from_
slice - Deserialize from a
&[u8]. The'delifetime allows types to borrow input. - from_
str - Deserialize from a
&str. The'delifetime allows types to borrow input. - from_
value - Convert a
Valueinto any type (owned, deserializable for any lifetime). - nextdecode
- Decode one complete JSON value using the native NextJson data model.
- nextencode
- Encode a value into a compact JSON byte vector using the native NextJson data model.
- schema_
of - Get the compile-time
TypeSchemaof a type (runtime-introspectable). - to_
io_ writer - Serialize a value to a
std::io::Writesink (requires thestdfeature). - to_
json_ schema - Generate a JSON Schema (draft-07 style) for any
NsonSchematype. - to_
string - Serialize a value into a compact JSON string.
- to_
string_ pretty - Serialize a value into a pretty-printed JSON string.
- to_
value - Convert any serializable value into a
Value. - to_vec
- Serialize a value into a compact JSON byte vector.
- to_
vec_ pretty - Serialize a value into a pretty-printed JSON byte vector.
- to_
writer - Serialize a value to any
Writesink. - to_
writer_ pretty - Serialize a value to any
Writesink with pretty printing.
Derive Macros§
- Nson
Deserialize - Derive NextJson’s native decoding contract.
- Nson
Serialize - Derive NextJson’s native serialization contract and compile-time schema.