brl.json examples?

Monkey Forums/Monkey Programming/brl.json examples?

Nobuyuki(Posted 2013) [#1]
Anyone have any good examples/tutorials on utilizing brl.json? I'm coming from warpy's json lib and the usage isn't immediately apparent to me.

I normally use it to save lists of objects, if this helps.


programmer(Posted 2013) [#2]
Local jsonArr := New JsonArray(OBJECTS.Length)
Local i := 0
For Local OBJ := EachIn OBJECTS
  Local jsonObj := New JsonObject()
  jsonObj.SetString("name", OBJ.NAME)
  jsonObj.SetInt("x", OBJ.X)
  jsonObj.SetFloat("width", OBJ.WIDTH)

  jsonArr.Set(i, jsonObj)
  i += 1
Next

Local jsonString := jsonArr.ToJson() 

(edited)


Nobuyuki(Posted 2013) [#3]
Thanks. A couple other questions if you have the time:

* Do you know how to get a JsonObject back out from a string to use the Get methods? Perhaps JsonParser is the way to get a JsonObject / JsonArray back out from a string?
* Does Get automatically cast to the appropriate type if late-bound, or is casting (or using the other get methods) required?
* How does JsonValue.PushJson work?


programmer(Posted 2013) [#4]
* Do you know how to get a JsonObject back out from a string to use the Get methods? Perhaps JsonParser is the way to get a JsonObject / JsonArray back out from a string?
Local jsonObj := New JsonObject("{~qKEY1~q: ~qVALUE1~q, ~qKEY2~q: 2, ~qKEY3~q: [0,1,2,3]}")

Local stringValue := jsonObj.GetString("KEY1")
Local intValue := jsonObj.GetInt("KEY2")
Local jsonArr := JsonArray(jsonObj.Get("KEY3"))

For Local i := 0 Until jsonArr.Length()
    Print(jsonArr.GetInt(i))
Next

* Does Get automatically cast to the appropriate type if late-bound, or is casting (or using the other get methods) required?
No, it doesn't. muddy_shoes's lib does - http://www.monkeycoder.co.nz/Community/posts.php?topic=1077
But probably brl.json works faster and requires less memory.


Nobuyuki(Posted 2013) [#5]
Allright, cool. I was using a ton of casting with muddy's lib anyway since it required it originally. This seems like it'll be pretty good.


AdamRedwoods(Posted 2013) [#6]
that example above does not work with brl.json.
I get a "Get Not Found" error, which makes sense, because the base object "JsonValue" has no "Get" function.

so therefore, i do not know how to get a JsonArray using this module.

EDIT: looks like a bug, i'll post it