gleam/json

Types

pub type DecodeError {
  UnexpectedEndOfInput
  UnexpectedByte(String)
  UnexpectedSequence(String)
  UnableToDecode(List(decode.DecodeError))
}

Constructors

  • UnexpectedEndOfInput
  • UnexpectedByte(String)
  • UnexpectedSequence(String)
  • UnableToDecode(List(decode.DecodeError))
pub type Json

Values

pub fn array(
  from entries: List(a),
  of inner_type: fn(a) -> Json,
) -> Json

Encode a list into a JSON array.

Examples

> to_string(array([1, 2, 3], of: int))
"[1, 2, 3]"
pub fn bool(input: Bool) -> Json

Encode a bool into JSON.

Examples

> to_string(bool(False))
"false"
pub fn dict(
  dict: dict.Dict(k, v),
  keys: fn(k) -> String,
  values: fn(v) -> Json,
) -> Json

Encode a Dict into a JSON object using the supplied functions to encode the keys and the values respectively.

Examples

> to_string(dict(dict.from_list([ #(3, 3.0), #(4, 4.0)]), int.to_string, float)
"{\"3\": 3.0, \"4\": 4.0}"
pub fn float(input: Float) -> Json

Encode a float into JSON.

Examples

> to_string(float(4.7))
"4.7"
pub fn int(input: Int) -> Json

Encode an int into JSON.

Examples

> to_string(int(50))
"50"
pub fn null() -> Json

The JSON value null.

Examples

> to_string(null())
"null"
pub fn nullable(
  from input: option.Option(a),
  of inner_type: fn(a) -> Json,
) -> Json

Encode an optional value into JSON, using null if it is the None variant.

Examples

> to_string(nullable(Some(50), of: int))
"50"
> to_string(nullable(None, of: int))
"null"
pub fn object(entries: List(#(String, Json))) -> Json

Encode a list of key-value pairs into a JSON object.

Examples

> to_string(object([
  #("game", string("Pac-Man")),
  #("score", int(3333360)),
]))
"{\"game\":\"Pac-Mac\",\"score\":3333360}"
pub fn parse(
  from json: String,
  using decoder: decode.Decoder(t),
) -> Result(t, DecodeError)

Decode a JSON string into dynamically typed data which can be decoded into typed data with the gleam/dynamic module.

Examples

> parse("[1,2,3]", decode.list(of: decode.int))
Ok([1, 2, 3])
> parse("[", decode.list(of: decode.int))
Error(UnexpectedEndOfInput)
> parse("1", decode.string)
Error(UnableToDecode([decode.DecodeError("String", "Int", [])]))
pub fn parse_bits(
  from json: BitArray,
  using decoder: decode.Decoder(t),
) -> Result(t, DecodeError)

Decode a JSON bit string into dynamically typed data which can be decoded into typed data with the gleam/dynamic module.

Examples

> parse_bits(<<"[1,2,3]">>, decode.list(of: decode.int))
Ok([1, 2, 3])
> parse_bits(<<"[">>, decode.list(of: decode.int))
Error(UnexpectedEndOfInput)
> parse_bits(<<"1">>, decode.string)
Error(UnableToDecode([decode.DecodeError("String", "Int", [])])),
pub fn preprocessed_array(from: List(Json)) -> Json

Encode a list of JSON values into a JSON array.

Examples

> to_string(preprocessed_array([int(1), float(2.0), string("3")]))
"[1, 2.0, \"3\"]"
pub fn string(input: String) -> Json

Encode a string into JSON, using normal JSON escaping.

Examples

> to_string(string("Hello!"))
"\"Hello!\""
pub fn to_string(json: Json) -> String

Convert a JSON value into a string.

Where possible prefer the to_string_tree function as it is faster than this function, and BEAM VM IO is optimised for sending StringTree data.

Examples

> to_string(array([1, 2, 3], of: int))
"[1,2,3]"
pub fn to_string_tree(json: Json) -> string_tree.StringTree

Convert a JSON value into a string tree.

Where possible prefer this function to the to_string function as it is slower than this function, and BEAM VM IO is optimised for sending StringTree data.

Examples

> to_string_tree(array([1, 2, 3], of: int))
string_tree.from_string("[1,2,3]")
Search Document