Today we had a bit of fun with JSON coding in Xojo and through what different ways we have to make a JSON in Xojo with different ways.
Let's start with Xojo's built-in ways. So let us use JSONItem class:
Sub Xojo1()
Dim root As New JSONItem
Dim arr As New JSONItem
arr.Add(16)
arr.Add(42)
arr.Add(128)
root.Value("RootA") = "Hello world"
root.Value("ArrayOfNumbers") = arr
root.Compact = False
TextArea1.Text = root.ToString
End Sub
This makes a JSON like this:
{
"RootA": "Hello world",
"ArrayOfNumbers": [
16,
42,
128
]
}
Now second way is to use dictionaries, arrays and variants and pass it initialize a JSONItem, then work with the JSONItem and later get the text:
Sub Xojo2()
Dim root As New Dictionary
Dim arr() as Integer
arr.Add(16)
arr.Add(42)
arr.Add(128)
root.Value("RootA") = "Hello world"
root.Value("ArrayOfNumbers") = arr
dim j as new JSONItem(root)
j.Compact = False
TextArea1.Text = j.ToString
End Sub
Third way is to skip the JSONItem and just use GenerateJSON method:
Sub Xojo3()
Dim root As New Dictionary
Dim arr() as Integer
arr.Add(16)
arr.Add(42)
arr.Add(128)
root.Value("RootA") = "Hello world"
root.Value("ArrayOfNumbers") = arr
TextArea1.Text = GenerateJSON(root, True)
End Sub
Next, let us use MBS Xojo Util Plugin with JSONMBS class:
Sub MBS1()
Dim root As New JSONMBS
Dim arr As JSONMBS = JSONMBS.NewArrayNode
arr.AddItemToArray(JSONMBS.NewInt64Node(16))
arr.AddItemToArray(JSONMBS.NewInt64Node(42))
arr.AddItemToArray(JSONMBS.NewInt64Node(128))
root.AddItemToObject("RootA", JSONMBS.NewStringNode("Hello world"))
root.AddItemToObject("ArrayOfNumbers", arr)
TextArea1.Text = root.toString
End Sub
But of course making all the notes by hand is a bit of tiring, so maybe we can speed it up by using a helper method like NewIntegerArray here:
Sub MBS2()
Dim root As New JSONMBS
root.AddItemToObject("RootA", JSONMBS.NewStringNode("Hello world"))
root.AddItemToObject("ArrayOfNumbers", JSONMBS.NewIntegerArray(Array(16, 42, 128)))
TextArea1.Text = root.toString
End Sub
We also have a Convert method in JSONMBS to convert from dictionaries, arrays and variant. Let's convert this to use that:
Sub MBS3()
Dim root As New Dictionary
Dim arr() As Integer
arr.Add(16)
arr.Add(42)
arr.Add(128)
root.Value("RootA") = "Hello world"
root.Value("ArrayOfNumbers") = arr
Dim j As JSONMBS = JSONMBS.Convert(root)
TextArea1.Text = j.toString
End Sub
You may also have seen three JSON plugins from Björn Eiriksson from Einhugur.com, so let's try the same with his first plugin and JSONNode class:
Sub Einhugur1()
Dim n As New JSONNode()
n.AppendNode(New JSONNode("RootA","Hello World"))
dim c as JSONNode = JSONNode.NewArrayNode("ArrayOfNumbers")
c.AppendValue(16)
c.AppendValue(42)
c.AppendValue(128)
n.AppendNode(c)
TextArea1.Text = n.GetSourceFormatted().DefineEncoding(Encodings.UTF8)
End Sub
Another way using the EinhugurJSON namespace with JSON classes in the second JSON Plugin:
Sub Einhugur2()
Using EinhugurJSON
Dim root As JSONObject = New JSONObject()
Dim arr As JSONArray = New JSONArray()
root.StringValueByKey("RootA") = "Hello world"
root("ArrayOfNumbers") = arr
arr.AppendInteger(16)
arr.AppendInteger(42)
arr.AppendInteger(128)
// Put the raw source to the edit field
TextArea1.Text = root.GetSource()
End Sub
Another way using the EinhugurJSONIII namespace with JSON classes in the third JSON Plugin:
Sub Einhugur3()
Using EinhugurJSONIII
Var root As JSONDocument = New JSONDocument()
// By default the documents root is born as null, so we need to define it as Object or Array.
root.SetObject()
Var arr As JSONArray = root.CreateArray()
arr.Append(16)
arr.Append(42)
arr.Append(128)
root.AddMember("RootA", "Hello world")
root.AddMember("ArrayOfNumbers", arr)
'// Put the raw source to the edit field
TextArea1.Text = root.GetSource(True)
End Sub
On the end, we have nine times code to make the same JSON. Except for little variations in the formatting of the output.
Our plugin was made to be faster than JSONItem, but since the Xojo framework caught up in performance, the difference is small enough, that most clients don't care about it anymore. Today you may pick a specific class only to use some feature it has and you need. Like Björn's third plugin can do schema validation, so you'd need this class if you need that feature.
For MBS we still have a few nice features like a toHTML method to show in a HTMLViewer or the Text property to show the user as text. The comparison functions may help to compare content, even if the order of keys is no the same. And we have helper functions like FindValueInObjectArray to quicker find something when parsing objects.
Now which variant is your favorite?