« MBS Plugins on Xojo C… | Home | MBS Xojo Plugins, ver… »

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.

The biggest plugin in space...
29 09 23 - 09:32