GithubHelp home page GithubHelp logo

chazsconi / gleam_json Goto Github PK

View Code? Open in Web Editor NEW

This project forked from midas-framework/gleam_json

0.0 2.0 0.0 10 KB

Gleam bindings for encoding decoding json

License: Apache License 2.0

Erlang 100.00%

gleam_json's Introduction

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
]}.

gleam_json's People

Contributors

crowdhailer avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.