Code Monkey home page Code Monkey logo

ppx_deriving_json's Introduction

ppx_deriving_json

A ppx deriver plugin for converting OCaml values to and from JSON. Works both in native (using yojson JSON representation) and melange (using Js.Json.t JSON representation.

Installation

While the package is not yet available in the official opam repository, you either need to pin the package or add a custom opam repository:

opam repo add andreypopp https://github.com/andreypopp/opam-repository.git
opam update

Then you can install the package:

opam install -y ppx_deriving_json

Finally, add the dune configuration to your project:

(executable
 ...
 (preprocess (pps ppx_deriving_json.native)))

(library
 (modes melange)
 (preprocess (pps ppx_deriving_json.browser)))

Note that you need to use the ppx_deriving_json.native preprocessor for native and the ppx_deriving_json.browser preprocessor for melange.

Usage

To generate JSON converters for a type, add the [@@deriving json] attribute to a type declaration:

type t = {
  a: int;
  b: string;
} [@@deriving json]

This will generate the following pair of functions for native:

val of_json : Yojson.Basic.json -> t
val to_json : t -> Yojson.Basic.json

and the following pair of functions for melange:

val of_json : Js.Json.t -> t
val to_json : t -> Js.Json.t

Generating JSON converters from type expressions

You can also generate JSON converters for a type expression using the to_json and of_json extension points:

let json = [%to_json: int * string] (42, "foo")

Enumeration-like variants

Note that variants where all constructors have no arguments are treated as enumeration-like variants:

type t = A | B [@@deriving json]

Such variants are represented as strings in JSON:

let json = to_json A
(* json = `String "A" *)

[@json.default E]: default values for records

You can specify default values for record fields using the [@json.default E] attribute:

type t = {
  a: int;
  b: string [@json.default "-"];
} [@@deriving of_json]

let t = of_json (`Assoc ["a", `Int 42])
(* t = { a = 42; b = "-"; } *)

[@json.key "S"]: customizing keys for record fields

You can specify custom keys for record fields using the [@json.key E] attribute:

type t = {
  a: int [@json.key "A"];
  b: string [@json.key "B"];
} [@@deriving of_json]

let t = of_json (`Assoc ["A", `Int 42; "B", `String "foo"])
(* t = { a = 42; b = "foo"; } *)

[@json.as "S"]: customizing the representation of a variant case

You can specify custom representation for a variant case using the [@json.as E] attribute:

type t = A | B [@json.as "bbb"] [@@deriving json]

let json = to_json B
(* json = `String "bbb" *)

ppx_deriving_json's People

Contributors

andreypopp avatar zakybilfagih avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

zakybilfagih

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.