package awsm-codegen

  1. Overview
  2. Docs
type 'a t

A parser returning a value of type 'a.

It can act on a JSON value using the run function.

val run : 'a t -> Json.t -> ('a, string) Core.Result.t

Run the parser against a JSON value. The Error case contains information about where an error occured in the JSON document.

val run_exn : 'a t -> Json.t -> 'a
val parse_with : (Json.t -> ('a, string) Core.Result.t) -> 'a t

Low-level way to define a parser. Prefer combining other functions from this module, as it will return better error messages.

When returning an error, only include the error message: the location data will be added automatically by this module.

val (>>|) : 'a t -> ('a -> 'b) -> 'b t

Map over a parser.

val map_result : 'a t -> f:('a -> ('b, string) Core.result) -> 'b t

Map over a parser, with a function that can return an error.

val string : string t

Convert a string.

val int : int t

Convert an int. Fails if a number is not exactly an integer.

val float : float t
val int64 : int64 t

Convert an int64. Fails if a number is not exactly an int64.

val bool : bool t

Convert a bool.

val list : 'a t -> 'a list t

Convert a list of values.

val dict : 'a t -> (string * 'a) list t

Convert an homogeneous dictionary. It is an object where all values have the same type. For example, {"x": 1, "y": 2, "z": 3} can be parsed using dict int to [("x", 1); ("y", 2); ("z", 3)].

type 'a record

An abstract value representing values in a JSON record. To parse such a record, use record.

val return : 'a -> 'a record

Create a record that contains a static value.

module Let_syntax : sig ... end

Applicative interface for record. The ppx_let syntax extension uses this to translate let%map x1 = v1 and x2 = v2 in e.

val field : string -> 'a t -> 'a record

A required field. If not present, will return an error.

val field_opt : string -> 'a t -> 'a option record

An optional field. If not present, will return None.

val field_or : string -> 'a t -> default:'a -> 'a record

Like field_opt, but will return default if the field is not present.

val field_ignored : string -> unit record

Ignore this field. This completely ignores the presence, type and value of this field. It is usually better to parse the field with its correct type, and ignore it in the calling code.

val record : 'a record -> 'a t

Parse a JSON record. In addition to returning the correct data, it will also check that all fields in the record have been parsed explicitly.

val record_or_list_of : 'a record -> 'a list t

If the JSON value being parsed is a record, parse it (and return a singleton). If it is a list, parse a list of records.

val field_based : string -> (string -> 'a record option) -> 'a t

Support for type/value objects. field_based key get_parser will parse the current JSON value as an object, inspect the string value at the given key, and pass it to get_parser. The return value of this function determines how the rest of the record will be parsed.

val if_field_present : string -> then_:'a record -> else_:'a record -> 'a record

Parse using a different function depending on whether a key is present.

OCaml

Innovation. Community. Security.