package query-json
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=045832458e20ae90f7bb14536e27aa5e4c5528968ac48ac390409a66fe6b438e
sha512=9d4af4a9c8f5b2f4fb84830f0651ba4400f32e97bdec1e61378780906077af80d5679f9ccf545a072c1ffbe70a18ca4b3dd57b79d02384384068ee695d67e4ac
doc/query-json.core/Json/index.html
Module JsonSource
include module type of struct include Yojson.Safe end
Type of the JSON tree
type t = [ | `Null| `Bool of bool| `Int of int| `Intlit of string| `Float of float| `String of string| `Assoc of (string * t) list| `List of t list
]All possible cases defined in Yojson:
- `Null: JSON null
- `Bool of bool: JSON boolean
- `Int of int: JSON number without decimal point or exponent.
- `Intlit of string: JSON number without decimal point or exponent, preserved as a string.
- `Float of float: JSON number, Infinity, -Infinity or NaN.
- `Floatlit of string: JSON number, Infinity, -Infinity or NaN, preserved as a string.
- `String of string: JSON string. Bytes in the range 128-255 are preserved as-is without encoding validation for both reading and writing.
- `Stringlit of string: JSON string literal including the double quotes.
- `Assoc of (string * json) list: JSON object.
- `List of json list: JSON array.
Pretty printer, useful for debugging
equal a b is the monomorphic equality. Determines whether two JSON values are considered equal. In the case of JSON objects, the order of the keys does not matter, except for duplicate keys which will be considered equal as long as they are in the same input order.
Long integers are converted to JSON strings.
Examples:
`Intlit "12345678901234567890" -> `String "12345678901234567890"
JSON writers
val to_channel :
?buf:Buffer.t ->
?len:int ->
?suf:string ->
?std:bool ->
out_channel ->
t ->
unitWrite a compact JSON value to a channel. Note: the out_channel is not flushed by this function.
See to_string for the role of the optional arguments and raised exceptions.
val to_output :
?buf:Buffer.t ->
?len:int ->
?suf:string ->
?std:bool ->
< output : string -> int -> int -> int.. > ->
t ->
unitWrite a compact JSON value to an OO channel.
See to_string for the role of the optional arguments and raised exceptions.
Write a compact JSON value to a file. See to_string for the role of the optional arguments and raised exceptions.
Write a compact JSON value to an existing buffer. See to_string for the role of the optional argument and raised exceptions.
val seq_to_string :
?buf:Buffer.t ->
?len:int ->
?suf:string ->
?std:bool ->
t Seq.t ->
stringWrite a sequence of suf-suffixed compact one-line JSON values to a string.
val seq_to_channel :
?buf:Buffer.t ->
?len:int ->
?suf:string ->
?std:bool ->
out_channel ->
t Seq.t ->
unitWrite a sequence of suf-suffixed compact one-line JSON values to a channel.
Write a sequence of suf-suffixed compact one-line JSON values to a file.
Write a sequence of suf-suffixed compact one-line JSON values to an existing buffer.
Write the given JSON value to the given buffer. Provided as a writer function for atdgen.
Miscellaneous
Sort object fields (stable sort, comparing field names and treating them as byte sequences)
JSON pretty-printing
Pretty-print into a Format.formatter. See to_string for the role of the optional std argument.
Pretty-print into a string. See to_string for the role of the optional std argument. See pretty_print for raised exceptions.
Pretty-print to a channel. See to_string for the role of the optional std argument. See pretty_print for raised exceptions.
Combined parser and pretty-printer. See to_string for the role of the optional std argument and raised exceptions.
Combined parser and printer. See to_string for the role of the optional std argument and raised exceptions.
JSON readers
Exception describing a failure in both finalizer and parsing.
Read a JSON value from a string.
Read a JSON value from a channel. See from_string for the meaning of the optional arguments and raised exceptions.
Read a JSON value from a file. See from_string for the meaning of the optional arguments and raised exceptions.
type lexer_state = {buf : Buffer.t;mutable lnum : int;mutable bol : int;mutable fname : string option;
}This alias is provided for backward compatibility. New code should refer to Yojson.lexer_state directly.
This alias is provided for backward compatibility. New code should use Yojson.init_lexer directly.
Read a JSON value from a lexbuf. A valid initial lexer_state can be created with init_lexer. See from_string for the meaning of the optional arguments and raised exceptions.
Input a sequence of JSON values from a string. Whitespace between JSON values is fine but not required. See from_string for the meaning of the optional arguments and raised exceptions.
val seq_from_channel :
?buf:Buffer.t ->
?fin:(unit -> unit) ->
?fname:string ->
?lnum:int ->
in_channel ->
t Seq.tInput a sequence of JSON values from a channel. Whitespace between JSON values is fine but not required.
See from_string for the meaning of the other optional arguments and other raised exceptions.
Input a sequence of JSON values from a file. Whitespace between JSON values is fine but not required.
See from_string for the meaning of the optional arguments and raised exceptions.
Input a sequence of JSON values from a lexbuf. A valid initial lexer_state can be created with init_lexer. Whitespace between JSON values is fine but not required.
See seq_from_channel for the meaning of the optional fin argument and other raised exceptions.
The type of values resulting from a parsing attempt of a JSON value.
val lineseq_from_channel :
?buf:Buffer.t ->
?fin:(unit -> unit) ->
?fname:string ->
?lnum:int ->
in_channel ->
json_line Seq.tInput a sequence of JSON values, one per line, from a channel. Exceptions raised when reading malformed lines are caught and represented using `Exn.
See seq_from_channel for the meaning of the optional fin argument. See from_string for the meaning of the other optional arguments and raised exceptions.
val lineseq_from_file :
?buf:Buffer.t ->
?fname:string ->
?lnum:int ->
string ->
json_line Seq.tInput a sequence of JSON values, one per line, from a file. Exceptions raised when reading malformed lines are caught and represented using `Exn.
See seq_from_channel for the meaning of the optional fin argument. See from_string for the meaning of the other optional arguments and raised exceptions.
Read a JSON value from the given lexer_state and lexing buffer and return it. Provided as a reader function for atdgen.
This module provides combinators for extracting fields from JSON values.
include module type of struct include Yojson.Safe.Util end
Raised when the JSON value is not of the correct type to support an operation, e.g. member on an `Int. The string message explains the mismatch.
Raised when the equivalent JavaScript operation on the JSON value would return undefined. Currently this only happens when an array index is out of bounds.
Returns all the key names in the given JSON object.
Return all the value in the given JSON object.
Combine two JSON objects together.
member k obj returns the value associated with the key k in the JSON object obj, or `Null if k is not present in obj.
index i arr returns the value at index i in the JSON array arr. Negative indices count from the end of the list (so -1 is the last element).
map f arr calls the function f on each element of the JSON array arr, and returns a JSON array containing the results.
Extract the items of a JSON object.
Return None if the JSON value is null or map the JSON value to Some value using the provided function.
Extract a boolean value.
Extract Some boolean value, return None if the value is null.
Extract a number.
Extract Some number, return None if the value is null.
Extract a float value. to_number is generally preferred as it also works with int literals.
Extract Some float value, return None if the value is null. to_number_option is generally preferred as it also works with int literals.
Extract an int from a JSON int.
Extract Some int from a JSON int, return None if the value is null.
Extract a list from JSON array.
Extract Some string from a JSON string, return None if the value is null.
The conversion functions above cannot be used with map, because they do not return JSON values. This convenience function convert_each to_f arr is equivalent to List.map to_f (to_list arr).
Exception-free filters
The following functions operate on lists of JSON nodes. None of them raises an exception when a certain kind of node is expected but no node or the wrong kind of node is found. Instead of raising an exception, nodes that are not as expected are simply ignored.
filter_map f l maps each element of the list l to an optional value using function f and unwraps the resulting values.
Expects JSON arrays and returns all their elements as a single list. flatten l is equivalent to List.flatten (filter_list l).
Expects JSON arrays and returns all their elements existing at the given position.
Expects JSON arrays and unwraps them.
Expects JSON objects and returns all the fields of the given name (at most one field per object).
Expects JSON objects and unwraps them.
Expects JSON booleans and unwraps them.
Expects JSON integers (`Int nodes) and unwraps them.
Expects JSON floats (`Float nodes) and unwraps them.
Expects JSON numbers (`Int or `Float) and unwraps them. Ints are converted to floats.
Expects JSON strings and unwraps them.