New JSON Plugin for Xojo

Our MBS JSON class called JSONMBS got a bit aged in the last years. Time for a rewrite and over the last months, we got that on the way. To make a compelling offer to our clients for using this class, it has include a few key things:

Although we swap the underlaying library, we have to keep the same interface so existing code using JSONMBS continues to work. And we need to do same behaviors as much as possible.

We need to keep performance at great levels, especially when JSONItem caught up in Xoxo 2021 Release 1. Since we have to go through the Plugin SDK interface, we are slower in some cases due to that extra layer. In other cases we can be more efficient and avoid calls and do things quicker. Performance should be in general the same and exact results depend on what operations are used in the benchmark code.

Since you may use JSONItem, we need to be compatible, so you can easily switch to our class. That means we have to copy the JSONItem public interface in both API 1 and API 2. That is why we get both Value() and ValueAt(). You can choose whatever name you prefer since they are all connected to the same methods internally.

We like to add great new features like JSON Path Queries, Search & Replace and flatten & unflatten. If you know XML queries, you may like the JSON queries:

  • Query(Path as string, Options as Integer = 0) as JSONMBS
  • Search(Path as string) as JSONMBS
  • Replace(Path as string, NewValue as Variant) as JSONMBS
  • Flatten(value as JSONMBS) as JSONMBS
  • Unflatten(value as JSONMBS) as JSONMBS

Let us show you an example of a search in JSON to find the names of the cities:

Dim json As String = "{ ""locations"": ["+EndOfLine+_
  "{""name"": ""Seattle"", ""state"": ""WA""},"+EndOfLine+_
  "{""name"": ""New York"", ""state"": ""NY""},"+EndOfLine+_
  "{""name"": ""Bellevue"", ""state"": ""WA""},"+EndOfLine+_
  "{""name"": ""Olympia"", ""state"": ""WA""}"+EndOfLine+_
  "]}"
Dim query As String = "locations[?state == 'WA'].name | sort(@) | {WashingtonCities: join(', ', @)}"

Dim j As New JSONMBS(json)
Dim r As JSONMBS = j.Search(query)

MessageBox r.toString
// shows: { "WashingtonCities": "Bellevue, Olympia, Seattle" }

We add modern features like iterators to use for-each loops, which didn’t exist when we started with our JSON classes over ten years ago. We got:

  • Use Iterate to iterate over child nodes and get JSONMBS objects from the iterator.
  • Use IterateEntries to iterate about entries and get JSONEntryMBS objects with keys and values. This can be used for both objects and arrays. For arrays, we use the index numbers as keys.
  • Use IterateValues to iterate about values for objects and arrays. The iterator gives you the values as variant.
Dim o As New JSONMBS

o.add 1
o.add 2
o.add 3

For Each v As JSONMBS In o.Iterate
  Break
  // watch in debugger
Next

For Each v As JSONEntryMBS In o.IterateEntries
  Break
  // watch in debugger
Next

For Each v As Variant In o.IterateValues
  Break
  // watch in debugger
Next

Please try the new class and let us know what you think.
We may keep a separate OldJSON plugin available with the old code in case someone runs into compatibility problems.

6 Likes

Those are some pretty compelling new features! Is JSONMBS case-sensitive like Xojo JSONitem?

It better be. JSON is case sensitive.

2 Likes

Yes. But you can switch to case insensitive if needed.

1 Like

Just to be clear, any libraries that are not case sensitive are not working correctly. JSON stands for JavaScript Object Notation and because JavaScript strings are case sensitive, so are JSON names (keys).

1 Like

Im having trouble with the xojo JSONItem introducing escape slashes into my json that I dont want. Does the MBS version do that? I think Ill switch anyway but some apis im using dont want the slashes. I could swear there used to be a property or a mthod to turn that off/on

There was when Xojo used a library that was written in Xojo. Problem was that it was really slow. The library they use now is part of the c++ framework and doesn’t offer that.

FWIW, there are some symbols that must be escaped. Tabs, Carriage Returns, Line Feeds and Unicode characters, specifically, but there may be others, so you’re not going to get rid of them all.

You can try our plugin and see if it fits your needs. Let’s try it:

Dim j As New JSONMBS

j.Value("test") = "Hello"+EndOfLine+"World"

Dim s As String = j.toString

Show: {"test":"Hello\nWorld"}

1 Like

Thanks Christian! I like this a lot better than the built in one!

Maybe more accurate to say that they are “not fully-compliant with JavaScript standards”. They could certainly work correctly within their own environment. There are plenty of applications where JSON is used internally and never interacts with the outside world.

My annoyance with case-sensitivity is not so much with regard to the JSON itself, but in dictionaries created by ParseJSON that somehow inherit the case-sensitivity. I’m just so used to Xojo dictionaries not being case-sensitive that I have to add a comment on every line reminding myself.

On the other hand, this is something I use often to create case sensitive dictionaries.

var d as Dictionary = ParseJSON( "{}" )
4 Likes