AneeshJay Member Licensed User Sep 14, 2021 #1 Hi, I'm trying to convert a JSON received from a server to a custom defined type. I'm first deserialising the json to a map and using the below function to convert it to a custom type. However the code breaks when setting a property which is a list. I'm getting the following exception. Kindly help me to extend my function to work with lists and maps. Code: Copy java.lang.IllegalArgumentException: field b4a.aiposlite.coremodule$_tableorder.CustomerPayments has type anywheresoftware.b4a.objects.collections.List, got java.util.ArrayList at java.lang.reflect.Field.set(Native Method) at anywheresoftware.b4a.agraham.reflection.Reflection.SetField2(Reflection.java:429) Code: Copy Type TableOrder(TableNum As Int, IsSettled As Boolean, OrderItems As List, CustomerPayments As List, TakeawayDetails As Map, DiscountAmount As Double) B4X: Copy Sub MapToTableOrder(m As Map) As TableOrder Dim MyTableOrder As TableOrder MyTableOrder.Initialize Dim r As Reflector r.Target = MyTableOrder Dim jo As JavaObject jo.InitializeContext Dim fields() As string = jo.RunMethod("GetFields", Array(MyTableOrder)) For Each str As string In fields r.SetField2(str, m.Get(str)) Next Return MyTableOrder End Sub Thanks !
Hi, I'm trying to convert a JSON received from a server to a custom defined type. I'm first deserialising the json to a map and using the below function to convert it to a custom type. However the code breaks when setting a property which is a list. I'm getting the following exception. Kindly help me to extend my function to work with lists and maps. Code: Copy java.lang.IllegalArgumentException: field b4a.aiposlite.coremodule$_tableorder.CustomerPayments has type anywheresoftware.b4a.objects.collections.List, got java.util.ArrayList at java.lang.reflect.Field.set(Native Method) at anywheresoftware.b4a.agraham.reflection.Reflection.SetField2(Reflection.java:429) Code: Copy Type TableOrder(TableNum As Int, IsSettled As Boolean, OrderItems As List, CustomerPayments As List, TakeawayDetails As Map, DiscountAmount As Double) B4X: Copy Sub MapToTableOrder(m As Map) As TableOrder Dim MyTableOrder As TableOrder MyTableOrder.Initialize Dim r As Reflector r.Target = MyTableOrder Dim jo As JavaObject jo.InitializeContext Dim fields() As string = jo.RunMethod("GetFields", Array(MyTableOrder)) For Each str As string In fields r.SetField2(str, m.Get(str)) Next Return MyTableOrder End Sub Thanks !
Erel B4X founder Staff member Licensed User Longtime User Sep 14, 2021 #2 I would never use such code. It will take you exactly 2 minutes to create code that creates a type instance and fills the field. Upvote 0
I would never use such code. It will take you exactly 2 minutes to create code that creates a type instance and fills the field.
AneeshJay Member Licensed User Sep 14, 2021 #3 Erel said: I would never use such code. It will take you exactly 2 minutes to create code that creates a type instance and fills the field. Click to expand... Thanks for your reply. I had kept your suggestion as a last resort. Since the type has too many fields thought of finding out a more efficient way to achieve this. Upvote 0
Erel said: I would never use such code. It will take you exactly 2 minutes to create code that creates a type instance and fills the field. Click to expand... Thanks for your reply. I had kept your suggestion as a last resort. Since the type has too many fields thought of finding out a more efficient way to achieve this.
Erel B4X founder Staff member Licensed User Longtime User Sep 14, 2021 #4 Maybe you should keep it as a map, or create a custom type with the more important fields and a field that holds the complete map: Code: Copy Type MyCustomType (Name As String, SomeOtherField As Int, AllOther As Map) Upvote 0
Maybe you should keep it as a map, or create a custom type with the more important fields and a field that holds the complete map: Code: Copy Type MyCustomType (Name As String, SomeOtherField As Int, AllOther As Map)