Re: json
- Posted by CoJaBo2 Aug 06, 2012
- 2525 views
You'd probably want to use the map type to represent the JSON Objects (call them Objects, not some other term).
The actual representation would look something like this:
json=deserialize({ "an_object": { "an_array": [ 104,101,108,108,111 ], "a_string": "hello", "an_array_of_objects": [ {},{},{} ] }, "a_boolean": false, "a_null": null}) --json={JSON_OBJECT,1} (1 simply being a map ID) an_object=get(json[2],"an_object") --object={JSON_OBJECT,2} (2 being another map ID) array=get(an_object[2],"an_array") --array={JSON_ARRAY,{{JSON_NUMBER,104},{JSON_NUMBER,101},{JSON_NUMBER,108},{JSON_NUMBER,108},{JSON_NUMBER,111}}} string=get(an_object[2],"a_string") --string={JSON_STRING,{104,101,108,108,111}} (or "hello") obj_array=get(an_object[2],"an_array_of_objects") --obj_array={JSON_ARRAY,{{JSON_OBJECT,3},{JSON_OBJECT,4},{JSON_OBJECT,5}}} boolean=get(json[2],"a_boolean") --boolean={JSON_BOOLEAN,0} (could probably define TRUE and FALSE constants if the std libs don't already) null=get(json[2],"a_null") --null={JSON_NULL,0} (keeping the "value" param for consistency, even though it is redundant)
As you can see, this allows us to guarantee certain structural conditions-
- Every JSON value (Object, Array, String, Number, Boolean, Null) is represented as a 2-element sequence in the format {TYPE, VALUE}.
- Every Object is a Eu map with 0 or more string keys, each of which has a value that is a valid JSON value.
- Every Array is a Eu sequence of 0 or more valid JSON values.
An API could be layered on top of this to hide this typing complexity and simplify accessing things for the programmer; I have an idea for such an API, but don't have time to properly describe it right now.