gleam/jsone

Types

DuplicateMapKeys

The option for determining whether to return the first or last key/value when there exist duplicates.

pub type DuplicateMapKeys {
  First
  Last
}

Constructors

  • First
  • Last

JsonNumber

A JSON number can be either an Int or a Float.

pub type JsonNumber {
  JsonInt(Int)
  JsonFloat(Float)
}

Constructors

  • JsonInt(Int)
  • JsonFloat(Float)

JsonValue

Represents a JSON value.

pub type JsonValue {
  JsonString(String)
  JsonNumber(JsonNumber)
  JsonArray(List(JsonValue))
  JsonBool(Bool)
  JsonNull
  JsonObject(List(tuple(String, JsonValue)))
}

Constructors

  • JsonString(String)
  • JsonNumber(JsonNumber)
  • JsonArray(List(JsonValue))
  • JsonBool(Bool)
  • JsonNull
  • JsonObject(List(tuple(String, JsonValue)))

Options

The available decoding options. The descriptions below are lifted or adapted from the jsone docs.

allow_ctrl_chars

If the value is True, strings which contain unescaped control characters will be regarded as a legal JSON string.

reject_invalid_utf8

Rejects JSON strings which contain invalid UTF-8 byte sequences.

duplicate_map_keys

IETF RFC 4627 says that keys SHOULD be unique, but they don't have to be. Most JSON parsers will either give you the value of the first, or last duplicate property encountered.

If the value of this option is First, the first duplicate key/value is returned. If it is Last, the last is instead.

pub type Options {
  Options(
    allow_ctrl_chars: Bool,
    reject_invalid_utf8: Bool,
    duplicate_map_keys: DuplicateMapKeys,
  )
}

Constructors

  • Options( allow_ctrl_chars: Bool, reject_invalid_utf8: Bool, duplicate_map_keys: DuplicateMapKeys, )

Functions

array

pub fn array(
  list: List(a),
  encoder: fn(a) -> JsonValue,
) -> JsonValue

Create an array of JSON values.

bool

pub fn bool(bool: Bool) -> JsonValue

Create a JSON boolean value.

decode

pub fn decode(json: String) -> Result(Dynamic, String)

decode_with_options

pub fn decode_with_options(
  json: String,
  options: Options,
) -> Result(Dynamic, String)

default_options

pub fn default_options() -> Options

The default options used by the decode function.

encode

pub fn encode(json_value: JsonValue) -> Result(Dynamic, String)

Encode a JSON value as a UTF-8 binary.

float

pub fn float(float: Float) -> JsonValue

Create a JSON float.

int

pub fn int(int: Int) -> JsonValue

Create a JSON int.

null

pub fn null() -> JsonValue

Create a JSON null value.

object

pub fn object(
  object: List(tuple(String, JsonValue)),
) -> JsonValue

Create a JSON object.

string

pub fn string(string: String) -> JsonValue

Create a JSON string.