Std.Json (Cure Standard Library v0.37.2)

View Source

Exact, typed JSON for Cure.

JSON numbers retain their lexical category and normalized exact spelling. Decoding a number into an application type is performed by that type's literal protocol implementation; the JSON layer never routes through Float.

Examples

use Std.Json

to_json(42)                            # => "42"
encode(Object([member("ok", Boolean(true))]))
decode("{\"ok\":true}")

Summary

Callbacks

Conversion is deliberately split at the structured Value boundary. Derivation constructs/consumes Values and therefore cannot accidentally double-encode a field or smuggle host JSON semantics into the result.

Functions

Parse a JSON string into a structured Value, rejecting invalid syntax or trailing tokens.

Parse a JSON string and decode it into any target type implementing FromJSON.

Render the structured tree entirely in Cure. Only principled character and numeric primitives used by the wider stdlib remain across the extern edge.

Construct an ObjectMember pairing a string key with a JSON Value.

Look up key in a list of object members, returning Error(UnexpectedValue(_)) when the key is missing.

Serialize any value implementing ToJSON directly to a JSON string.

Types

enum DecodeError

 enum DecodeError
   InvalidSyntax
   UnexpectedValue
   InvalidNumber

enum Number

 enum Number
   NaturalNumber
   IntegerNumber
   DecimalNumber

struct NumberScan

 struct NumberScan
   ?
   ?
   ?
   ?

struct ObjectMember

 struct ObjectMember
   ?
   ?

enum ParseResult

 enum ParseResult
   Parsed
   ParseFailure

enum Value

 enum Value
   Null
   Boolean
   Number
   String
   Array
   Object

Callbacks

interface FromJSON(t)

 interface FromJSON(t)

interface ToJSON(t)

 interface ToJSON(t)

Conversion is deliberately split at the structured Value boundary. Derivation constructs/consumes Values and therefore cannot accidentally double-encode a field or smuggle host JSON semantics into the result.

Functions

append_code_point(value: Int, rest: List(Char), reversed: List(Char)) -> ParseResult(Value)

 fn append_code_point(value: Int, rest: List(Char), reversed: List(Char)) -> ParseResult(Value)

decode(source: String) -> Result(Value, DecodeError)

 fn decode(source: String) -> Result(Value, DecodeError)

Parse a JSON string into a structured Value, rejecting invalid syntax or trailing tokens.

decode_as(source: String) -> Result(t, DecodeError)

 fn decode_as(source: String) -> Result(t, DecodeError)

Parse a JSON string and decode it into any target type implementing FromJSON.

digit_1_to_9(char: Char) -> Bool

 fn digit_1_to_9(char: Char) -> Bool

encode(value: Value) -> String

 fn encode(value: Value) -> String

Render the structured tree entirely in Cure. Only principled character and numeric primitives used by the wider stdlib remain across the extern edge.

escape(text: String) -> String

 fn escape(text: String) -> String

escape_reversed(text: List(Char), output: List(Char)) -> List(Char)

 fn escape_reversed(text: List(Char), output: List(Char)) -> List(Char)

finish_exponent_digits(source: List(Char), reversed: List(Char), negative: Bool) -> ParseResult(NumberScan)

 fn finish_exponent_digits(source: List(Char), reversed: List(Char), negative: Bool) -> ParseResult(NumberScan)

four_hex(source: List(Char)) -> ParseResult(Int)

 fn four_hex(source: List(Char)) -> ParseResult(Int)

from_json_value(value: Value) -> Result(t, DecodeError)

 fn from_json_value(value: Value) -> Result(t, DecodeError)

hex_digit(value: Int) -> Char

 fn hex_digit(value: Int) -> Char

hex_value(char: Char) -> Result(Int, DecodeError)

 fn hex_value(char: Char) -> Result(Int, DecodeError)

hex_values(chars: List(Char)) -> Result(List(Int), DecodeError)

 fn hex_values(chars: List(Char)) -> Result(List(Int), DecodeError)

is_digit(char: Char) -> Bool

 fn is_digit(char: Char) -> Bool

is_number_start(char: Char) -> Bool

 fn is_number_start(char: Char) -> Bool

literal_float_decimal(literal: DecimalLiteral) -> Result(Float, DecodeError)

 fn literal_float_decimal(literal: DecimalLiteral) -> Result(Float, DecodeError)

literal_float_integer(literal: IntegerLiteral) -> Result(Float, DecodeError)

 fn literal_float_integer(literal: IntegerLiteral) -> Result(Float, DecodeError)

literal_float_natural(literal: NaturalLiteral) -> Result(Float, DecodeError)

 fn literal_float_natural(literal: NaturalLiteral) -> Result(Float, DecodeError)

member(key: String, value: Value) -> ObjectMember

 fn member(key: String, value: Value) -> ObjectMember

Construct an ObjectMember pairing a string key with a JSON Value.

parse_array(source: List(Char), reversed: List(Value)) -> ParseResult(Value)

 fn parse_array(source: List(Char), reversed: List(Value)) -> ParseResult(Value)

parse_array_separator(source: List(Char), reversed: List(Value)) -> ParseResult(Value)

 fn parse_array_separator(source: List(Char), reversed: List(Value)) -> ParseResult(Value)

parse_array_value(source: List(Char), reversed: List(Value)) -> ParseResult(Value)

 fn parse_array_value(source: List(Char), reversed: List(Value)) -> ParseResult(Value)

parse_escape(source: List(Char), reversed: List(Char)) -> ParseResult(Value)

 fn parse_escape(source: List(Char), reversed: List(Char)) -> ParseResult(Value)

parse_keyword(source: List(Char), wanted: List(Char), value: Value) -> ParseResult(Value)

 fn parse_keyword(source: List(Char), wanted: List(Char), value: Value) -> ParseResult(Value)

parse_low_surrogate(high: Int, source: List(Char), reversed: List(Char)) -> ParseResult(Value)

 fn parse_low_surrogate(high: Int, source: List(Char), reversed: List(Char)) -> ParseResult(Value)

parse_member_colon(key: String, source: List(Char), reversed: List(ObjectMember)) -> ParseResult(Value)

 fn parse_member_colon(key: String, source: List(Char), reversed: List(ObjectMember)) -> ParseResult(Value)

parse_number(source: List(Char)) -> ParseResult(Value)

 fn parse_number(source: List(Char)) -> ParseResult(Value)

parse_object(source: List(Char), reversed: List(ObjectMember)) -> ParseResult(Value)

 fn parse_object(source: List(Char), reversed: List(ObjectMember)) -> ParseResult(Value)

parse_object_member(source: List(Char), reversed: List(ObjectMember)) -> ParseResult(Value)

 fn parse_object_member(source: List(Char), reversed: List(ObjectMember)) -> ParseResult(Value)

parse_object_separator(source: List(Char), reversed: List(ObjectMember)) -> ParseResult(Value)

 fn parse_object_separator(source: List(Char), reversed: List(ObjectMember)) -> ParseResult(Value)

parse_string(source: List(Char), reversed: List(Char)) -> ParseResult(Value)

 fn parse_string(source: List(Char), reversed: List(Char)) -> ParseResult(Value)

parse_unicode_escape(source: List(Char), reversed: List(Char)) -> ParseResult(Value)

 fn parse_unicode_escape(source: List(Char), reversed: List(Char)) -> ParseResult(Value)

parse_value(source: List(Char)) -> ParseResult(Value)

 fn parse_value(source: List(Char)) -> ParseResult(Value)

prepend_escape(char: Char, output: List(Char)) -> List(Char)

 fn prepend_escape(char: Char, output: List(Char)) -> List(Char)

prepend_unicode_control(point: Int, output: List(Char)) -> List(Char)

 fn prepend_unicode_control(point: Int, output: List(Char)) -> List(Char)

render_member(entry: ObjectMember) -> String

 fn render_member(entry: ObjectMember) -> String

render_members(members: List(ObjectMember)) -> String

 fn render_members(members: List(ObjectMember)) -> String

render_number(number: Number) -> String

 fn render_number(number: Number) -> String

render_value(value: Value) -> String

 fn render_value(value: Value) -> String

render_values(values: List(Value)) -> String

 fn render_values(values: List(Value)) -> String

required_member(key: String, members: List(ObjectMember)) -> Result(Value, DecodeError)

 fn required_member(key: String, members: List(ObjectMember)) -> Result(Value, DecodeError)

Look up key in a list of object members, returning Error(UnexpectedValue(_)) when the key is missing.

scan_digits(source: List(Char), reversed: List(Char)) -> Tuple(List(Char), List(Char))

 fn scan_digits(source: List(Char), reversed: List(Char)) -> Tuple(List(Char), List(Char))

scan_exponent(source: List(Char), reversed: List(Char), negative: Bool) -> ParseResult(NumberScan)

 fn scan_exponent(source: List(Char), reversed: List(Char), negative: Bool) -> ParseResult(NumberScan)

scan_exponent_digits(source: List(Char), reversed: List(Char), negative: Bool) -> ParseResult(NumberScan)

 fn scan_exponent_digits(source: List(Char), reversed: List(Char), negative: Bool) -> ParseResult(NumberScan)

scan_fraction_or_exponent(source: List(Char), reversed: List(Char), negative: Bool, decimal: Bool) -> ParseResult(NumberScan)

 fn scan_fraction_or_exponent(source: List(Char), reversed: List(Char), negative: Bool, decimal: Bool) -> ParseResult(NumberScan)

scan_integer_part(source: List(Char), reversed: List(Char), negative: Bool) -> ParseResult(NumberScan)

 fn scan_integer_part(source: List(Char), reversed: List(Char), negative: Bool) -> ParseResult(NumberScan)

scan_number(source: List(Char)) -> ParseResult(NumberScan)

 fn scan_number(source: List(Char)) -> ParseResult(NumberScan)

skip_space(source: List(Char)) -> List(Char)

 fn skip_space(source: List(Char)) -> List(Char)

to_json(value: t) -> String

 fn to_json(value: t) -> String

Serialize any value implementing ToJSON directly to a JSON string.

to_json_value(value: t) -> Value

 fn to_json_value(value: t) -> Value