Xojo Conferences
MBSSep2018MunichDE
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/Util/JSON/JSONItem clone
Function:
Required plugins for this example: MBS Util Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Util/JSON/JSONItem clone
This example is the version from Tue, 20th Jan 2014.
Project "JSONItem clone.rbp"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
EventHandler Sub Open() // MyJSONItem is a clone for replace JSONItem, a fast clone... dim j as new JSONItem j.Value("Hello") = "World" j.Value("1") = 2 MsgBox str(j.Count)+" items" dim v as new MyJSONItem v.Value("Hello") = "World" v.Value("1") = 2 MsgBox str(v.Count)+" items" End EventHandler
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
Class MyJSONItem
Sub Append(v as Variant) // for arrays we use array node instead of object node, so we need to switch if not IsArray then j = JSONMBS.NewArrayNode IsArray = true end if dim p as JSONMBS = ToJSON(v) j.AddItemToArray p End Sub
Sub Constructor() // start with new object j = JSONMBS.NewObjectNode End Sub
Protected Sub Constructor(j as JSONMBS) // wrap a JSONMBS self.j = j End Sub
Sub Constructor(data as string) // parse and raise exception on error j = new JSONMBS(data) if j.Handle = 0 then dim je as new MyJSONException je.Message = j.ParseError end if End Sub
Function Count() As Integer // only for array objects, give size of array Return j.ArraySize End Function
Private Function FromJSON(j as JSONMBS) As Variant if j = nil then Return nil Select case j.Type case JSONMBS.kTypeArray Return new MyJSONItem(j) case JSONMBS.kTypeFalse Return false case JSONMBS.kTypeNull return nil case JSONMBS.kTypeNumber Return j.ValueDouble case JSONMBS.kTypeObject Return new MyJSONItem(j) case JSONMBS.kTypeString Return j.ValueString case JSONMBS.kTypetrue Return true end Select End Function
Function Lookup(key as Variant, defaultValue as Variant) As Variant // lookup a value Select case j.Type case JSONMBS.kTypeArray dim n as integer = key.IntegerValue dim c as JSONMBS = j.ArrayItem(n) if c = nil then Return defaultValue else Return FromJSON(c) end if case JSONMBS.kTypeObject dim l as string = key dim c as JSONMBS = j.Child(l) if c = nil then Return defaultValue else Return FromJSON(c) end if else Break end Select Return defaultValue End Function
Function Name(index as integer) As string // find name of an item dim c as JSONMBS = j.ChildNode for i as integer = 1 to index c = c.NextNode next Return c.Name End Function
Private Function ToJSON(v as Variant) As JSOnMBS // get a JSONItem for Variant if v = nil then Return JSONMBS.NewNullNode end if if v isa MyJSONItem then dim m as MyJSONItem = v Return m.j end if if v isa JSONMBS then Return v end if Select case v.Type case Variant.TypeString Return JSONMBS.NewStringNode(v.StringValue) case Variant.TypeBoolean Return JSONMBS.NewBoolNode(v.BooleanValue) case Variant.TypeDouble, Variant.TypeSingle, Variant.TypeInteger Return JSONMBS.NewNumberNode(v.DoubleValue) else Break end Select End Function
Function ToString() As string // return Return j.toString End Function
Function Value(key as Variant) As Variant Select case j.Type case JSONMBS.kTypeArray dim n as integer = key dim c as JSONMBS = j.ArrayItem(n) if c = nil then dim k as new KeyNotFoundException k.Message = "Item "+str(n)+" not found." raise k else Return FromJSON(c) end if case JSONMBS.kTypeObject dim l as string = key dim c as JSONMBS = j.Child(l) if c = nil then dim k as new KeyNotFoundException k.Message = "Key "+L+" not found." raise k else Return FromJSON(c) end if else Break end Select End Function
Sub Value(key as Variant, assigns value as Variant) j.AddItemToObject key, ToJSON(Value) End Sub
Property IsArray As Boolean
Property j As JSONMBS
End Class
Class MyJSONException Inherits RuntimeException
End Class
End Project

Feedback, Comments & Corrections

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





Links
MBS Xojo Plugins