Platforms to show: All Mac Windows Linux Cross-Platform

Back to JSONMBS class.

JSONMBS.ApplyMergePatch(target as JSONMBS, patch as JSONMBS) as JSONMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method JavaScript Object Notation MBS Util Plugin 24.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Applies a merge patch to a json document.
Example
Var source As New JSONMBS("{"+_
" ""title"": ""Goodbye!"","+_
" ""author"" : {"+_
" ""givenName"" : ""John"","+_
" ""familyName"" : ""Doe"""+_
" },"+_
" ""tags"":[ ""example"", ""sample"" ],"+_
" ""content"": ""This will be unchanged"""+_
"}")

Var patch As New JSONMBS("{"+_
" ""title"": ""Hello!"","+_
" ""phoneNumber"": ""+01-123-456-7890"","+_
" ""author"": {"+_
" ""familyName"": null"+_
" },"+_
" ""tags"": [ ""example"" ]"+_
"}")

Var result As JSONMBS = JSONMBS.ApplyMergePatch(source, patch)

Var sourceText As String = source.toString(True)
Var resultText As String = result.toString(True)
Var PatchText As String = Patch.toString(True)

Break

The mergepatch function implement the IETF standard JSON Merge Patch

JSONMBS.ApplyPatch(target as JSONMBS, patch as JSONMBS) as JSONMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method JavaScript Object Notation MBS Util Plugin 24.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Applies a patch to a json document.
Example
Var source As New JSONMBS("{""foo"": ""bar""}")
Var patch As New JSONMBS("["+_
"{ ""op"": ""add"", ""path"": ""/baz"", ""value"": ""qux"" },"+_
"{ ""op"": ""add"", ""path"": ""/foo"", ""value"": [ ""bar"", ""baz"" ] }"+_
"]")

Var result As JSONMBS = JSONMBS.ApplyPatch(source, patch)

Var sourceText As String = source.toString(True)
Var resultText As String = result.toString(True)
Var PatchText As String = Patch.toString(True)

Break

The jsonpatch functions implement the IETF standard JavaScript Object Notation (JSON) Patch.

The JSON Patch IETF standard requires that the JSON Patch method is atomic, so that if any JSON Patch operation results in an error, the target document is unchanged. The patch function implements this requirement by generating the inverse commands and building an undo stack, which is executed if any part of the patch fails.

JSONMBS.Convert(value as variant) as JSONMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method JavaScript Object Notation MBS Util Plugin 19.2 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Converts Xojo variant to JSON object.
Example
Var d As New Dictionary
Var n as Double = 1/3

d.Value("number") = n // default double handling

// custom formatted number
Var j As JSONMBS = JSONMBS.NewNumberNode(Str(n, "-0.0000000000"))

d.value("customNumber") = j
d.Value( "int64") = 1000000000000000000
d.Value("uint64") = 8000000000000000000

Var a() As String
a.Append "Hello"
a.Append "World"

d.Value("array") = a
d.Value("dictionary") = New Dictionary("a":1, "b":2, "c":3)

Var r As JSONMBS = JSONMBS.Convert(d)
MsgBox r.toString

Converts values in variant to matching JSON structures.
Currency is converted to double. Color is converted to integer. Dates are converted to string.
We detect arrays of String, Object, Variant, Single, Double, Int32, Int64, Boolean and Currency and convert them to JSON arrays. Dictionaries are converted to JSON objects with texts for keys.
Anything else can raise an exception about an unsupported type.

Please note that Xojo variants don't make a difference between signed and unsigned integers, so we always convert using signed integers.

With version v19.3, the array/dictionary can also contain JSONMBS objects.
With version v24.3 we also convert JSONItem and convert folderitem, date or DateTime objects to strings.

See also:

JSONMBS.Flatten(value as JSONMBS) as JSONMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Flattens a json object or array to a single depth object of key-value pairs.
Example
Var j As New JSONMBS

j.Value("Hello") = "World"
j.Value("test") = 123

Var f As JSONMBS = JSONMBS.Flatten(j)
Var s As String = f.toString

MessageBox s
// shows: {"$['Hello']":"World","$['test']":123}

The keys in the flattened object are normalized json paths. The values are primitive (string, number, boolean, or null), empty object ({}) or empty array ([]).

JSONMBS.isValidJSON(JSON as string) as Boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method JavaScript Object Notation MBS Util Plugin 24.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Checks if JSON text is valid.

This parses the JSON text and returns true on success or false on failure.

This checks if the given text is a valid JSON fragment and can be e.g. added as value to a JSON object or array. So this returns true for proper formatted boolean, number and text pieces, too.

JSONMBS.JSONObjectCount as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method JavaScript Object Notation MBS Util Plugin 15.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
For debugging, the plugin counts how many JSONMBS objects we have.

JSONMBS.MergePatchFromDiff(source as JSONMBS, target as JSONMBS) as JSONMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method JavaScript Object Notation MBS Util Plugin 24.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Create a JSON Merge Patch from a diff of two json documents.
Example
Var source As New JSONMBS("{"+_
" ""title"": ""Goodbye!"","+_
" ""author"" : {"+_
" ""givenName"" : ""John"","+_
" ""familyName"" : ""Doe"""+_
" },"+_
" ""tags"":[ ""example"", ""sample"" ],"+_
" ""content"": ""This will be unchanged"""+_
"}")

Var target As New JSONMBS("{"+_
" ""title"": ""Hello!"","+_
" ""author"": {"+_
" ""givenName"": ""John"""+_
" },"+_
" ""tags"": ["+_
" ""example"""+_
" ],"+_
" ""content"": ""This will be unchanged"","+_
" ""phoneNumber"": ""\u002B01-123-456-7890"""+_
"}")

Var patch As JSONMBS = JSONMBS.MergePatchFromDiff(source, target)

Var sourceText As String = source.toString(True)
Var targetText As String = target.toString(True)
Var PatchText As String = Patch.toString(True)

Break

The mergepatch function implement the IETF standard JSON Merge Patch
Returns a JSON Merge Patch.

JSONMBS.PatchFromDiff(source as JSONMBS, target as JSONMBS) as JSONMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method JavaScript Object Notation MBS Util Plugin 24.0 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Create a JSON Patch from a diff of two json documents.
Example
Var source As New JSONMBS("{""/"": 9, ""foo"": ""bar""}")
Var target As New JSONMBS("{ ""baz"":""qux"", ""foo"": [ ""bar"", ""baz"" ]}")

Var Patch As JSONMBS = JSONMBS.PatchFromDiff(source, target)

Var sourceText As String = source.toString(True)
Var targetText As String = target.toString(True)
Var PatchText As String = Patch.toString(True)

Break

Returns a JSON Patch.

The jsonpatch functions implement the IETF standard JavaScript Object Notation (JSON) Patch.

The JSON Patch IETF standard requires that the JSON Patch method is atomic, so that if any JSON Patch operation results in an error, the target document is unchanged. The patch function implements this requirement by generating the inverse commands and building an undo stack, which is executed if any part of the patch fails.

See also:

JSONMBS.PatchFromDiff(source as JSONMBS, target as JSONMBS, KeyToCopy as String) as JSONMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method JavaScript Object Notation MBS Util Plugin 24.1 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Create a JSON Patch from a diff of two json documents.

Same as normal PatchFromDiff, but copies the given primary key field from JSON entries to the diff.

See also:

JSONMBS.Unflatten(value as JSONMBS) as JSONMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
shared method JavaScript Object Notation MBS Util Plugin 23.5 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Unflattens that object back to the original json.
Example
Var t As String = "{""$['Hello']"":""World"",""$['test']"":123}"

Var f As New JSONMBS(t)
Var j As JSONMBS = JSONMBS.Unflatten(f)

Var s As String = j.toString

MessageBox s
// shows: {"Hello":"World","test":123}

The keys in the flattened object are normalized json paths. The values are primitive (string, number, boolean, or null), empty object ({}) or empty array ([]).

The items on this page are in the following plugins: MBS Util Plugin.


The biggest plugin in space...