package ppx_yojson

  1. Overview
  2. Docs

Install

Dune Dependency

Authors

Maintainers

Sources

ppx_yojson-0.1.0.tbz
sha256=c1c4e07d81479fabfba79ef1816d06e48d93df5b613c4b036f9a464a2b9f826d
md5=39c9a36ad76a6d5ff657b8b290fb39c4

Description

PPX extension for Yojson literals and patterns

Published: 04 Oct 2018

README

ppx_yojson

PPX extension for Yojson literals and patterns

Overview

ppx_yojson lets you write Yojson expressions and patterns using ocaml syntax to make your code more concise and readable.

For example you can turn:

let json =
  `List
    [ `Assoc
        [ ("name", `String "Anne")
        ; ("grades", `List [`String "A"; `String "B-"; `String "B+"]
        ]
    ; `Assoc
        [ ("name", `String "Bernard")
        ; ("grades", `List [`String "B+"; `String "A"; `String "B-"]
        ]
    ]

into:

let json =
  [%yojson
    [ {name = "Anne"; grades = ["A"; "B-"; "B+"]}
    ; {name = "Anne"; grades = ["A"; "B-"; "B+"]}
    ]
  ]

Expressions

The expression rewriter supports the following Yojson values:

  • Null: [%yojson None]

  • Bool of bool: [%yojson true]

  • Float of float: [%yojson 1.2e+10]

  • Int of int: [%yojson 0xff]. As long as the int literal in the payload fits in an int, the 0x, 0o and 0b notations are accepted.

  • Intlit of string: [%yojson 100000000000000000000000000000000]. For arbitrary long integers, 0x, 0o and 0b notations are currently not supported and the rewriter will raise an error.

  • String of string: [%yojson "abc"]

  • List of json list: [%yojson [1; 2; 3]]. It supports mixed type list as well such as ["a"; 2].

  • Assoc of (string * json) list: [%yojson {a = 1; b = "b"}]

  • any valid combination of the above

The resulting expression are not constrained, meaning it works with Yojson.Safe or Yojson.Basic regardless.

Patterns

Currently unsupported, coming soon.

Dependencies (3)

  1. ppxlib >= "0.3.0" & < "0.9.0"
  2. ocaml >= "4.04.2"
  3. dune

Dev Dependencies (2)

  1. yojson with-test
  2. ounit with-test & >= "2.0.0"

Used by (2)

  1. openai
  2. quests

Conflicts

None

OCaml

Innovation. Community. Security.