jackson

Functions

pub fn array(entries: List(Json)) -> Json

build an array from a list of json values

pub fn bool(value: Bool) -> Json

build a json boolean value

pub fn decode(
  json: Json,
  decoder: fn(Dynamic) -> Result(a, List(DecodeError)),
) -> Result(a, List(DecodeError))

take a parsed json value from jackson.parse and decode it from a dynamic

pub fn dynamic_to_json(
  dyn: Dynamic,
) -> Result(Json, List(DecodeError))

take a dynamic value, created either manually or from the jackson.decode function, and turn it back into json. This is how the following values are converted:

  • Int -> json.Int(inner)
  • Float -> json.Float(inner)
  • String -> json.String(inner)
  • Bool -> json.Bool(inner)
  • Nil -> json.Null
  • List(#(String, Json)) -> json.Object(entries)
  • List(Json) -> json.Array(entries)

A slight problem is that gleam’s dynamic.tuple2 is unable to differentiate between a tuple and an array of length two. Additionally, if your json contains an array, whose children are all arrays of length two where the value at index 0 is a string, (such as [["a", 1], ["b", 2], ["c", 3], ["d", 4]]) then this function will re-encode it as though it were the following object:

{
  "a": 1,
  "b": 2,
  "c": 3,
  "d": 4
}
pub fn float(value: Float) -> Json

build a json float value

pub fn int(value: Int) -> Json

build a json number value

pub fn null() -> Json

build a json null value

pub fn object(entries: List(#(String, Json))) -> Json

build a json object from a list of keys and values, similar to dict.from_list

pub fn parse(in: String) -> Result(Json, String)

parse a string into json

pub fn resolve_pointer(
  json: Json,
  pointer: String,
) -> Result(Json, Nil)

resolve a json pointer, starting with a “/” or a “#/” note json pointers starting with a “#/” will be resolved with reference to the current json value, and not a remote resource

pub fn string(value: String) -> Json

build a json string value

pub fn to_string(json: Json) -> String

encode json to string (uses jackson.to_string_builder under the hood)

pub fn to_string_builder(json: Json) -> StringBuilder

encode json to a string builder

Search Document