core:json
JSON serialization and deserialization (self-describing format).
Implements the core:serde Serializer and Deserializer traits for JSON.
Serde I/O is bytes-primary: prefer to_bytes / from_bytes when the data
is consumed as bytes. to_bytes_pretty and to_bytes_canonical cover the
indented and the deterministic (sorted-key) forms.
Synopsis
struct Point {
x: i32,
y: i32,
}
impl Serialize for Point;
impl Deserialize for Point;
assert to_string(&Point { x: 1, y: 2 }) matches { Ok(s) && s == "{\"x\":1,\"y\":2}" };
assert from_string::<Point>("{\"x\":1,\"y\":2}") matches { Ok(p) && p.x == 1 && p.y == 2 };
Functions
pub fn write_escaped_string(buf: &mut String, s: &String)
Writes a JSON-escaped string (with surrounding quotes) into buf.
Only ", \, and controls (< 0x20) are escaped — all ASCII — so unescaped
runs (including any multi-byte UTF-8) bulk-copy instead of re-encoding.
pub fn to_string<T: Serialize>(value: &T) -> Result<String, SerializeError>
Serializes a value to a JSON string. Convenience over to_bytes for text
contexts; serde I/O is bytes-primary, so prefer to_bytes when the output
is consumed as bytes (e.g. an HTTP response body).
pub fn to_bytes<T: Serialize>(value: &T) -> Result<List<u8>, SerializeError>
Serializes a value directly to UTF-8 JSON bytes.
Equivalent to to_string followed by a byte-buffer conversion, but
skips the intermediate re-encoding: the serializer's UTF-8 buffer is
handed back directly as an List<u8> with no copy. Prefer this over
to_string(value).bytes().collect() whenever the output is consumed
as bytes (e.g. an HTTP response body).
pub fn to_string_pretty<T: Serialize>(value: &T) -> Result<String, SerializeError>
Serializes a value to a pretty JSON string. Convenience over
to_bytes_pretty; serde I/O is bytes-primary.
pub fn from_string<T: Deserialize>(input: String) -> Result<T, DeserializeError>
Deserializes a value from a JSON string. Convenience wrapper over
from_bytes (a String is a UTF-8 byte source); serde I/O is bytes-primary.
pub fn from_bytes<T: Deserialize, S: AsByteSlice>(input: S) -> Result<T, DeserializeError>
Deserializes a value from UTF-8 JSON bytes — the primary entry point.
Accepts any byte source via AsByteSlice: a ByteList, ByteArray,
ByteSlice, or a String (its UTF-8 bytes). The deserializer scans the
borrowed bytes directly (zero-copy); UTF-8 is validated where string content
is materialized (RFC 8259 §8.1), reporting invalid bytes as MalformedInput.
pub fn to_bytes_canonical<T: Serialize>(value: &T) -> Result<List<u8>, SerializeError>
Serializes a value to canonical (deterministic) UTF-8 JSON bytes.
Object / struct keys are emitted in ascending bytewise order of their
encoded form (RFC 8785-style); equal values produce byte-identical output
regardless of map insertion order. Use this for signing or content
addressing; use to_bytes for the faster insertion-order form.
pub fn to_bytes_pretty<T: Serialize>(value: &T) -> Result<List<u8>, SerializeError>
Serializes a value to pretty-printed UTF-8 JSON bytes.
Structs
pub struct JsonSerializer
Fields are private.
impl Serializer for JsonSerializer
fn serialize_i32(&mut self, v: i32) -> Result<(), SerializeError>
fn serialize_i64(&mut self, v: i64) -> Result<(), SerializeError>
fn serialize_u32(&mut self, v: u32) -> Result<(), SerializeError>
fn serialize_u64(&mut self, v: u64) -> Result<(), SerializeError>
fn serialize_i128(&mut self, v: i128) -> Result<(), SerializeError>
fn serialize_u128(&mut self, v: u128) -> Result<(), SerializeError>
fn serialize_f32(&mut self, v: f32) -> Result<(), SerializeError>
fn serialize_f64(&mut self, v: f64) -> Result<(), SerializeError>
fn serialize_bool(&mut self, v: bool) -> Result<(), SerializeError>
fn serialize_char(&mut self, v: char) -> Result<(), SerializeError>
fn serialize_string(&mut self, v: &String) -> Result<(), SerializeError>
fn serialize_bytes(&mut self, v: ByteSlice) -> Result<(), SerializeError>
fn serialize_null(&mut self) -> Result<(), SerializeError>
fn begin_seq(&mut self, len: i32) -> Result<JsonSeqSerializer, SerializeError>
fn begin_map(&mut self, len: i32) -> Result<JsonMapSerializer, SerializeError>
fn begin_struct(&mut self, name: &String, fields: i32) -> Result<JsonStructSerializer, SerializeError>
fn serialize_unit_variant(&mut self, type_name: &String, variant_name: &String, disc: i32) -> Result<(), SerializeError>
fn begin_variant(&mut self, type_name: &String, variant_name: &String, disc: i32) -> Result<JsonVariantSerializer, SerializeError>
pub struct PrettyJsonSerializer
buf: &mut String
indent: &String
depth: i32
impl Serializer for PrettyJsonSerializer
fn serialize_i32(&mut self, v: i32) -> Result<(), SerializeError>
fn serialize_i64(&mut self, v: i64) -> Result<(), SerializeError>
fn serialize_u32(&mut self, v: u32) -> Result<(), SerializeError>
fn serialize_u64(&mut self, v: u64) -> Result<(), SerializeError>
fn serialize_i128(&mut self, v: i128) -> Result<(), SerializeError>
fn serialize_u128(&mut self, v: u128) -> Result<(), SerializeError>
fn serialize_f32(&mut self, v: f32) -> Result<(), SerializeError>
fn serialize_f64(&mut self, v: f64) -> Result<(), SerializeError>
fn serialize_bool(&mut self, v: bool) -> Result<(), SerializeError>
fn serialize_char(&mut self, v: char) -> Result<(), SerializeError>
fn serialize_string(&mut self, v: &String) -> Result<(), SerializeError>
fn serialize_bytes(&mut self, v: ByteSlice) -> Result<(), SerializeError>
fn serialize_null(&mut self) -> Result<(), SerializeError>
fn begin_seq(&mut self, len: i32) -> Result<PrettyJsonSeqSerializer, SerializeError>
fn begin_map(&mut self, len: i32) -> Result<PrettyJsonMapSerializer, SerializeError>
fn begin_struct(&mut self, name: &String, fields: i32) -> Result<PrettyJsonStructSerializer, SerializeError>
fn serialize_unit_variant(&mut self, type_name: &String, variant_name: &String, disc: i32) -> Result<(), SerializeError>
fn begin_variant(&mut self, type_name: &String, variant_name: &String, disc: i32) -> Result<PrettyJsonVariantSerializer, SerializeError>
pub struct JsonDeserializer
input: ByteSlice
pos: i32
pub fn peek(&self) -> i32
pub fn advance(&mut self)
pub fn skip_whitespace(&mut self)
pub fn expect_char(&mut self, c: i32) -> Result<(), DeserializeError>
pub fn read_json_string(&mut self) -> Result<String, DeserializeError>
pub fn read_number_raw(&mut self) -> Result<String, DeserializeError>
Reads a JSON number token as a String. Validates the token's prefix
(optional '-' followed by a digit), then delegates the scan to
skip_number.
pub fn skip_number(&mut self)
Advances past a JSON number in self.input without allocating a String.
pub fn parse_i32_direct(&mut self) -> Result<i32, DeserializeError>
Parses an i32 directly from self.input, without an intermediate
String allocation.
pub fn parse_i64_direct(&mut self) -> Result<i64, DeserializeError>
Parses an i64 directly from self.input, without an intermediate
String allocation.
pub fn parse_u64_direct(&mut self) -> Result<u64, DeserializeError>
Parses a u64 directly from self.input; only the rare token with one
digit more than FPFMT_DIGITS_MAX allocates (for an exact re-parse).
pub fn parse_f64_direct(&mut self) -> Result<f64, DeserializeError>
Parses an f64 directly from self.input using fpfmt, without an
intermediate String allocation.
pub fn skip_string(&mut self) -> Result<(), DeserializeError>
Advances past a JSON string without allocating.
pub fn skip_value(&mut self) -> Result<(), DeserializeError>
Skips the next JSON value without allocating.
impl Deserializer for JsonDeserializer
fn deserialize_i32(&mut self) -> Result<i32, DeserializeError>
fn deserialize_i64(&mut self) -> Result<i64, DeserializeError>
fn deserialize_u32(&mut self) -> Result<u32, DeserializeError>
fn deserialize_u64(&mut self) -> Result<u64, DeserializeError>
fn deserialize_i128(&mut self) -> Result<i128, DeserializeError>
fn deserialize_u128(&mut self) -> Result<u128, DeserializeError>
fn deserialize_f32(&mut self) -> Result<f32, DeserializeError>
fn deserialize_f64(&mut self) -> Result<f64, DeserializeError>
fn deserialize_bool(&mut self) -> Result<bool, DeserializeError>
fn deserialize_char(&mut self) -> Result<char, DeserializeError>
fn deserialize_string(&mut self) -> Result<String, DeserializeError>
fn deserialize_bytes(&mut self) -> Result<ByteList, DeserializeError>
fn is_null(&mut self) -> Result<bool, DeserializeError>
fn begin_seq(&mut self) -> Result<JsonSeqAccess, DeserializeError> with stores[self]
fn begin_map(&mut self) -> Result<JsonMapAccess, DeserializeError> with stores[self]
fn begin_struct(&mut self, name: &String, num_fields: i32) -> Result<JsonStructAccess, DeserializeError> with stores[self]
fn begin_variant(&mut self, type_name: &String, num_cases: i32) -> Result<JsonVariantAccess, DeserializeError> with stores[self]
fn deserialize_any<V: Visitor>(&mut self, visitor: &mut V) -> Result<V::Value, DeserializeError>
pub struct CanonicalJsonSerializer
Fields are private.
