JSONObject class

JSONObject is a subclass and enhancement for the Xojo’s built-in JSONItem class.
It fixes some problems/bugs and enhance the original functionality.
More info and download from: https://github.com/maxvalle/JSONObject

I have added a link to this on the Open-Source project page.

This class does not correspond to JSON anymore:

©2016 by Massimo Valle
Provided with MIT license. See the enclosed LICENSE file.

This class is a subclass of the original JSONItem class built-in in Xojo and it’s meant to fix
some problems/bugs reported below:

  • The JSONItem class accept values and array elements as Variant.
    This mean it’s possible to assign to JSONItem ANY kind of object and thus creating invalid JSON objects
    which doesn’t render/serialize. A such problem is only detected when rendering the object to obtain
    a JSON string (Exception).[/quote]
    Still any object can be added as with the regular JSONItem class. But now Nil is used instead of raising an exception. What good is that for? I’d rather know that something wrong has been appended while debugging.

[quote]- The JSONItem doesn’t render added objects which implicitly convert to string or JSONItem
Despite the use of Variant any object that can be implicitly converted to a string or JSONItem is added
as the original object, producing again invalid JSON.
Additionally, when a such invalid object is rendered, the JSONException is misleading: it reports
the object is not valid but shows the string resulting from conversion. Like to say a string is invalid.[/quote]
Do not rely on Operator_Convert of an object. Append the string representation of that object.
And appending a date object is nonsense, as there is no date representation in JSON. Convert the date to the required string representation and append it.
And in addition most blogs and forum posts recommend ISO 8601 for date representation, which would look like this:
and not like this:
2016-03-16 07:13:27

[quote]- The JSONItem permit to create circular references
It’s possible to add a JSONItem to itself.
This crash completely the Xojo IDE or the application when the JSONItem is rendered with JSONItem.toString().[/quote]
Circular references are allowed in JSON.

With that class problems are deferred from debugging to the end user. Not good IMHO…

Eli, I mostly don’t agree with your comments, however the above class fit my needs and I released as open source for anyone else might need it. The standard JSONItem is still there and you can continue using it.

But you know, the good part of open source is that you have one more choice: modify it to fit your needs.