json

moon add tonyfettes/json@0.4.2
Download zip
Version
0.4.2
License
Apache-2.0
Last updated
6 months ago
Downloads
44
README

#tonyfettes/json

ToJson and FromJson that produce Number for Int64.

#
Replacer

A Replacer provides a way to filter and transform JSON object properties during stringification.

Replacers contain a function that takes a property key and value, and returns:
  • Some(value) to include the property in the output (possibly transformed)
  • None to exclude the property from the output

Only applies to object properties, not array elements.

#
FromJson

pub(open) trait FromJson {
from_json(Json, JsonPath) -> Self raise JsonDecodeError
}

Trait for types that can be converted from Json
impl FromJson for Unit
impl FromJson for Bool
impl FromJson for Char
impl FromJson for Int
impl FromJson for Int64
impl FromJson for UInt
impl FromJson for UInt64
impl FromJson for Float
impl FromJson for Double
impl FromJson for String
impl FromJson for FixedArray[X]
impl FromJson for Bytes
impl FromJson for Array[X]
impl FromJson for ArrayView[X]
impl FromJson for Map[String, V]
impl FromJson for Tuple2[A, B]
impl FromJson for Tuple3[A, B, C]
impl FromJson for Tuple4[A, B, C, D]
impl FromJson for Tuple5[A, B, C, D, E]
impl FromJson for Tuple6[A, B, C, D, E, F]
impl FromJson for Tuple7[A, B, C, D, E, F, G]
impl FromJson for Tuple8[T0, T1, T2, T3, T4, T5, T6, T7]
impl FromJson for Tuple9[T0, T1, T2, T3, T4, T5, T6, T7, T8]
impl FromJson for Tuple10[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9]
impl FromJson for Tuple11[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]
impl FromJson for Tuple12[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11]
impl FromJson for Tuple13[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12]
impl FromJson for Tuple14[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13]
impl FromJson for Tuple15[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14]
impl FromJson for Tuple16[T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15]

#
ToJson

pub(open) trait ToJson {
to_json(Self) -> Json
}

impl ToJson for Bool
impl ToJson for Char
impl ToJson for Int
impl ToJson for Int64
impl ToJson for UInt
impl ToJson for UInt64
impl ToJson for Float
impl ToJson for Double
impl ToJson for String
impl ToJson for FixedArray[A]
impl ToJson for Array[A]
impl ToJson for ArrayView[A]
impl ToJson for Map[String, V]
impl ToJson for MutArrayView[A]
impl ToJson for Tuple2[A, B]
impl ToJson for Tuple3[A, B, C]
impl ToJson for Tuple4[A, B, C, D]
impl ToJson for Tuple5[A, B, C, D, E]
impl ToJson for Tuple6[A, B, C, D, E, F]
impl ToJson for Tuple7[A, B, C, D, E, F, G]
impl ToJson for Tuple8[A, B, C, D, E, F, G, H]
impl ToJson for Tuple9[A, B, C, D, E, F, G, H, I]
impl ToJson for Tuple10[A, B, C, D, E, F, G, H, I, J]
impl ToJson for Tuple11[A, B, C, D, E, F, G, H, I, J, K]
impl ToJson for Tuple12[A, B, C, D, E, F, G, H, I, J, K, L]
impl ToJson for Tuple13[A, B, C, D, E, F, G, H, I, J, K, L, M]
impl ToJson for Tuple14[A, B, C, D, E, F, G, H, I, J, K, L, M, N]
impl ToJson for Tuple15[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O]
impl ToJson for Tuple16[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P]

#
JsonDecodeError

pub(all) suberror JsonDecodeError {
JsonDecodeError(JsonPath, String)
}

#
ParseError

pub(all) suberror ParseError {
InvalidChar(Position, Char)
InvalidEof
InvalidNumber(Position, String)
InvalidIdentEscape(Position)
DepthLimitExceeded
}

impl Eq for ParseError
impl Debug for ParseError

#
Json

pub enum Json {
Null
Bool(Bool)
Number(Number)
String(String)
Array(Array[Json])
Object(Map[String, Json])
}

impl Eq for Json
impl Debug for Json
impl FromJson for Json
impl ToJson for Json

#
Json::array

#as_free_fn
fn Json::array(elements : Array[Json]) -> Json

#
Json::bigint

#as_free_fn
fn Json::bigint(n :
BigInt
) -> Json

#
Json::bool

#as_free_fn
fn Json::bool(b : Bool) -> Json

#
Json::double

#as_free_fn
fn Json::double(n : Double) -> Json

#
Json::float

#as_free_fn
fn Json::float(n : Float) -> Json

#
Json::int

#as_free_fn
fn Json::int(n : Int) -> Json

#
Json::int64

#as_free_fn
fn Json::int64(n : Int64) -> Json

#
Json::number

#as_free_fn
fn Json::number(n : Number) -> Json

#
Json::object

#as_free_fn
fn Json::object(object : Map[String, Json]) -> Json

#
Json::string

#as_free_fn
fn Json::string(s : String) -> Json

#
Json::stringify

fn Json::stringify(self : Json, escape_slash? : Bool, indent? : Int) -> String

#
Json::uint

#as_free_fn
fn Json::uint(n : UInt) -> Json

#
Json::uint64

#as_free_fn
fn Json::uint64(n : UInt64) -> Json

#
JsonPath

type JsonPath

impl Eq for JsonPath
impl Show for JsonPath
impl Debug for JsonPath
impl ToJson for JsonPath

#
JsonPath::add_index

fn JsonPath::add_index(self : JsonPath, index : Int) -> JsonPath

#
JsonPath::add_key

fn JsonPath::add_key(self : JsonPath, key : String) -> JsonPath

#
JsonPath::root

fn JsonPath::root() -> JsonPath

#
Number

type Number

impl Eq for Number
impl Debug for Number

#
Number::from_bigint

#
Number::from_double

fn Number::from_double(value : Double) -> Number

#
Number::from_float

fn Number::from_float(value : Float) -> Number

#
Number::from_int

fn Number::from_int(value : Int) -> Number

#
Number::from_int64

fn Number::from_int64(value : Int64) -> Number

#
Number::from_uint

fn Number::from_uint(value : UInt) -> Number

#
Number::from_uint64

fn Number::from_uint64(value : UInt64) -> Number

#
Number::to_double

fn Number::to_double(self : Number, path? : JsonPath) -> Double raise JsonDecodeError

#
Number::to_float

fn Number::to_float(self : Number, path? : JsonPath) -> Float raise JsonDecodeError

#
Number::to_int

fn Number::to_int(self : Number, path? : JsonPath) -> Int raise JsonDecodeError

#
Number::to_int64

fn Number::to_int64(self : Number, path? : JsonPath) -> Int64 raise JsonDecodeError

#
Number::to_string

fn Number::to_string(self : Number) -> String

#
Number::to_uint

fn Number::to_uint(self : Number, path? : JsonPath) -> UInt raise JsonDecodeError

#
Number::to_uint64

fn Number::to_uint64(self : Number, path? : JsonPath) -> UInt64 raise JsonDecodeError

#
ObjectReader

pub(all) type ObjectReader Map[String, Json]

#
ObjectReader::get

fn ObjectReader::get(self : ObjectReader, key : String) -> Json?

#
ObjectReader::inner

#deprecated("Use `struct T(A)` to declare a newtype and use `.0` access the underlying type instead.")
fn ObjectReader::inner(self : ObjectReader) -> Map[String, Json]
Convert newtype to its underlying type, automatically derived.

#
ObjectReader::iter2

fn ObjectReader::iter2(self : ObjectReader) -> Iter2[String, Json]

Returns an iterator over the key-value pairs in the JSON object.

Parameters:

  • self : The JSON object to iterate over.

Returns an Iter2[String, Json] that yields each key-value pair in the object, where keys are strings and values are JSON values.

#
ObjectReader::new

fn ObjectReader::new(json : Json, path~ : JsonPath) -> ObjectReader raise JsonDecodeError

#
ObjectReader::optional

fn[T : FromJson] ObjectReader::optional(object : ObjectReader, key : String, path~ : JsonPath) -> T? raise JsonDecodeError

Extracts and deserializes a nullable field from a JSON object.

Parameters:

  • object : The JSON object to extract the field from.
  • key : The name of the nullable field to extract.
  • path : The current JSON path for error reporting context.

Returns the deserialized value of type T from the specified field if the field is present and not null, None otherwise.

Throws an error of type JsonDecodeError if the field is present but deserialization fails.

#
ObjectReader::required

fn[T : FromJson] ObjectReader::required(object : ObjectReader, key : String, path~ : JsonPath) -> T raise JsonDecodeError

Extracts and deserializes a required field from a JSON object.

Parameters:

  • object : The JSON object to extract the field from.
  • key : The name of the required field to extract.
  • path : The current JSON path for error reporting context.

Returns the deserialized value of type T from the specified field.

Throws an error of type JsonDecodeError if the field is missing from the object or if deserialization fails.

#
ObjectWriter

pub(all) type ObjectWriter Map[String, Json]

#
ObjectWriter::inner

#deprecated("Use `struct T(A)` to declare a newtype and use `.0` access the underlying type instead.")
fn ObjectWriter::inner(self : ObjectWriter) -> Map[String, Json]
Convert newtype to its underlying type, automatically derived.

#
ObjectWriter::new

#
ObjectWriter::optional

fn[T : ToJson] ObjectWriter::optional(writer : ObjectWriter, key : String, value : T?) -> Unit

#
ObjectWriter::required

fn[T : ToJson] ObjectWriter::required(writer : ObjectWriter, key : String, value : T) -> Unit

#
ObjectWriter::to_json

fn ObjectWriter::to_json(self : ObjectWriter) -> Json

#
Position

pub(all) struct Position {
line : Int
column : Int
}

impl Eq for Position
impl Debug for Position

#
from_json

fn[T : FromJson] from_json(json : Json, path? : JsonPath) -> T raise JsonDecodeError

#
inspect

#callsite(autofill(args_loc, loc))
fn inspect(object : &ToJson, content? : String, loc~ : SourceLoc, args_loc~ : ArgsLoc) -> Unit raise InspectError

#
null

let null : Json

#
parse

fn parse(input : StringView, max_nesting_depth? : Int) -> Json raise ParseError

Parse a JSON input string into a Json value, with an optional maximum nesting depth (default is 1024)

#
stringify

fn stringify(json : Json, escape_slash? : Bool, indent? : Int) -> String

#
to_json

fn[T : ToJson] to_json(value : T) -> Json

#
valid

fn valid(input : StringView) -> Bool