gleam/jsone

Types

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

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

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

Constructors

  • JsonInt(Int)
  • JsonFloat(Float)

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)))

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

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

Create an array of JSON values.

pub fn bool(bool: Bool) -> JsonValue

Create a JSON boolean value.

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

The default options used by the decode function. They are:

Options(
  allow_ctrl_chars: False,
  reject_invalid_utf8: False,
  duplicate_map_keys: First,
)
pub fn encode(json_value: JsonValue) -> Result(Dynamic, String)

Encode a JSON value as a UTF-8 binary.

pub fn float(float: Float) -> JsonValue

Create a JSON float.

pub fn int(int: Int) -> JsonValue

Create a JSON int.

pub fn null() -> JsonValue

Create a JSON null value.

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

Create a JSON object.

pub fn string(string: String) -> JsonValue

Create a JSON string.

pub fn to_dynamic(json_value: JsonValue) -> Dynamic