std.json.ser

JSON serialization

This module is only available if gluon is compiled with the serialization feature.

Types

type Value = 
    | Null
    | Bool Bool
    | Int Int
    | Float Float
    | String String
    | Array (Array Value)
    | Object (Map String Value)

type ValueSerializer a = { serialize : a -> Result Error Value }

#[implicit]
type Serialize a = ValueSerializer a

Values

let serialize ?ser : forall a . [Serialize a] -> a -> Result Error Value

let to_string v : forall a . [Serialize a] -> a -> Result Error String

Serializes a to a JSON without whitespace string

let { ? } = import! std.effect
let { Value, to_string, ? } = import! std.json.ser
let { Result, ? } = import! std.result
let { singleton, ? } = import! std.map
let { (<>) } = import! std.semigroup
let { assert_eq, ? } = import! std.test

seq assert_eq (to_string "string") (Ok r#""string""#)

let expected = r#"[1,2,3]"#
seq assert_eq (to_string [1, 2, 3]) (Ok expected)

let expected = r#"{"field":1}"#
assert_eq (to_string (singleton "field" 1)) (Ok expected)

let to_string_pretty v : forall a . [Serialize a] -> a -> Result Error String

Serializes a to a JSON string

let { ? } = import! std.effect
let { Value, to_string_pretty, ? } = import! std.json.ser
let { Result, ? } = import! std.result
let { singleton, ? } = import! std.map
let { (<>) } = import! std.semigroup
let { assert_eq, ? } = import! std.test

seq assert_eq (to_string_pretty "string") (Ok r#""string""#)

let expected = r#"[
  1,
  2,
  3
]"#
seq assert_eq (to_string_pretty [1, 2, 3]) (Ok expected)

let expected = r#"{
  "field": 1
}"#
assert_eq (to_string_pretty (singleton "field" 1)) (Ok expected)

#[implicit]
let serialize_unit : Serialize ()

#[implicit]
let serialize_int : Serialize Int

#[implicit]
let serialize_bool : Serialize Bool

#[implicit]
let serialize_float : Serialize Float

#[implicit]
let serialize_string : Serialize String

#[implicit]
let serialize_option : forall a . [Serialize a] -> Serialize (Option a)

#[implicit]
let serialize_array : forall a . [Serialize a] -> Serialize (Array a)

#[implicit]
let serialize_map : forall a . [Serialize a] -> Serialize (Map String a)

#[implicit]
let serialize_value : Serialize Value