Cerberus X Documentation

Module brl.json

Minimalistic JSON module. More...

Declarations

Imported By
holzchopf.doctest
Please note that only documented modules are listed here, there might be undocumented modules that import this one.
Classes
JsonArray JSON arrays are ordered lists of values in square brackets, e.g.
JsonBool Value representing a bool.
JsonError Object thrown when
JsonNull Value representing null.
JsonNumber Value representing a number.
JsonObject JSON objects are collections of pairs in curly brackets, e.g.
JsonParser The json parser is used to translate a JSON string to json.
JsonString Value representing a string.
JsonValue This base class provides some fall-back functionality for the specific json value

Detailed Discussion

Minimalistic JSON module. Fully functional on one hand, but on the other hand not very sophisticated concerning error correction or -handling when fed with faulty json strings.

JSON is a form of markup language to store any kind of value in any complexity in objects and / or arrays. Here are some examples of valid JSON strings (each line is considered to be one JSON string):

"Foobar"
42
true
{"name":"John","age":55}
[1,2,3,"four","five",false]
See also

http://json.org

Example
Strict

Import brl.json

Function Main:Int()
Local jsonTypes := ["OBJECT", "ARRAY", "NULL", "BOOL", "NUMBER", "STRING"]

Local str:String
' try these different JSON inputs
'str = "foul!" ' invalid JSON
'str = "null" ' null value
'str = "true" ' boolean value
'str = "3.14" ' numerical value
'str = "~qtext~q" ' string value
'str = "{~qfoo~q:true,~qbar~q:false}" ' object
str = "[1,2,~qbla~q]" ' array

' report what's fed into the parser
Print "json input:"
Print str
Print ""

' try to parse JSON
Local json:JsonValue
Try
json = New JsonParser(str).ParseValue()
Catch e:JsonError
Print "invalid JSON"
Return 0
End

' report how JSON is interpreted
Print "value is of type "+ jsonTypes[ json.Type ] +" and contains: "

Select json.Type
' JsonObject
Case JsonValue.OBJECTTYPE
For Local jn := Eachin JsonObject( json ).GetData()
Print "node "+ jn.Key() +" containing "+ jn.Value().ToJson()
End
' JsonArray
Case JsonValue.ARRAYTYPE
For Local entry := Eachin JsonArray( json ).GetData()
Print entry.ToJson()
End
' JsonNull
Case JsonValue.NULLTYPE
Print JsonNull( json ).ToJson()
' JsonBool
Case JsonValue.BOOLTYPE
Print JsonBool( json ).ToJson()
' JsonNumber
Case JsonValue.NUMBERTYPE
Print JsonNumber( json ).ToJson()
' JsonString
Case JsonValue.STRINGTYPE
Print JsonString( json ).ToJson()
End

Return 0
End