silk.json.codec.
The interfaces that let a type be written to JSON and read from JSON.
When to use#
Implement Serialize for your type to write it with silk.json.value.Json.serialize.
Implement Decode (no memory needed) or Deserialize (memory needed) to read it with
Json.decode or Json.deserialize.
Details#
The standard library implements the interfaces for bool, the integer types, f32, f64,
strings, Option<T>, and List<T>. Option<T> is null when absent.
A Serialize implementation for a struct usually writes an object with
silk.json.object.JsonObject, or uses silk.json.reflect.JsonReflect.write. A Deserialize
implementation reads tokens from a JsonScanner and calls JsonCodec.deserializeOne for each
field.
Errors#
JsonErrorwhen the JSON does not match the type, a number does not fit, or a float that is not finite is written. A float that is not finite fails before any bytes are written.
Gotchas#
Each read function reads exactly one value and leaves the next token, such as , or ], for
the caller. The caller must check that the document ends.
Examples#
Implement Serialize for a struct#
import silk.core.effect { Effect }
import silk.json.codec { Serialize }
import silk.json.object { JsonObject }
import silk.json.output { JsonOptions }
import silk.json.scanner { JsonError }
import silk.json.value { Json }
import silk.io.writer { Writer, WriterError }
struct CaptureWriter {
bytes: [u8; 64],
length: usize,
}
impl Writer for CaptureWriter {
effect fn writeAll(self: &mut Self, value: &[u8]) -> ()
! WriterError
? &mut Writer {
let mut index: usize = 0
while index < value.length {
self.bytes[self.length] = value[index]
self.length = self.length + 1
index = index + 1
}
return ()
}
effect fn flush(self: &mut Self) -> ()
! WriterError
? &mut Writer {
return ()
}
}
fn holds(writer: &CaptureWriter, expected: &[u8]) -> bool {
if writer.length != expected.length {
return false
}
let mut index: usize = 0
while index < expected.length {
if writer.bytes[index] != expected[index] {
return false
}
index = index + 1
}
return true
}
struct Point {
x: i32,
y: i32,
}
impl Serialize for Point {
effect fn serialize(self: &Self, options: &JsonOptions) -> ()
! JsonError | WriterError
? &mut Writer {
return run JsonObject.begin(options) |> JsonObject.field("x", &self.x)
|> JsonObject.field("y", &self.y)
|> JsonObject.end
}
}
pub effect fn main() -> i32
! JsonError | WriterError {
let mut writer = CaptureWriter { bytes: [0; 64], length: 0 }
let point = Point { x: 3, y: -4 }
run Json.serialize(&point, &JsonOptions.compact()) |> Effect.provideMut<Writer>(&mut writer)
if !holds(&writer, b"{\"x\":3,\"y\":-4}") {
return 1
}
return 0
}Import as JsonCodec with import silk.json.codec { JsonCodec }.
Public declarations: 4.
Serialize#
pub interface SerializeA type that can write itself as one JSON value.
When to use#
Implement it for your type to write the type with silk.json.value.Json.serialize. The
standard library implements it for bool, the integer types, f32, f64, strings,
Option<T> (null when absent), and List<T> (an array).
Details#
An implementation for a struct usually writes an object with silk.json.object.JsonObject, or
uses silk.json.reflect.JsonReflect.write.
Errors#
JsonErrorwhen a value cannot be written, such as a float that is not finite. A float that is not finite fails before any bytes are written.
Operation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterWrites self as one JSON value to the Writer, with options.
Decode#
pub interface DecodeA type that can read itself from one JSON value without memory from an Allocator.
When to use#
Implement it for your type to read the type with silk.json.value.Json.decode. If the type
needs memory, implement Deserialize instead. The standard library implements it for bool,
the integer types, f32, f64, and Option<T> of those (null when absent).
Gotchas#
decode reads exactly one value and leaves the next token, such as , or ], for the caller.
The caller must check that the document ends.
Operation decode#
fn decode(scanner: &mut JsonScanner) -> Result<Self, JsonError>Reads one value from scanner. It leaves the next token unread.
Deserialize#
pub interface DeserializeA type that can read itself from one JSON value and can use memory from the Allocator.
When to use#
Implement it for your type to read the type with silk.json.value.Json.deserialize. The
standard library implements it for bool, the integer types, f32, f64, strings,
Option<T> (null when absent), and List<T>.
Details#
An implementation for a struct reads tokens from the JsonScanner and calls
JsonCodec.deserializeOne for each field.
Gotchas#
deserialize reads exactly one value and leaves the next token, such as , or ], for the
caller. The caller must check that the document ends.
Operation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorReads one value from scanner. It leaves the next token unread.
JsonCodec#
pub struct JsonCodecThe owner type of the generic read and write operations. It has no fields and is never constructed.
Implementation JsonCodec#
impl JsonCodecAssociated function decodeOne#
pub fn decodeOne<T: Decode>(scanner: &mut JsonScanner) -> Result<T, JsonError>Reads one value of type T from scanner with its Decode implementation. It leaves the
next token unread.
Errors#
JsonErrorif the value is not valid JSON or does not have typeT.
Associated function deserializeOne#
pub effect fn deserializeOne<T: Deserialize>(scanner: &mut JsonScanner)
-> T ! JsonError | OutOfMemoryError ? &mut AllocatorReads one value of type T from scanner with its Deserialize implementation. It leaves
the next token unread.
When to use#
Call it for each field in your own Deserialize implementation.
Errors#
JsonErrorif the value is not valid JSON or does not have typeT.OutOfMemoryErrorif theAllocatorcannot supply the value.
Associated function writeOne#
pub effect fn writeOne<T: Serialize>(value: &T, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterWrites value with its Serialize implementation.
Errors#
JsonErrorfrom the implementation, such as for a float that is not finite.WriterErrorif theWriterfails. Bytes that it accepted before the failure stay written.
Implementation Serialize for bool#
impl Serialize for boolOperation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Decode for bool#
impl Decode for boolOperation decode#
fn decode(scanner: &mut JsonScanner) -> Result<Self, JsonError>Implementation Deserialize for bool#
impl Deserialize for boolOperation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorImplementation Serialize for u8#
impl Serialize for u8Operation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Decode for u8#
impl Decode for u8Operation decode#
fn decode(scanner: &mut JsonScanner) -> Result<Self, JsonError>Implementation Deserialize for u8#
impl Deserialize for u8Operation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorImplementation Serialize for u16#
impl Serialize for u16Operation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Decode for u16#
impl Decode for u16Operation decode#
fn decode(scanner: &mut JsonScanner) -> Result<Self, JsonError>Implementation Deserialize for u16#
impl Deserialize for u16Operation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorImplementation Serialize for u32#
impl Serialize for u32Operation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Decode for u32#
impl Decode for u32Operation decode#
fn decode(scanner: &mut JsonScanner) -> Result<Self, JsonError>Implementation Deserialize for u32#
impl Deserialize for u32Operation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorImplementation Serialize for u64#
impl Serialize for u64Operation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Decode for u64#
impl Decode for u64Operation decode#
fn decode(scanner: &mut JsonScanner) -> Result<Self, JsonError>Implementation Deserialize for u64#
impl Deserialize for u64Operation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorImplementation Serialize for usize#
impl Serialize for usizeOperation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Decode for usize#
impl Decode for usizeOperation decode#
fn decode(scanner: &mut JsonScanner) -> Result<Self, JsonError>Implementation Deserialize for usize#
impl Deserialize for usizeOperation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorImplementation Serialize for i8#
impl Serialize for i8Operation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Decode for i8#
impl Decode for i8Operation decode#
fn decode(scanner: &mut JsonScanner) -> Result<Self, JsonError>Implementation Deserialize for i8#
impl Deserialize for i8Operation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorImplementation Serialize for i16#
impl Serialize for i16Operation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Decode for i16#
impl Decode for i16Operation decode#
fn decode(scanner: &mut JsonScanner) -> Result<Self, JsonError>Implementation Deserialize for i16#
impl Deserialize for i16Operation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorImplementation Serialize for i32#
impl Serialize for i32Operation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Decode for i32#
impl Decode for i32Operation decode#
fn decode(scanner: &mut JsonScanner) -> Result<Self, JsonError>Implementation Deserialize for i32#
impl Deserialize for i32Operation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorImplementation Serialize for i64#
impl Serialize for i64Operation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Decode for i64#
impl Decode for i64Operation decode#
fn decode(scanner: &mut JsonScanner) -> Result<Self, JsonError>Implementation Deserialize for i64#
impl Deserialize for i64Operation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorImplementation Serialize for isize#
impl Serialize for isizeOperation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Decode for isize#
impl Decode for isizeOperation decode#
fn decode(scanner: &mut JsonScanner) -> Result<Self, JsonError>Implementation Deserialize for isize#
impl Deserialize for isizeOperation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorImplementation Decode for f32#
impl Decode for f32Operation decode#
fn decode(scanner: &mut JsonScanner) -> Result<Self, JsonError>Implementation Deserialize for f32#
impl Deserialize for f32Operation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorImplementation Serialize for f32#
impl Serialize for f32Operation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Decode for f64#
impl Decode for f64Operation decode#
fn decode(scanner: &mut JsonScanner) -> Result<Self, JsonError>Implementation Deserialize for f64#
impl Deserialize for f64Operation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorImplementation Serialize for f64#
impl Serialize for f64Operation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Serialize for string<'a>#
impl<'a> Serialize for string<'a>Operation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Serialize for String#
impl Serialize for StringOperation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Deserialize for String#
impl Deserialize for StringOperation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorImplementation Serialize for Option<T>#
impl<T: Serialize> Serialize for Option<T>Operation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Decode for Option<T>#
impl<T: Decode> Decode for Option<T>Operation decode#
fn decode(scanner: &mut JsonScanner) -> Result<Self, JsonError>Implementation Deserialize for Option<T>#
impl<T: Deserialize> Deserialize for Option<T>Operation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorImplementation Serialize for List<T>#
impl<T: Serialize> Serialize for List<T>Operation serialize#
effect fn serialize(self: &Self, options: &JsonOptions)
-> () ! JsonError | WriterError ? &mut WriterImplementation Deserialize for List<T>#
impl<T: Deserialize> Deserialize for List<T>Operation deserialize#
effect fn deserialize(scanner: &mut JsonScanner)
-> Self ! JsonError | OutOfMemoryError ? &mut AllocatorCategory: Data formats.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.