Skip to content

JsonExtractor

[Source]

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.

class val JsonExtractor

Constructors

create

[Source]

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


Public Functions

apply

[Source]

Extract an array or object by index or key and return a new JsonExtractor.

fun val apply(
  idx_or_key: (String val | USize val))
: JsonExtractor val ?

Parameters

Returns


size

[Source]

Return the size of the JSON structure.

Results in an error for any structure that isn't a JsonArray or JsonObject.

fun val size()
: USize val ?

Returns


values

[Source]

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


pairs

[Source]

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


as_array

[Source]

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


as_object

[Source]

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


as_string

[Source]

Extract a String from the JSON structure.

fun val as_string()
: String val ?

Returns


as_none

[Source]

Extract a None from the JSON structure.

fun val as_none()
: None val ?

Returns


as_f64

[Source]

Extract a F64 from the JSON structure.

fun val as_f64()
: F64 val ?

Returns


as_i64

[Source]

Extract a I64 from the JSON structure.

fun val as_i64()
: I64 val ?

Returns


as_bool

[Source]

Extract a Bool from the JSON structure.

fun val as_bool()
: Bool val ?

Returns


as_string_or_none

[Source]

Extract a String or None from the JSON structure.

fun val as_string_or_none()
: (String val | None val) ?

Returns