Skip to main content

Module formats

Module formats 

Source
Expand description

Dependency-free multi-format codec engine.

NextJson serializes and deserializes values through the format-neutral crate::ser::FormatEncoder and crate::de::FormatDecoder contracts. The same NsonSerialize / NsonDeserialize implementation can be used with each codec whose documented wire model can represent the value. Most encoders emit events directly; document-oriented codecs may collect a Value before producing bytes.

§Registry

  • Text, self-describing: json, json5, hjson, yaml, toml, ron, sexpr, csv, urlform.
  • Binary, self-describing: cbor, msgpack, bson, bencode, pickle.
  • Binary, schema-light: postcard.
  • Environment: envy (deserialization from process environment).

The Format trait carries the name, MIME type, file extensions and the generic encode / decode entry points, so formats are first-class values that can be passed around, stored in the all registry, or selected by detect.

§Unified typed API

use nextjson::formats;

let value = ("NextJson", vec![1_u64, 2, 3], true);

let json = formats::encode_with(&value, formats::Json)?;
let msgpack = formats::encode_with(&value, formats::MsgPack)?;
let yaml = formats::encode_with(&value, formats::Yaml)?;

let back: (String, Vec<u64>, bool) = formats::decode_with(&json, formats::Json)?;
let back_mp: (String, Vec<u64>, bool) =
    formats::decode_with(&msgpack, formats::MsgPack)?;
let back_yaml: (String, Vec<u64>, bool) =
    formats::decode_with(&yaml, formats::Yaml)?;
assert_eq!(back, back_mp);
assert_eq!(back, back_yaml);

§Format-to-format transcoding

transcode converts between compatible formats without a typed value:

use nextjson::formats;
let json = br#"{"name":"NextJson","values":[1,2,3]}"#;
let msgpack = formats::transcode(json, formats::Json, formats::MsgPack)?;
let json2 = formats::transcode(&msgpack, formats::MsgPack, formats::Json)?;
assert_eq!(json2, json);

Modules§

bin
Shared primitives for the binary codecs.

Structs§

Bencode
Bencode format marker.
BencodeDecoder
Streaming bencode decoder.
BencodeEncoder
Streaming bencode encoder.
Bson
BSON format marker.
BsonDecoder
Streaming BSON decoder.
BsonEncoder
Streaming BSON encoder.
Cbor
CBOR format marker.
Csv
CSV format marker.
CsvDecoder
Streaming CSV decoder over pre-parsed rows.
CsvEncoder
Streaming CSV encoder.
Envy
Envy format marker (deserialization from std::env).
FormatInfo
Metadata for one registered format.
Hjson
Hjson format marker.
HjsonDecoder
Streaming Hjson decoder.
Json
JSON format marker.
Json5
JSON5 format marker.
Json5Decoder
Streaming JSON5 decoder.
MsgPack
MessagePack format marker.
MsgPackDecoder
Streaming MessagePack decoder.
MsgPackEncoder
Streaming MessagePack encoder.
Pickle
Pickle format marker.
PickleEncoder
Streaming pickle (protocol 2) encoder.
Postcard
Postcard format marker.
PostcardDecoder
Streaming Postcard decoder.
PostcardEncoder
Streaming Postcard encoder.
Ron
RON format marker.
RonDecoder
Streaming RON decoder.
RonEncoder
Streaming RON encoder.
Sexpr
S-expression format marker.
SexprDecoder
Streaming S-expression decoder.
SexprEncoder
Streaming S-expression encoder.
Toml
TOML format marker.
TomlEncoder
TOML encoder that collects one event stream and emits it on finish.
TreeDecoder
A FormatDecoder that replays an owned token stream produced from a Value tree.
UrlForm
URL form-encoding format marker.
UrlFormDecoder
Streaming URL-form decoder.
UrlFormEncoder
Streaming URL-form encoder (flat key/value map only).
Yaml
YAML format marker.
YamlEncoder
YAML encoder that collects one event stream and emits it on finish.

Enums§

FormatKind
Discriminant for every format registered in all.

Traits§

Format
A registered data format with generic typed entry points.

Functions§

all
All registered formats.
by_extension
Look up a format by file extension (with or without a leading dot).
by_name
Look up a format by canonical name (case-insensitive).
decode_with
Deserialize a value with an explicit format.
detect
Sniff the most likely format from a byte prefix.
encode_with
Serialize a value with an explicit format.
to_value
Decode one format’s bytes into a Value.
transcode
Convert between any two formats without a typed value.

Type Aliases§

CborDecoder
CBOR decoder serving the unified interface over a JSON relay.
CborEncoder
The CBOR encoder writes through the JSON relay.
EnvyDecoder
Envy decoder serving the unified interface from a parsed Value.
HjsonEncoder
The Hjson encoder is the standard JSON encoder (Hjson is a superset).
Json5Encoder
The JSON5 encoder is the standard JSON encoder (JSON5 is a superset).
JsonDecoder
JSON decoder serving the unified interface (the native Decoder, which already implements crate::de::FormatDecoder).
JsonEncoder
The JSON encoder is the native buffered encoder.
PickleDecoder
Pickle decoder that serves the unified interface from a parsed Value.
TomlDecoder
TOML decoder serving the unified interface from a parsed Value.
YamlDecoder
YAML decoder serving the unified interface from a parsed Value.