gleam/json
Types
pub type DecodeError {
UnexpectedEndOfInput
UnexpectedByte(String)
UnexpectedSequence(String)
UnexpectedFormat(List(dynamic.DecodeError))
UnableToDecode(List(decode.DecodeError))
}
Constructors
-
UnexpectedEndOfInput
-
UnexpectedByte(String)
-
UnexpectedSequence(String)
-
UnexpectedFormat(List(dynamic.DecodeError))
-
UnableToDecode(List(decode.DecodeError))
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)
The same as parse
, but using the old gleam/dynamic
decoder API.
pub fn decode_bits(
from json: BitArray,
using decoder: fn(Dynamic) -> Result(a, List(DecodeError)),
) -> Result(a, gleam/json.DecodeError)
The same as parse_bits
, but using the old gleam/dynamic
decoder API.
pub fn float(input: Float) -> Json
Encode a float into JSON.
Examples
> to_string(float(4.7))
"4.7"
pub fn nullable(
from input: 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: Decoder(a),
) -> Result(a, 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]", decode.list(of: decode.int))
Ok([1, 2, 3])
> decode("[", decode.list(of: decode.int))
Error(UnexpectedEndOfInput)
> decode("1", decode.string)
Error(UnableToDecode([decode.DecodeError("String", "Int", [])]))
pub fn parse_bits(
from json: BitArray,
using decoder: Decoder(a),
) -> Result(a, 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]">>, decode.list(of: decode.int))
Ok([1, 2, 3])
> decode_bits(<<"[">>, decode.list(of: decode.int))
Error(UnexpectedEndOfInput)
> decode_bits("<<1">>, decode.string)
Error(UnexpectedFormat([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_builder(json: Json) -> StringTree
Deprecated: Use `json.to_string_tree` instead.
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
StringTree
data.
Examples
> to_string_builder(array([1, 2, 3], of: int))
string_builder.from_string("[1,2,3]")
pub fn to_string_tree(json: Json) -> 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]")