I’m running into a problem with the way that JSONItem instantiates empty arrays.
Dim obj As new JSONItem("[]")
if obj.IsArray then
MsgBox "JSON arrays are awesome."
else
MsgBox "I'm not an object, I'm an array?" // this should not display
end if
In the above code, the obj.IsArray value is false. Obj.ToString() also returns “{}”, instead of “[]”, which is different than the string that obj was initialized with.
It might seem like a trivial issue, but I use JSON extensively in communication protocols, and sometimes need to indicate that a certain value is an array by giving it a value of “[]” (initializing the value as [“1”] or something similar is not an option).
Dim item1 As New JSONItem("[]")
Dim item2 As New JSONItem("{}")
Both item1.Count() and item2.Count() is equal to 0.
My JSON objects are serialized on the one side to a string with the ToString() method, and deserialzed on the other side with the JSONItem constructor… and because ToString() changes empty arrays (e.g. []) to objects ({}) the data “type” comes out wrong at the other side.
I’ve tried the “adding one element” workaround, but for technical reasons beyond the scope of this post, I need that an “empty” arrays are correctly instantiated.
Just a brief explanation through code why.
Dim obj1 As new JSONItem("[""""]")
Dim obj2 As new JSONItem("{}")
Dim obj3 As new JSONItem("[]")
// obj1 is an array and obj1.Count = 1 : CORRECT
// obj2 is an object and obj2.count = 0 : CORRECT
// obj3 is an object : INCORRECT
For my program, I need to create a JSONItem that is an array and constains zero elements… and I cannot find a way of doing this.
Yes I know,
Maybe you should not take the count method to check if it’s an array or not.
If you parse a “[…]” structure then it’s an array
If you assign a indexed value (i.e. jsonItem.append) then it’s an array
if you remove an indexed item (i.e. jsonitem.remove) you should not reflag the array type (documentation asserts that once you assign a type for an item then it’s of that type)
Now count is useful for its real meaning : the item has count info in it; not it’s an array or not.
Perhaps I’m getting old, and the cool kids growing up with tablets are more comfortable with touch typing, but I much rather prefer a good old 101 keyboard.
I am more than willing to make my workaround class for this issue available to anyone else that needs it.
While working on a MongoDB driver for Xojo I wrote a JSON parser capable of parsing less strict JSON (e.g. {mykey:“myvalue”} instead of {“mykey”:“myvalue”}), so it was a quick exercise for me to wrap this existing code as a drop-in replacement class, with one difference… the IsArray is a property instead of a method.
This JSON parser is also used in my 3D engine to parse very large JSON strings very quickly.
Haven’t done any official testing about the correctness of the class, so I provide it “as-is” to those who might need it… so please use it at your own risk.
Whoa, wAit a sec. Making a class that allows you to parse errors in a JSON string just encourages people to make bad JSON generators which won’t work anywhere else.
FWIW what you are talking about there is a JavaScript object (keys without quotes).