Android Question Convert Map to Custom Type

AneeshJay

Member
Licensed User
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.

Exception:
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)

Custom Type:
    Type TableOrder(TableNum As Int, IsSettled As Boolean, OrderItems As List, CustomerPayments As List, TakeawayDetails As Map, DiscountAmount As Double)

B4X:
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 !
 

AneeshJay

Member
Licensed User
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.
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
Top