JsonExtractor¶
Utility class for working with JSON structures.
Given the following JSON: ```json { "environments": [ { "name": "corral-env", "user": "sean", "image": "an-image:latest", "shell": "fish", "workdir": "/workspace", "workspace": "/home/sean/ponylang/corral", "mounts": [ {"source":"/var/run/docker.sock","target":"/var/run/docker.sock", "type": "bind"} ] } ] }
We can use the following code to extract our values:
```pony
primitive Parser
fun apply(json: JsonType val): Array[Environment] val ? =>
recover val
let envs = JsonExtractor(json)("environments")?.as_array()?
let result: Array[Environment] = Array[Environment]
for e in envs.values() do
let obj = JsonExtractor(e).as_object()?
let name = JsonExtractor(obj("name")?).as_string()?
let user = JsonExtractor(obj("user")?).as_string()?
let image = JsonExtractor(obj("image")?).as_string()?
let shell = JsonExtractor(obj("shell")?).as_string()?
let workdir = JsonExtractor(obj("workdir")?).as_string()?
let workspace = JsonExtractor(obj("workspace")?).as_string()?
let mounts = recover trn Array[Mount] end
for i in JsonExtractor(obj("mounts")?).as_array()?.values() do
let m = MountParser(i)?
mounts.push(m)
end
let environment = Environment(name, user, image, shell, workdir, workspace, consume mounts)
result.push(environment)
end
result
end
primitive MountParser
fun apply(json: JsonType val): Mount ? =>
let obj = JsonExtractor(json).as_object()?
let source = JsonExtractor(obj("source")?).as_string()?
let target = JsonExtractor(obj("target")?).as_string()?
let mtype = JsonExtractor(obj("type")?).as_string()?
Mount(source, target, mtype)
The JsonExtractor creates a lot of intermediate objects, but it makes the code easier to read and understand. We suggest not using it in critical paths where performance is a concern.
Constructors¶
create¶
Create a new JsonExtractor from a JSON structure.
new val create(
json: (F64 val | I64 val | Bool val |
None val | String val | JsonArray val |
JsonObject val))
: JsonExtractor val^
Parameters¶
Returns¶
- JsonExtractor val^
Public Functions¶
apply¶
Extract an array or object by index or key and return a new JsonExtractor.
Parameters¶
Returns¶
- JsonExtractor val ?
size¶
Return the size of the JSON structure.
Results in an error for any structure that isn't a JsonArray
or JsonObject
.
Returns¶
- USize val ?
values¶
Return an iterator over the values of the JSON structure.
Results in an error for any structure that isn't a JsonArray
.
fun val values()
: Iterator[(F64 val | I64 val | Bool val | None val | String val | JsonArray val | JsonObject val)] ref ?
Returns¶
- Iterator[(F64 val | I64 val | Bool val | None val | String val | JsonArray val | JsonObject val)] ref ?
pairs¶
Return a pairs iterator over the values of the JSON structure.
Results in an error for any structure that isn't a JsonArray
.
fun val pairs()
: Iterator[(String val , (F64 val | I64 val | Bool val | None val | String val | JsonArray val | JsonObject val))] ref ?
Returns¶
- Iterator[(String val , (F64 val | I64 val | Bool val | None val | String val | JsonArray val | JsonObject val))] ref ?
as_array¶
Extract an Array from the JSON structure.
fun val as_array()
: Array[(F64 val | I64 val | Bool val | None val | String val | JsonArray ref | JsonObject ref)] val ?
Returns¶
- Array[(F64 val | I64 val | Bool val | None val | String val | JsonArray ref | JsonObject ref)] val ?
as_object¶
Extract a Map from the JSON structure.
fun val as_object()
: HashMap[String val, (F64 val | I64 val | Bool val | None val | String val | JsonArray ref | JsonObject ref), HashEq[String val] val] val ?
Returns¶
- HashMap[String val, (F64 val | I64 val | Bool val | None val | String val | JsonArray ref | JsonObject ref), HashEq[String val] val] val ?
as_string¶
Extract a String from the JSON structure.
Returns¶
- String val ?
as_none¶
Extract a None from the JSON structure.
Returns¶
- None val ?
as_f64¶
Extract a F64 from the JSON structure.
Returns¶
- F64 val ?
as_i64¶
Extract a I64 from the JSON structure.
Returns¶
- I64 val ?
as_bool¶
Extract a Bool from the JSON structure.
Returns¶
- Bool val ?
as_string_or_none¶
Extract a String or None from the JSON structure.