0

I upgraded to Firebase 9.0.2. When saving to the Realtime Database I cannot get objects to map like they used to in older versions. See my sample project demonstrating this.

For instance, take these objects:

public class Vehicle {

    private String vin;
    private int wheels;
    private int color;
    private double price;
    private String type;

    public Vehicle() { }

    ....
    //public accessors
    .... 
}

and

public class Truck extends Vehicle {

    private int bedLength;
    private boolean fourWheelDrive;
    private boolean diesel;

    public Truck() { }

    ....
    //public accessors
    .... 
}

If I were to create and save a Truck object:

final Truck truck = new Truck();
truck.setVin(getRef().push().getKey());
truck.setVehicleType("truck");
truck.setColor(random.color());
truck.setPrice(random.price());
truck.setBedLength(random.number(8));
truck.setDiesel(random.bool());
truck.setFourWheelDrive(random.bool());
truck.setVehicleType(VehicleType.TRUCK);

FirebaseDatabase
     .getInstance()
     .getReference()
     .child(vehicle.getVin())
     .setValue(truck);

I only see values from Truck, and none from Vehicle:

"-KKpeYZO4eG3lAYyCd40" : {
    "bedLength" : 1,
    "diesel" : true,
    "fourWheelDrive" : true
}

I am considering making a JSON mapper and manually map all of my objects every time I save or retrieve(Like older versions of Firebase used to do). If there are better solutions or plans to fix this I would really like to know before I start down that rabbit hole.

In the meantime, I have followed the advice from this answer, which that makes for ugly, redundant, and bug-prone code.


Edit:

This solution from Firebase engineer, Frank van Puffelen did not work for me because I believe that it is geared towards deserializing objects, where I am struggling with serializing.

Community
  • 1
  • 1
Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
  • See Kato's comment at this [related question/answer](http://stackoverflow.com/questions/37331293/firebase-java-object-serialization-and-inheritance). – Bob Snyder Jun 22 '16 at 02:52
  • Yeah, I have seen that question. It was no help because there is no solution. – Chad Bingham Jun 22 '16 at 02:59

0 Answers0