Android Question [solved] Serialization of a slightly more complex structure

ThePuiu

Active Member
Licensed User
Longtime User
I have a structure like:
B4X:
Type listObject (idValue As Int, Name As String, NewVal As Object, OldVal As Object)
Public listObjects As listObject
Public SaveList As List

and I'm trying to get a JSON to send to a server. Used code:
B4X:
Dim JSONGenerator As JSONGenerator
JSONGenerator.Initialize(SaveList)
Dim JSONstring As String
JSONstring = JSONGenerator.ToString

but application crash and I receive the error code:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap
at anywheresoftware.b4a.objects.collections.JSONParser$JSONGenerator.Initialize(JSONParser.java:132)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7357)
at android.widget.TextView.performClick(TextView.java:14210)
at android.view.View.performClickInternal(View.java:7323)
at android.view.View.access$3200(View.java:849)
at android.view.View$PerformClick.run(View.java:27884)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7266)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
java.net.SocketException: Socket closed
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:144)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:248)
at java.io.BufferedInputStream.read(BufferedInputStream.java:267)
at anywheresoftware.b4a.shell.ShellConnector.readControlData(ShellConnector.java:191)

How can I solve the problem?
 

DonManfred

Expert
Licensed User
Longtime User
How can I solve the problem?
start with posting more complete code. The code you posted does not raise this error. At least i do not see any Map involved here...

java.lang.ClassCastException: java.util.ArrayList cannot be cast to anywheresoftware.b4a.objects.collections.Map$MyMap
you are trying to cast a arraylist (probably list) to a MAP

I guess you should not use Object in the custom Type. Use the Type the object really is. Map, List, Array, primitive.
 
Last edited:
Upvote 0

ThePuiu

Active Member
Licensed User
Longtime User
I am forced to use the object type, because is not known what date it will contain.
Attached is a test project that demonstrates the error.
 

Attachments

  • testing.zip
    9.1 KB · Views: 181
Upvote 0
Top