%REM Copyright 2022-2023 HCL America, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License %END REM Option Public Option Declare ' Modify use statement as appropriate for your environent to point to relative path Use "../../../src/VoltScriptJsonConverter" Class Person Public firstName as String Public lastName as String Public age as Integer Function toString() as String toString = firstName & " " & lastName & " - " & age End Function End Class Class CustomPerson Public firstName as String Public lastName as String Public generatedFromJson as Boolean Function fromJson(source as JsonObject) as Variant Me.firstName = source.getChild("firstName").scalarValue Me.lastName = source.getChild("lastName").scalarValue Me.generatedFromJson = true Set fromJson = Me End Function End Class Sub Initialize() Print "Running Sample1" Call sample1() Print "Running Sample2" Call sample2() Print "Running Sample3" Call sample3() Print "Running Sample4" Call sample4() Print "Running Sample5" Call sample5() End Sub Sub sample1() Dim json as String Dim helper as New JsonConversionHelper() Dim jd as Person json = |{"firstName":"John","lastName":"Doe","age":42}| Set jd = helper.fromJsonString(json, "Person", "deser-10") Print jd.toString() End Sub Sub sample2() Dim json as String Dim helper as New JsonConversionHelper() Dim jd as Variant json = |[{"firstName":"John","lastName":"Doe","age":42},{"firstName":"Jane","lastName":"Doe","age":30}]| jd = helper.fromJsonString(json, "Person", "deser-10") Print jd(0).toString() Print jd(1).toString() End Sub Sub sample3() Dim parser as New JsonParser() Dim json as String Dim obj as JsonObject Dim helper as New JsonConversionHelper() Dim jd as Variant json = |{"success": true, "data": {"firstName":"John","lastName":"Doe","age":42}}| Call parser.loadFromJson(json) Set obj = parser.getRootobject().getChild("data") Set jd = helper.toObject(obj, "Person", "deser-10").fromJson(obj) Print jd.toString() End Sub Sub sample4() Dim parser as New JsonParser() Dim json as String Dim obj as JsonObject Dim helper as New JsonConversionHelper() Dim jd as Variant json = |{"success": true, "data": [{"firstName":"John","lastName":"Doe","age":42},{"firstName":"Jane","lastName":"Doe","age":30}]}| Call parser.loadFromJson(json) Set obj = parser.getRootobject().getChild("data") jd = helper.jsonArrayToObjects(obj, "Person", "deser-10") Print jd(0).toString() Print jd(1).toString() End Sub Sub sample5() Dim json as String Dim helper as New JsonConversionHelper() Dim jd as CustomPerson json = |{"firstName":"John","lastName":"Doe","age":42}| Set jd = helper.fromJsonString(json, "CustomPerson", "deser-10") Print jd.firstName & " " & jd.lastName & " - generated: " & jd.generatedFromJson End Sub