package jsonxt

  1. Overview
  2. Docs

Yojson compatibility module

To use Jsonxt's Yojson compatibility module create a yojson.ml file in the projects source directory with the following contents:

include Jsonxt.Yojson

Note that compatibility is mostly a thin layer on top of Jsonxt. In particular the error reporting by the utils module uses the Failure exception rather than Yojson's specialist exceptions

JSON compatibility differences

The underlying parser used by the Yojson compatibility modules are RFC 8259 compliant except for specific extensions. In particular:

  • Control characters must be escaped as defined by RFC 8259
  • Comment are supported with /* */ and // at the end of the line
  • Variants and tuples are supported but with syntax changes noted later
  • Object keys must be quoted. So {ab:10} is not valid and must be encoded as {"ab":10}
  • Variant names must be quoted. eg. <"ab"> and <"ab":10>

Behavioural differences

  • The lexer_state data structure is used to report errors but not updated during the parsing of the input
  • The optional buf parameter is ignored
  • Error messages are likely to be different
  • The utils module module uses the Failure exception rather than Yojson's specialist exceptions
exception Json_error of string
type lexer_state = {
  1. buf : Buffer.t;
  2. mutable lnum : int;
  3. mutable bol : int;
  4. mutable fname : string option;
}
val init_lexer : ?buf:Buffer.t -> ?fname:string -> ?lnum:int -> unit -> lexer_state
module Basic : sig ... end
module Safe : sig ... end
module Raw : sig ... end