gleam/json

Types

pub type DecodeError {
  UnexpectedEndOfInput
  UnexpectedByte(byte: String, position: Int)
  UnexpectedSequence(byte: String, position: Int)
  UnexpectedFormat(List(dynamic.DecodeError))
}

Constructors

  • UnexpectedEndOfInput
  • UnexpectedByte(byte: String, position: Int)
  • UnexpectedSequence(byte: String, position: Int)
  • UnexpectedFormat(List(dynamic.DecodeError))
pub type Json

Functions

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 decode(
  from json: String,
  using decoder: fn(Dynamic) -> Result(a, List(DecodeError)),
) -> Result(a, gleam/json.DecodeError)

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

Examples

> decode("[1,2,3]", dynamic.list(of: dynamic.int))
Ok([1, 2, 3])
> decode("[", dynamic.list(of: dynamic.int))
Error(UnexpectedEndOfInput)
> decode("1", dynamic.string)
Error(UnexpectedFormat([dynamic.DecodeError("String", "Int", [])]))
pub fn decode_bits(
  from json: BitArray,
  using decoder: fn(Dynamic) -> Result(a, List(DecodeError)),
) -> Result(a, gleam/json.DecodeError)

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

Examples

> decode_bits(<<"[1,2,3]">>, dynamic.list(of: dynamic.int))
Ok([1, 2, 3])
> decode_bits(<<"[">>, dynamic.list(of: dynamic.int))
Error(UnexpectedEndOfInput)
> decode_bits("<<1">>, dynamic.string)
Error(UnexpectedFormat([dynamic.DecodeError("String", "Int", [])]))
pub fn float(input: Float) -> Json

Encode an 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(a),
  of inner_type: fn(a) -> Json,
) -> Json

Encode an optional value into JSON, using null if it 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 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("Hello!")
"\"Hello!\""
pub fn to_string(json: Json) -> String

Convert a JSON value into a string.

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

Examples

> to_string(array([1, 2, 3], of: int))
"[1,2,3]"
pub fn to_string_builder(json: Json) -> StringBuilder

Convert a JSON value into a string builder.

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 StringBuilder data.

Examples

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