I have been playing around with FLEXJSON, a free Java package for translating objects into JSON Strings (= serialization) and JSON Strings into objects (= de-serialization).
The demo code:
|
Jabaco Source
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
Option Explicit
'
' Demo for FLEXJSON serializer/deserializer in Jabaco
'
' cf. http://flexjson.sourceforge.net/
'
' Make sure that flexjson-2.1.jar is included in the classpath
'
#Import flexjson
#Import java#util#ArrayList
#Import java#util#LinkedList
Public Sub Command1_Click()
Dim serializer As New JSONSerializer()
Dim deserializer As New JSONDeserializer()
Dim s As String
Dim p1 As New clsPerson("Paul", 16)
Dim p2 As New clsPerson("Peter", 5)
Dim p As clsPerson
Dim list1 As New LinkedList()
Dim list2 As ArrayList
list1.add p1
list1.add p2
s = serializer.serialize(list1)
Debug.Print s
' FLEXJSON returns an ArrayList rather than a LinkedList
list2 = Cast(deserializer.deserialize(s), ArrayList)
For Each p In list2
Debug.Print p.name
Next
End Sub
|
My demo class clsPerson
|
Jabaco Source
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
Option Explicit
' FLEXJSON does not recognize our public getters/setters
' so we declare our member variables public for the time being
Public myName As String
Public myAge As Integer
' constructor
Public Sub clsPerson(name As String, age As Integer)
Me.name = name
Me.age = age
End Sub
' FLEXJSON insists that objects should have a parameterless constructor
Public Sub clsPerson()
Me.name = "name ???"
Me.age = 0
End Sub
Public Property Get name() As String
name = myName
End Property
Public Property Let name(n As String)
myName = n
End Property
Public Property Get age() As Integer
age = myAge
End Property
Public Property Let age(a As Integer)
myAge = a
End Property
|
The debugging output is probably no surprise:
[{"class":"clsPerson","myAge":16,"myName":"Paul"},{"class":"clsPerson","myAge":5,"myName":"Peter"}]
Paul
Peter
Consult the
FLEXJSON home for more details and features.
There might be better packages available than FLEXJSON. Any suggestions?
Enjoy!
A1880