Android Question can't serialize object

jamesnz

Active Member
Licensed User
Longtime User
I have an object I am trying to save with the Key-Value Store but it fails serialization, I think this is due to it containing a bounding box , (being a list of doubles (lat,lng, lat lng))
The project also contains other classes with lists of coordinates which I am looking to save ,so Im looking for a solution that allows me persistence whilst maintaining the structure of the class



B4X:
'Object is of type "GeocodedLocation"
Sub Class_Globals
    Dim  id As String
    Dim  gid As String
    Dim  layer As String
    Dim  source As String
    Dim  source_id As String
    Dim  name As String
    Dim  housenumber As String
    Dim  street As String
    Dim  confidence As Int
    Dim  match_type As String
    Dim  distance As Int
    Dim  accuracy As String
    Dim  country As String
    Dim  country_gid As String
    Dim  country_a As String
    Dim  region As String
    Dim  region_gid As String
    Dim  region_a As String
    Dim  county As String
    Dim  county_gid As String
    Dim  county_a As String
    Dim  locality As String
    Dim  locality_gid As String
    Dim  continent As String
    Dim  continent_gid As String
    Dim  label As String
    Dim Latitude As Double
    Dim Longitude As Double
    Dim height As Int
    Dim BoundingBox As List ' ( this is a list of 4 doubles, lat,lng,lat,lng)
    Dim requiresGeocoding As Boolean
   
End Sub

' here I am simply saving the object

Sub saveLocation ( tag As String, l As geocodedLocation)
   
    Starter.kvs.Put(tag,l)
       
End Sub

'LOG

job
{"geocoding":{"version":"0.2","attribution":"openrouteservice.org | OpenStreetMap contributors | Geocoding by Pelias","query":{"text":"200 Victoria Street Hamilton New Zealand","size":10,"private":false,"lang":{"name":"English","iso6391":"en","iso6393":"eng","defaulted":true},"querySize":20,"parser":"libpostal","parsed_text":{"number":"200","street":"victoria street","city":"hamilton","country":"new zealand"}},"engine":{"name":"Pelias","author":"Mapzen","version":"1.0"},"timestamp":1562742913764},"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[175.266497,-37.771428]},"properties":{"id":"102079271","gid":"whosonfirst:county:102079271","layer":"county","source":"whosonfirst","source_id":"102079271","name":"Hamilton City","confidence":0.4,"match_type":"fallback","accuracy":"centroid","country":"New Zealand","country_gid":"whosonfirst:country:85633345","country_a":"NZL","region":"Waikato Region","region_gid":"whosonfirst:region:85687229","region_a":"WK","county":"Hamilton City","county_gid":"whosonfirst:county:102079271","county_a":"HM","continent":"Oceania","continent_gid":"whosonfirst:continent:102191583","label":"Hamilton City, New Zealand"},"bbox":[175.1843005,-37.846039184,175.344869484,-37.6993298504]},{"type":"Feature","geometry":{"type":"Point","coordinates":[175.289863,-37.765649]},"properties":{"id":"101914271","gid":"whosonfirst:locality:101914271","layer":"locality","source":"whosonfirst","source_id":"101914271","name":"Hamilton","confidence":0.6,"match_type":"fallback","accuracy":"centroid","country":"New Zealand","country_gid":"whosonfirst:country:85633345","country_a":"NZL","region":"Waikato Region","region_gid":"whosonfirst:region:85687229","region_a":"WK","county":"Hamilton City","county_gid":"whosonfirst:county:102079271","county_a":"HM","locality":"Hamilton","locality_gid":"whosonfirst:locality:101914271","continent":"Oceania","continent_gid":"whosonfirst:continent:102191583","label":"Hamilton, New Zealand"},"bbox":[175.218250114,-37.8318706255,175.33390517,-37.723626726]}],"bbox":[175.1843005,-37.846039184,175.344869484,-37.6993298504]}

Origin SUCCESFULLY SET = Hamilton City, New Zealand

Error occurred on line: 220 (directionsAndGeocoding)
java.lang.RuntimeException: Cannot serialize object: [accuracy=centroid, appoptions=null, boundingbox=(ArrayList) [175.1843005, -37.846039184, 175.344869484, -37.6993298504]
, confidence=0, continent=Oceania, continent_gid=whosonfirst:continent:102191583
, country=New Zealand, country_a=NZL, country_gid=whosonfirst:country:85633345
, county=Hamilton City, county_a=HM, county_gid=whosonfirst:county:102079271
, distance=0, getdirections=null, gid=102079271
, height=0, housenumber=null, httputils2service=null
, id=whosonfirst:county:102079271, label=Hamilton City, New Zealand, latitude=-37.771428
, layer=county, locality=null, locality_gid=null
, longitude=175.266497, main=null, makenote=null
, match_type=, name=Hamilton City, pra=null
, region=Waikato Region, region_a=WK, region_gid=whosonfirst:region:85687229
, requiresgeocoding=false, routeprep=null, source=whosonfirst
, source_id=102079271, starter=null, street=null
]
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator.writeType(B4XSerializator.java:258)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator.writeObject(B4XSerializator.java:224)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator.writeList(B4XSerializator.java:252)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator.writeObject(B4XSerializator.java:204)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator.WriteObject(B4XSerializator.java:104)
    at anywheresoftware.b4a.randomaccessfile.B4XSerializator.ConvertObjectToBytes(B4XSerializator.java:62)
    at b4a.example3.keyvaluestore._put(keyvaluestore.java:395)
    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.debug.Debug.delegate(Debug.java:262)
    at com.ideas.openroad.directionsandgeocoding._savelocation(directionsandgeocoding.java:817)
    at com.ideas.openroad.directionsandgeocoding._processreturn(directionsandgeocoding.java:523)
    at com.ideas.openroad.directionsandgeocoding$ResumableSub_LatLngFromAddress.resume(directionsandgeocoding.java:783)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
    at java.lang.reflect.Method.invoke(Native Method)
 

DonManfred

Expert
Licensed User
Longtime User
where is the code which generates the customtype from the json?
 
Upvote 0

jamesnz

Active Member
Licensed User
Longtime User
where is the code which generates the customtype from the json?
Apologies for delay,
B4X:
Sub  ProcessReturn (job As HttpJob)

    Log("tag"&job.Tag)

    If job.Success = True Then
    
        Log(job.GetString)
        If job.Tag ="origin" Or job.Tag="destination" Then
            Dim jp As JSONParser
            jp.Initialize(job.GetString)
            Dim m As Map = jp.NextObject
        
            Dim results As List = m.Get("features")
            Dim bb As List = m.Get("bbox")
            If results.Size > 0 Then
                Dim first As Map = results.Get(0)
                Dim geometry As Map = first.Get("geometry")
                Dim properties As Map = first.Get("properties")
                    
                'Log("this is the property map"& properties)
                    
                Dim coordinates As List = geometry.Get("coordinates")
                
                If job.Tag="origin" Then
                    Origin.latitude= coordinates.Get(1)
                    Origin.Longitude =coordinates.Get(0)
                    Origin.gid=properties.Get("id")
                    Origin.id=properties.Get("gid")
                    Origin.layer=properties.Get("layer")
                    Origin.source=properties.Get("source")
                    Origin.source_id=properties.Get("source_id")
                    Origin.name=properties.Get("name")
                    Origin.housenumber=properties.Get("housenumber")
                    Origin.street=properties.Get("street")
                    Origin.confidence=properties.Get("confidence")
                    If properties.ContainsKey("distance") Then
                        Origin.distance=properties.Get("distance")
                    End If
                    Origin.accuracy=properties.Get("accuracy")
                    Origin.country=properties.Get("country")
                    Origin.country_gid=properties.Get("country_gid")
                    Origin.country_a=properties.Get("country_a")
                    Origin.region=properties.Get("region")
                    Origin.region_gid=properties.Get("region_gid")
                    Origin.region_a=properties.Get("region_a")
                    Origin.county=properties.Get("county")
                    Origin.county_gid=properties.Get("county_gid")
                    Origin.county_a=properties.Get("county_a")
                    Origin.locality=properties.Get("locality")
                    Origin.locality_gid=properties.Get("locality_gid")
                    Origin.continent=properties.Get("continent")
                    Origin.continent_gid=properties.Get("continent_gid")
                    Origin.label=properties.Get("label")
                    Origin.BoundingBox=bb
                    Origin.requiresGeocoding=False
                    LogColor("Origin SUCCESFULLY SET = " & Origin.label,Colors.Green)
                    ToastMessageShow("Origin Set",True)
                    saveLocation("Origin",Origin)
                    
                    else if job.Tag ="destination" Then
                    
                    Destination.latitude= coordinates.Get(1)
                    Destination.Longitude =coordinates.Get(0)
                    Destination.gid=properties.Get("id")
                    Destination.id=properties.Get("gid")
                    Destination.layer=properties.Get("layer")
                    Destination.source=properties.Get("source")
                    Destination.source_id=properties.Get("source_id")
                    Destination.name=properties.Get("name")
                    Destination.housenumber=properties.Get("housenumber")
                    Destination.street=properties.Get("street")
                    Destination.confidence=properties.Get("confidence")
                    If properties.ContainsKey("distance") Then
                        Destination.distance=properties.Get("distance")
                    End If
                    Destination.accuracy=properties.Get("accuracy")
                    Destination.country=properties.Get("country")
                    Destination.country_gid=properties.Get("country_gid")
                    Destination.country_a=properties.Get("country_a")
                    Destination.region=properties.Get("region")
                    Destination.region_gid=properties.Get("region_gid")
                    Destination.region_a=properties.Get("region_a")
                    Destination.county=properties.Get("county")
                    Destination.county_gid=properties.Get("county_gid")
                    Destination.county_a=properties.Get("county_a")
                    Destination.locality=properties.Get("locality")
                    Destination.locality_gid=properties.Get("locality_gid")
                    Destination.continent=properties.Get("continent")
                    Destination.continent_gid=properties.Get("continent_gid")
                    Destination.BoundingBox=bb
                    Destination.label=properties.Get("label")
                    Destination.requiresGeocoding=False   
                    ToastMessageShow("Destination Set",True)
                    LogColor("DESTINATION SUCCESSFULLY SET  = " & Destination.label,Colors.Green)
                    saveLocation("Destination",Destination)
                End If
                
            End If
        End If
        job.Release
    Else
        Log("job was a failure")
        job.Release
    End If

    job.Release
    Log( "end of job done")
End Sub
 
Upvote 0

jamesnz

Active Member
Licensed User
Longtime User
Origin and Destination are instances of the class GeocodedLocation, both are declared and initialised in main.
I swapped the values for id and Gid, they are ultimately the same but id is prefixed with text, gid is purely numeric,
i have tried commenting out all references to the boundingbox
B4X:
'Origin.BoundingBox=bb
and entirely removing the BoundingBox from GeocodedLocation class yet I still get the same error
 
Upvote 0
Top