jon.recoil.org

Module Misc.Json

JSON utilities

Simple (and not very robust) JSON generation utilities.

Everything is string based, creating lots of intermediate strings. Moreover, Json.string uses a custom string encoding based on the code for the format specifier %S, but with escapes for \u00HH instead of OCaml's \DDD.

val field : string -> string -> string

field name value creates a JSON field with the given name and value.

val string : string -> string

string value formats a string value as a JSON string.

val int : int -> string

int value formats an integer value as a JSON number.

val float : float -> string

float value formats a float value as a JSON number.

val object_ : string list -> string

object_ fields creates a JSON object from a list of field strings.

val array : string list -> string

array items creates a JSON array from a list of item strings.

val null : string

null is the JSON null literal.

val option : ('a -> string) -> 'a option -> string

option f opt formats an optional value as JSON. Returns the JSON null literal for None, or applies f to the value for Some v.