A library to parse JSON strings into a structure you can interpret in Uiua. Also includes utility functions to build JSON structures, stringify and prettyfy them.
The implementation is fairly rigorously tested and ready for general use. If you want to build microservices in Uiua, this should prove to be indispensible.
Because Uiua does not have primitives for true
, false
and null
, all primitives are wrapped in a box array with a type and a literal value. For true
, the literal value is 1
, for false
and null
, the literal value is 0
. See examples for how to check for JSON value type.
Note: Since 0.12.0 Uiua includes a native json
function for encoding/decoding JSON. This library still provides some features that you might find useful (such as distinct definitions for 0
/false
, 1
/true
, pretty printing, parsing with trailing commas and builder pattern), but it might be overkill if you just need basic JSON support. Consider using the native function instead of this library.
Simplest case of JSON parsing and value retrieval.
# Experimental!
~ "git: github.com/ekgame/uiua-json" ~ JsonElementValue ParseJsonString
$ {
$ "foo": "bar"
$ }
# Parse the JSON
ParseJsonString
# The object values are wrapped in a typed element, use JsonElementValue to unwrap
JsonElementValue
# Get the value of "foo", which is wrapped in a typed element and unwrap it
JsonElementValue get "foo"
This library also includes convinient builder functions to construct complex JSON structures and get a string from them.
# Experimental!
~ "git: github.com/ekgame/uiua-json"
~ BuildJsonArray! JsonArrayEntry
~ BuildJsonObject! JsonObjectEntry
~ JsonBoolean JsonNull JsonNumber JsonString
~ StringifyJsonPretty
# Build the JSON object.
BuildJsonObject!(
JsonObjectEntry "key" JsonString "value"
JsonObjectEntry "number" JsonNumber 42
JsonObjectEntry "bool-1" JsonBoolean 1
JsonObjectEntry "bool-2" JsonBoolean 0
JsonObjectEntry "null-value" JsonNull
JsonObjectEntry "list" BuildJsonArray!(
JsonArrayEntry JsonNumber 1
JsonArrayEntry JsonNumber 2
JsonArrayEntry JsonNumber 3
)
JsonObjectEntry "nested" BuildJsonObject!(
JsonObjectEntry "nested-key" JsonString "nested-value"
)
)
# Convert it to string and print it.
# Alternatively use "StringifyJson" if you don't need formatting.
&p StringifyJsonPretty
You can also combine the parsing function and the pretty stringifying function to effectively format a JSON string.
# Experimental!
~ "git: github.com/ekgame/uiua-json"
~ ParseJsonString
~ StringifyJsonPretty
$ {"unformatted": "json string", "value": null, "number": 42}
# Parse the string and format it
&p StringifyJsonPretty ParseJsonString
You can also use "under" glyph to unwrap values, edit them and wrap them back - effectively editing the JSON.
# Experimental!
~ "git: github.com/ekgame/uiua-json"
~ JsonElementValue
~ ParseJsonString StringifyJson
$ {"list": [2, 3, 4, 5]}
ParseJsonString
⍜(
# Use "under" to unwrap the values in "list" key
# After it's done, "under" will automatically wrap it back
JsonElementValue get "list" JsonElementValue
| # For each value (using "inventory") - unwrap it and square it
# Then "under" will automatically wrap it back
⍚⍜(JsonElementValue|×.)
)
&p StringifyJson
If you have a wrapped value, you can check for it's type.
# Experimental!
~ "git: github.com/ekgame/uiua-json"
~ JsonBooleanValue
~ JsonElementType JsonElementValue
~ ParseJsonString StringifyJson
# Parse a Json value
ParseJsonString $ true
# Extract the element type
JsonElementType
# Check if the value is a boolean.
# Alternatively chack against "JsonStringValue", "JsonNumberValue",
# "JsonNullValue", "JsonArrayValue" or "JsonObjectValue" as needed.
≍ JsonBooleanValue
- Thanks to JoaoFelipe3 for the struct macro.
- Thanks to jonathanperret from Uiua Discord for helping to massively simplifying my overkill code to unescape UTF16 codepoints to characters.
- Thanks to allantaylor314 from Uiua Discord for their hexidecomal decoding code.
- General thanks to the Uiua Discord server for holding my hand and Uiua tips.