%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 pet as Pet Function getName() as String getName = firstName & " " & lastName End Function End Class Class Pet Public name as String Public type as String End Class Class ComplexPerson Public firstName as String Public lastName as String Public pets as Variant Function getName() as String getName = firstName & " " & lastName End Function End Class Sub Initialize Print "Running Sample1" Call sample1() Print "Running Sample2" Call sample2() End Sub Sub sample1() Dim helper as new JsonConversionHelper() Dim petConverter as New JsonBasicObjectConverter("Pet", "deser-20") Dim person as Person Dim json as String json = |{"firstName":"Ron","lastName":"Burgundy","pet": {"name":"Baxter","type":"Dog"}}| Call helper.withCustomConverter("pet", petConverter) Set person = helper.fromJsonString(json, "Person", "deser-20") Print person.getName() & " has a pet called " & person.pet.name End Sub Sub sample2() Dim helper as new JsonConversionHelper() Dim petConverter as New JsonBasicObjectArrayConverter("Pet", "deser-20") Dim person as ComplexPerson Dim json as String json = |{"firstName":"Tom","lastName":"Popper","pets": [{"name":"Captain","type":"Gentoo Penguin"},{"name":"Loudy","type":"Gentoo Penguin"},{"name":"Nimrod","type":"Gentoo Penguin"}]}| Call helper.withCustomConverter("pets", petConverter) Set person = helper.fromJsonString(json, "ComplexPerson", "deser-20") ' person.pets returns a Variant, so need to wrap CStr() around person.pets(0).name in Print statement Print person.getName() & " has " & UBound(person.pets) + 1 & " pets called " & Cstr(person.pets(0).name) & ", " & Cstr(person.pets(1).name) & " and " & Cstr(person.pets(2).name) End Sub