Skip to content

Gleam bindings for encoding decoding json

License

Notifications You must be signed in to change notification settings

midas-framework/gleam_json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gleam_json

A Gleam library for encoding decoding JSON.

Encoding

import gleam/json

let data = json.object([
  tuple("foo", json.string("bar")),
  tuple("values", json.list([json.int(1), json.int(2)])),
  tuple("nope", json.null())
])
json.encode(data)
// {"foo":"bar","nope":null,"values":[1,2]}

Decoding

Note: JSON is a dynamic data structure. The best way to manage decode JSON data is using gleam/dynamic.

import gleam/json

let encoded =
  "{\"foo\":\"bar\",\"nope\":null,\"values\":[1,2]}"

assert Ok(data) = json.decode(encoded)
let data = dynamic.from(data)

assert Ok(foo) = dynamic.field(data, "foo")
assert Ok("bar") = dynamic.string(foo)

assert Ok(values) = dynamic.field(data, "values")
assert Ok([1, 2]) = dynamic.typed_list(values, dynamic.int)

assert Ok(nope) = dynamic.field(data, "nope")
assert Ok(_) = dynamic.atom(nope)

Installation

This package can be installed by adding gleam_json to your rebar.config dependencies:

{deps, [
    gleam_json
]}.

About

Gleam bindings for encoding decoding json

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages