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: