-1

i use Firebase as backend for my app. But i cant serialize PolylineOptions from Firebase back to a PolylineOptions type. If i try to convert it back with dataSnapshot.getValue(PolylineOptions.class); , i have the following error message:

com.firebase.client.FirebaseException: Failed to bounce to type
....  
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: 
Unrecognized field "width" (class com.google.android.gms.maps.model.PolylineOptions), not marked as ignorable (one known property: "points"])
....
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:555)

How can I fix that problem?

Here is the code which demonstrates the issue:

import android.location.Location;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.firebase.client.Query;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.android.gms.maps.model.LatLng;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;


public class MainActivity extends AppCompatActivity{

    PolylineOptions mPolylineOptions;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPolylineOptions = new PolylineOptions();

        Location targetLocation = new Location("name");
        targetLocation.setLatitude(1.0d);
        targetLocation.setLongitude(2.0d);

        mPolylineOptions.add(new LatLng(targetLocation.getLatitude(), targetLocation.getLongitude()));
        mPolylineOptions.color(ContextCompat.getColor(this, R.color.colorPrimary));

        Firebase.setAndroidContext(this);
        Firebase ref = new Firebase("https://xxxxxxx");
        Firebase testRef = ref.child("test");

        final Query queryRef = testRef.orderByKey();
        queryRef.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.d("Test", "data change");
                PolylineOptions activity1 = dataSnapshot.getValue(PolylineOptions.class);
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                System.out.println("The read failed: " + firebaseError.getMessage());
            }
        });
        testRef.setValue(mPolylineOptions);
    }
}

Here is the JSON from Firebase:

Here is the JSON from Firebase:

{
  "test" : {
    "clickable" : false,
    "color" : -12627531,
    "geodesic" : false,
    "points" : [ {
      "latitude" : 50.68468468468468,
      "longitude" : 8.190167190445726
    } ],
    "visible" : true,
    "width" : 10.0,
    "zindex" : 0.0
  }
}
M. Gron
  • 1
  • 2
  • As the error message says: your Java object has a `field` while your JSON has a `points`. There's nothing more we can say here, without seeing some more useful information. Add a snippet of your JSON as text (no screenshot), which you can easily get by using the Export button in your Firebase dashboard Then also add the code that you use to read that JSON from the database. See [MCVE](http://stackoverflow.com/help/mcve). – Frank van Puffelen Mar 27 '16 at 15:34
  • Have pasten a full code example to the Top post. Also i have posted the JSON form Firebase... – M. Gron Mar 27 '16 at 15:47
  • Note that the M in MCVE stands for **minimal** . I doubt the location handling itself is relevant to the exception. So if you remove that from the question (while still keeping the code working and reproducing the problem), you're saving us the effort of understanding what it does. The easier you make it for us to help you, the more likely it is that someone will help. – Frank van Puffelen Mar 27 '16 at 15:51
  • You have a class `PolylineOptions` that you use to read the JSON. You're clearly missing some properties either in that class or in the JSON. But again: without seeing the class, it's impossible to say more (although that `points` array looks like a world of problems at first glance). – Frank van Puffelen Mar 27 '16 at 15:54
  • Added a shorter code again. Which class do you mean? I thought PolylineOptions is a model class because the package name is com.google.android.gms.maps.model. And with model classes i can serialize and deserialize!? – M. Gron Mar 27 '16 at 16:17
  • Serialization/deserialization depends on some very strict requirements. 1. each property in the JSON must have a corresponding field (+potentially a getter method) in the Java code. 2. each public field/property in the Java code will be serialized to JSON. The error message is an explicit signal that the `PolylineOptions` does not match those requirements. Also see http://stackoverflow.com/questions/32108969/why-do-i-get-failed-to-bounce-to-type-when-i-turn-json-from-firebase-into-java – Frank van Puffelen Mar 27 '16 at 16:48

2 Answers2

1

The minimum classes needed to read your JSON are:

public static class LatLon {
    public double latitude;
    public double longitude;
}
public static class Test {
    public boolean clickable;
    public long color;
    public boolean geodesic;
    public LatLon[] points;
    public boolean visible;
    public double width;
    public double zindex;
}

I wouldn't necessarily call these a best practice as they are more like C structs than classes. To make them slightly cleaner:

  • make all the fields private instead if public
  • add a getter method for each field (so getLatitude() and getLongitude() for class LatLon)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

I have coded a liitle bit and here are my functionable models:

public class PolylineOptions {
    public boolean clickable;
    public long color;
    public boolean geodesic;
    public LatLng[] points = new LatLng[1];;
    public boolean visible;
    public double width;
    public double zindex;

    public PolylineOptions(){

    }

    public void add(LatLng point){
        points[0] = point;
    }
}


public class LatLng {
    public double latitude;
    public double longitude;

    public LatLng(double latitude, double longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public LatLng(){

    }
}

But very often i get arror messages like that:

Error:(32, 32) error: incompatible types: app.firebasetest.PolylineOptions cannot be converted to com.google.android.gms.maps.model.PolylineOptions

on lines like:

GoogleMaps mGoogleMap.addPolyline(mPolylineOptions);

I dont need GoogleMaps objects in Firebase so i dont need to write a model for that type too. How can i persuade my app to assume my model too?

M. Gron
  • 1
  • 2