Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deserialize nested Firebase database in Java with Map<String, Obect> or List <object> class?

Here is my simple primary Firebase data.

{
  "elephant" : {
    "color" : "gray",
    "lifespan" : 48,
    "status" : "endangered"
  }
}

In my android project I made this class to represent this data

public class elephant{
    private String color;
    private int lifespan;
    private String status;

    // Constructors
    public elephant(){
    }

    public elephant(String color, int lifespan, String status) { . . . }

    // Getters and Setters
    public String getColor() { return color; }

    public void setColor(String color) { this.color = color; }

    public int getLifespan() { return lifespan; }

    public void setLifespan(int lifespan) { this.lifespan = lifespan; }

    public String getStatus() { return status; }

    public void setStatus(String status) { this.status = status; }
}

That way I can call getValue(elephant.class) method to retrieve this data.

Elephant ele = datasnapshot.getValue(elephant.class);

It works.

Now I'm adding one nested node in my project.

{
  "elephant" : {
    "color" : "gray",
    "lifespan" : 48,
    "status" : "endangered",
    "dimension" : {
      "height" : 270,
      "length" : 607
    }
  }
}

database Image

I've created a new class called Dimension.java in my project:

public class Dimension {
    private int length, height;

    // Constructors
    public Dimension() { }

    public Dimension(int length, int height) { . . . }

    // Public getters and setters
    public int getLength() {return length;}

    public void setLength(int length) {this.length = length;}

    public int getHeight() {return height;}

    public void setHeight(int height) {this.height = height;}
}

And I modified my elephant.java to include this variable/object:

private Dimension dimension;

and proper constructor, getters and setters for it.

and my previous method still works. It returns the proper java representation for the whole datasnapshot.

Elephant ele = datasnapshot.getValue(elephant.class);

and, If I get a datasnapshot at DatabaseRef.child('dimension') I can pass the dimension.class to getValue() method to get proper data:

Dimension data = datasnapshot.getValue(dimension.class);

Thus these multipurpose classes are pretty convenient to me.

Now, I know, Firebase data is supposed to be normalized/flattened. But for experiments sake let's say I've added some data with pushID.

{
  "elephant" : {
    "color" : "gray",
    "status" : "endangered",
    "lifespan" : 48,
    "dimension" : {
      "height" : 270,
      "length" : 607
    },
    "population" : {
      "-KTcpw_SsMb55qLc6Xme" : {
        "count" : 210,
        "location" : "Bangladesh"
      },
      "-KTcq6YeB3kX3h9F9xPU" : {
        "count" : 428,
        "location" : "Bhutan"
      },
      "-KTcqDySLBAFtCuUc3R4" : {...},
      "-KTcqMyy4GULKo6FqPEM" : {...},
      "-KTcqWwtli672ROpry8_" : {...},
      "-KTcqhnBb6dTN1mA1otW" : {...}
    }
  }
}

Database Screenshot

Now, If I want to make previous code work for this case:

Elephant ele = datasnapshot.getValue(elephant.class);

How do I write my new class for the 'population' node.

In Firebase reference it is said that it returns a Map<String, Object> or List<Object> java equivalent. But being a beginner in Java I'm stuck with this problem in past 4 days.

I can iterate through the dataSnapshot.child('population') and add them in a List of separate 'population' object. But I've wondered if it can be achieved in this way. Please help me with the code or link me to a example where it was done in one line and automatically. It'll be such a relief.

like image 628
BorhnN Avatar asked Oct 11 '16 02:10

BorhnN


2 Answers

You can try this one:

class PopulationDetail {
    long count;
    String location;
}

class Elephan {
    ...
    Map<String, PopulationDetail> population;
    ...
}
like image 51
Akivamu Avatar answered Nov 05 '22 11:11

Akivamu


To convert json into pojo class, try this -

http://www.jsonschema2pojo.org/

like image 45
developerick Avatar answered Nov 05 '22 12:11

developerick