std.json.de

JSON deserialization

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 Error = String

type Deserializer i a = i -> Result Error { value : a, input : i }

Deserializer which extracts the data from the Value type

#[implicit]
type Deserialize a = { deserializer : ValueDeserializer a }

Values

#[implicit]
let functor : forall i . Functor (Deserializer i)

A Functor represents an action on a parameterized type which does not change the structure with the mapped type.

The following laws should hold:

  • map id == id
  • map (f << g) == map f << map g

#[implicit]
let applicative : forall i . Applicative (Deserializer i)

A Functor with application.

The following laws should hold:

  • wrap id <*> v = v
  • wrap (<<) <> u <> v <> w = u <> (v <*> w)
  • wrap f <*> wrap x = wrap (f x)
  • u <> wrap y = wrap (\g -> g x) <> u

#[implicit]
let alternative : forall i . Alternative (Deserializer i)

A monoid on applicative functors.

#[implicit]
let monad : forall i . Monad (Deserializer i)

A generalised interface for imperatively sequencing actions

Deserializes a Bool

let { ? } = import! std.effect
let { Value, bool, deserialize_with } = import! std.json.de
let { Result, ? } = import! std.result
let { assert_eq, ? } = import! std.test

seq assert_eq (deserialize_with bool "true") (Ok True)
assert_eq (deserialize_with bool "123") (Err "Expected bool")

Deserializes an Int

let { ? } = import! std.effect
let { Value, int, deserialize_with } = import! std.json.de
let { Result, ? } = import! std.result
let { assert_eq, ? } = import! std.test

seq assert_eq (deserialize_with int "123") (Ok 123)
assert_eq (deserialize_with int "true") (Err "Expected integer")

Deserializes a Float

Note that the deserializer will "integers" such as 123 as floats

let { ? } = import! std.effect
let { Value, float, deserialize_with } = import! std.json.de
let { Result, ? } = import! std.result
let { assert_eq, ? } = import! std.test

seq assert_eq (deserialize_with float "123.45") (Ok 123.45)
seq assert_eq (deserialize_with float "123") (Ok 123.0)
assert_eq (deserialize_with float "true") (Err "Expected float")

Deserializes a String

let { ? } = import! std.effect
let { Value, string, deserialize_with } = import! std.json.de
let { Result, ? } = import! std.result
let { assert_eq, ? } = import! std.test

seq assert_eq (deserialize_with string "\"abc\"") (Ok "abc")
assert_eq (deserialize_with string "true") (Err "Expected string")

let array a : forall a . ValueDeserializer a -> ValueDeserializer (Array a)

Deserializes an Array of a

let { ? } = import! std.effect
let { Value, array, int, deserialize_with } = import! std.json.de
let { Result, ? } = import! std.result
let { ? } = import! std.array
let { assert_eq, ? } = import! std.test

seq assert_eq (deserialize_with (array int) "[123, 456]") (Ok [123, 456])
assert_eq (deserialize_with (array int) "[123, \"\"]") (Err "Expected integer")

let field name a : forall a . String -> ValueDeserializer a -> ValueDeserializer a

Deserializes the field name of an object using a

let { ? } = import! std.effect
let { Value, field, int, deserialize_with } = import! std.json.de
let { Result, ? } = import! std.result
let { ? } = import! std.array
let { assert_eq, ? } = import! std.test

seq assert_eq (deserialize_with (field "test" int) "{ \"test\": 123 }") (Ok 123)
assert_eq (deserialize_with (field "test" int) "{ \"abc\": 123 }") (Err "Expected field `test`")

let map a : forall a . ValueDeserializer a -> ValueDeserializer (Map String a)

Deserializes the a Map String a

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

seq assert_eq
    (deserialize_with (map int) r#"{ "test": 123, "test2": 0 }"#)
    (Ok (singleton "test" 123 <> singleton "test2" 0))
assert_eq (deserialize_with (map int) r#"{ "abc": "" }"#) (Err "Expected integer")

let option a : forall a . ValueDeserializer a -> ValueDeserializer (Option a)

Deserializes an Option of a.

null maps to None and all other values to a

let { ? } = import! std.effect
let { Value, option, int, deserialize_with } = import! std.json.de
let { Result, ? } = import! std.result
let { ? } = import! std.array
let { assert_eq, ? } = import! std.test

seq assert_eq (deserialize_with (option int) "123") (Ok (Some 123))
seq assert_eq (deserialize_with (option int) "null") (Ok None)
assert_eq (deserialize_with (option int) "\"\"") (Err "Expected integer")

Deserializes a Value

let deserialize ?de input : forall a . [Deserialize a] -> String -> Result Error a

Runs the deserializer de on input Produces a value of type a if deserialization was successful

let deserialize_with de input : forall a . ValueDeserializer a -> String -> Result Error a

let run ?de value : forall a . [Deserialize a] -> Value -> Result Error a

let deserializer ?d : forall a . [Deserialize a] -> ValueDeserializer a

Deserializer which extracts the data from the Value type

#[implicit]
let int_deserializer : Deserialize Int

#[implicit]
let string_deserializer : Deserialize String

#[implicit]
let float_deserializer : Deserialize Float

#[implicit]
let option_deserializer : forall a . [Deserialize a] -> Deserialize (Option a)

#[implicit]
let list_deserializer : forall a . [Deserialize a] -> Deserialize (List a)

#[implicit]
let array_deserializer : forall a . [Deserialize a] -> Deserialize (Array a)

#[implicit]
let map_deserializer : forall a . [Deserialize a] -> Deserialize (Map String a)