I had just commented on Xojo’s recent blog post, but I thought I repeat it here again in case others want to comment:
1. Comment:
Hold on - Does the new Xojo.Core.Dictionary type guarantee the order of items added to it now? The classic one did not, and I’d be surprised if these semantics have changed.
I like to point out that, when working with JSON data, it’s sometimes critical that the order of items is known and stable. So, if I add two key-value pairs, I expect them to be kept in that order, both when convertin them to text and back. The old JSONItem could do that, apparently. I doubt that the new Xojo.Core.Dictionary does that, or will it?
2. Comment:
Yep, just as I feared:
The input text
{ “z”: “z value”, “a”: “a value” }
when converted with this code:
Dim d As Dictionary = Xojo.Data.ParseJSON(TextArea1.Text.ToText)
TextArea2.Text = Xojo.Data.GenerateJSON(d)
comes out with the wrong order:
{“a”:“a value”,“z”:“z value”}
Now, while you may claim that this is within the spec, it’s still unexpected as it shifts the behavior we’ve come to expect from the old JSONItem class.
Furthermore, the WP article says for this type of JSON data:
“Object an unordered collection of name/value pairs where the names (also called keys) are strings. Since objects are intended to represent associative arrays,[10] it is recommended, though not required,[11] that each key is unique within an object.”
Now, what do you think will happen if I have two key-value pairs with identical keys in there, which according to above quote is valid? Yes, boom.
Though, at least in this case, JSONItem didn’t do better either.
Also, shouldn’t the new Dict classes at least provide case-sensitive keys now? Yet, when I have two keys in a JSON “object” that only differ in case, such as “a” and “A”, I still can’t convert them into a valid JSON object or dictionary, or can I now? Because, JSON keys are indeed handled case-sensitively by most JSON libs I’ve used. Only Xojo’s fails for me.
Remember: Just because it works for you, it doesn’t make it a well working API.