Json
Edit on GitHubJSON (JavaScript Object Notation) parsing, printing, and access utilities.
1 | print( |
Types
Type declarations included in the Json module.
Json.Json
1 | enum Json { |
Data structure representing JSON in Grain.
Variants:
Represents the JSON null
value.
Represents a JSON boolean value.
Represents a JSON number value.
Represents a JSON string value.
Represents a JSON array value.
Represents a JSON object value, as a list of (key, value).
Examples:
1 | assert Json.parse("{\"currency\":\"€\",\"price\":99.99}") == JsonObject([ |
1 | assert Json.parse("{\n\"currency\":\"€\",\n\"price\":99.99\n}") == JsonObject([ |
Json.JsonToStringError
Represents errors for cases where a Json
data structure cannot be represented as a
JSON string.
Variants:
The Json
data structure contains a number value of NaN
, Infinity
, or -Infinity
.
Json.IndentationFormat
Controls how indentation is output in custom formatting.
Variants:
No indentation is emitted.
Tabs are emitted.
The desired number of spaces are emitted.
IndentWithSpaces(2)
IndentWithSpaces(4)
Json.ArrayFormat
Controls how arrays are output in custom formatting.
Variants:
Arrays are emitted in a compact manner.
Arrays are emitted with spaces between elements.
Arrays are emitted with newlines and indentation between each element.
Json.ObjectFormat
Controls how objects are output in custom formatting.
Variants:
Objects are emitted in a compact manner.
Objects are emitted with spaces between entries.
Objects are emitted with each entry on a new line.
Json.LineEnding
Controls how line endings are output in custom formatting.
Variants:
No line endings will be emitted.
A \n
will be emitted at the end of each line.
A \r\n
will be emitted at the end of each line.
A \r
will be emitted at the end of each line.
Json.FormattingChoices
Allows control of formatting in JSON output.
Variants:
Recommended human readable formatting.
Escapes all control points for the sake of clarity, but outputs unicode codepoints directly so the result needs to be treated as proper unicode and is not safe to be transported in ASCII encoding.
Roughly Equivalent to:
Compact formatting that minimizes the size of resulting JSON at cost of not being easily human readable.
Only performs minimal string escaping as required by the ECMA-404 standard, so the result needs to be treated as proper unicode and is not safe to be transported in ASCII encoding.
Roughly Equivalent to:
Pretty and conservative formatting to maximize compatibility and embeddability of the resulting JSON.
Should be safe to copy and paste directly into HTML and to be transported in plain ASCII.
Roughly Equivalent to:
Compact and conservative formatting to maximize compatibility and embeddability of the resulting JSON.
Should be safe to copy and paste directly into HTML and to transported in plain ASCII.
Roughly Equivalent to:
Allows for fined grained control of the formatting output.
Json.JsonParseError
1 | enum JsonParseError { |
Represents errors for JSON parsing along with a human readable message.
Values
Functions and constants included in the Json module.
Json.toString
Added in 0.6.0
No other changes yet.
Converts the Json
data structure into a JSON string with specific formatting settings.
Parameters:
param | type | description |
---|---|---|
?format |
FormattingChoices |
Formatting options |
json |
Json |
The Json data structure to convert |
Returns:
type | description |
---|---|
Result<String, JsonToStringError> |
Ok(str) containing the JSON string or Err(err) if the provided Json data structure cannot be converted to a string |
Examples:
1 | assert toString( |
1 | assert toString( |
1 | assert toString( |
Json.parse
Added in 0.6.0
No other changes yet.
Parses JSON string into a Json
data structure.
Parameters:
param | type | description |
---|---|---|
str |
String |
The JSON string to parse |
Returns:
type | description |
---|---|
Result<Json, JsonParseError> |
Ok(json) containing the parsed data structure on a successful parse or Err(err) containing a parse error otherwise |
Examples:
1 | assert parse("{\"currency\":\"$\",\"price\":119}") == Ok( |
Json.Lenses
Utilities for accessing and updating JSON data.
Added in 0.7.0
No other changes yet.
1 | let obj = JsonObject([("x", JsonNumber(123))]) |
1 | let obj = JsonObject([("x", JsonNumber(123))]) |
Types
Type declarations included in the Json.Lenses module.
Json.Lenses.Lens
Added in 0.7.0
No other changes yet.
A structure which provides functionality for accessing and setting JSON data.
Fields:
name | type | description |
---|---|---|
get |
(subject: a0) => Option<b0> |
A function which reads a value from the subject. |
set |
(newValue: b0, subject: a0) => Option<a0> |
A function which immutably updates a value in the subject. |
Values
Functions and constants included in the Json.Lenses module.
Json.Lenses.get
Reads the value focused on by the given lens from the input data.
Parameters:
param | type | description |
---|---|---|
lens |
Lens<a, b> |
The lens to apply to the subject data |
subject |
a |
The data which will have the lens applied to it |
Returns:
type | description |
---|---|
Option<b> |
Some(data) containing the data read by the lens if the lens matches the given data, or None if the data cannot be matched to the lens |
Examples:
Json.Lenses.set
Sets the value focused on by the given lens from the input data to the desired new value.
Parameters:
param | type | description |
---|---|---|
lens |
Lens<a, b> |
The lens to apply to the subject data |
newValue |
b |
The new value to set at the focus of the lens |
subject |
a |
The data which will have the lens applied to it |
Returns:
type | description |
---|---|
Option<a> |
Some(data) containing the new data after the lens substitution if the lens matches the given data, or None if the data cannot be matched to the lens |
Examples:
1 | assert set(property("a"), JsonNumber(123), JsonObject([("a", JsonNull)])) == Some(JsonObject([("a", JsonNumber(123))])) |
Json.Lenses.map
Updates the value focused on by the given lens from the input data by applying a function to it and setting the focus to the result of the function
Parameters:
param | type | description |
---|---|---|
lens |
Lens<a, b> |
The lens to apply to the subject data |
fn |
b => b |
The function to apply to the matched data at the lens if matched |
subject |
a |
The data which will have the lens applied to it |
Returns:
type | description |
---|---|
Option<a> |
Some(data) containing the new data after the lens mapping has been applied if the lens matches the given data, or None if the data cannot be matched to the lens |
Examples:
1 | assert map(property("x"), x => JsonArray([x, x]), JsonObject([("x", JsonNumber(1))])) == |
Json.Lenses.json
Added in 0.7.0
No other changes yet.
A lens whose focus is a JSON value.
Examples:
Json.Lenses.boolean
Added in 0.7.0
No other changes yet.
A lens whose focus is a JSON boolean value.
Examples:
Json.Lenses.string
Added in 0.7.0
No other changes yet.
A lens whose focus is a JSON string value.
Examples:
Json.Lenses.number
Added in 0.7.0
No other changes yet.
A lens whose focus is a JSON number value.
Examples:
Json.Lenses.array
Added in 0.7.0
No other changes yet.
A lens whose focus is a JSON array.
Examples:
Json.Lenses.objectProperties
Added in 0.7.0
No other changes yet.
A lens whose focus is the property pair list of a JSON object.
Examples:
1 | assert get(objectProperties, JsonObject([("a", JsonNumber(123))])) == Some([("a", JsonNumber(123))]) |
Json.Lenses.property
Added in 0.7.0
No other changes yet.
Creates a lens whose focus is a given property of a JSON object.
Parameters:
param | type | description |
---|---|---|
propertyName |
String |
The property name of the JSON object to focus on |
Returns:
type | description |
---|---|
Lens<Json, Json> |
A lens whose focus is the given property of a JSON object |
Examples:
1 | assert set(property("x"), JsonString("new"), JsonObject([("x", JsonNumber(123))])) == |
Json.Lenses.nullable
Added in 0.7.0
No other changes yet.
Wraps a lens to permit nullable values in addition to the original value
type of the given lens. During a get
operation if the lens matches then
the result will be enclosed in Some
; if the lens does not match but the
value focused is null, then the lens will still successfully match and
None
will be returned.
Examples:
Json.Lenses.propertyPath
Added in 0.7.0
No other changes yet.
Creates a lens whose focus is a given property path within a JSON object tree.
Parameters:
param | type | description |
---|---|---|
propertyNames |
List<String> |
The property path of the JSON object to create a focus on |
Returns:
type | description |
---|---|
Lens<Json, Json> |
A lens whose focus is the given property path of a JSON object |
Examples:
1 | let nestedObj = JsonObject([("a", JsonObject([("b", JsonNumber(123))]))]) |
Json.Lenses.(||>)
Added in 0.7.0
No other changes yet.
Reverse lens composition.
Parameters:
param | type | description |
---|---|---|
lens1 |
Lens<a, b> |
The lens which will be applied first |
lens2 |
Lens<b, c> |
The lens which will be applied second |
Returns:
type | description |
---|---|
Lens<a, c> |
A lens which combines the two given lenses, passing through the first and then the second |
Examples: