Skip to content

state-machine-systems/JsonCodec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JsonCodec

JSON combinator library for BuckleScript/Reason

Using the magic of pickler combinators, this library allows you to parse and serialize JSON structures in a declarative way, free of boilerplate.

Here's a little example:

let json = {js|
{
    "name": "Great Pyramid of Giza",
    "lat": 29.979175,
    "lon": 31.134358,
    "height": 146.5
}
|js};

/* Define a codec for the above object type */
let codec =
  JsonCodec.(
    object4(
      field("name", string),
      field("lat", number),
      field("lon", number),
      field("height", number),
    )
  );

/* Decoding */
switch (JsonCodec.decodeJson(codec, json)) {
| Belt.Result.Ok((name, lat, lon, height)) =>
  Printf.printf("name='%s' location=%f,%f height=%f\n", name, lat, lon, height)
| Belt.Result.Error(error) => Printf.printf("Decoding error: %s", error)
};

/* Encoding */
let encoded =
  JsonCodec.encodeJson(codec, ("Machu Picchu", -13.163333, -72.545556, 2430.0));

Printf.printf("JSON: %s\n", encoded);

© 2017-2018 State Machine Systems Ltd. Apache Licence, Version 2.0

About

JSON combinator library for BuckleScript/Reason

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 5

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy