### Serialize Custom Types

To serialize Custom Types with JSON a Map of Maps is used.
The Map is build by a Sub which converts the Custom Type.
Example Sub
'Convert the custom type to a map
'Type ContactType(Name As String, EMail As String)
`Sub ContactTypeToMap(contact As ContactType) As Map
Return CreateMap("name": contact.Name, "email": contact.EMail)
End Sub`
Example Non-UI application using above Sub
'Example of serialize custom types with json by creating a sub that converts the type to a Map.
'The map then used to create a map of maps for json
Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
End Region
`Sub Process_Globals
Type ContactType(Name As String, EMail As String)
End Sub`
`Sub AppStart (Args() As String)
DefineContacts
End Sub`
`Sub DefineContacts
Dim contacta, contactb, contactc As ContactType
'
contacta.Initialize
contacta.Name = "Name A"
contacta.EMail = "EMail A"
contactb.Initialize
contactb.Name = "Name B"
contactb.EMail = "EMail B"
contactc.Initialize
contactc.Name = "Name C"
contactc.EMail = "EMail C"
Dim jg As JSONGenerator
jg.Initialize(CreateMap(1: ContactTypeToMap(contacta), 2: ContactTypeToMap(contactb), 3: ContactTypeToMap(contactc)))
Log(jg.ToPrettyString(2))
End Sub`
'Convert the custom type to a map
'Type ContactType(Name As String, EMail As String)
`Sub ContactTypeToMap(contact As ContactType) As Map
Return CreateMap("name": contact.Name, "email": contact.EMail)
End Sub`

Changed: , Category: JSON, File: T6C210M1540.htm, ID: 1540