Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing ordered List in Gson

I'm using Gson library in Android. I retrieve some data from web service, in json format.

So I get the data I do the following:

Gson gson = new GsonBuilder().create();
ClassA obj = gson.fromJson(returnFromServer.toString(), ClassA.class);

where the class ClassA is:

public class ClassA{
    private String id;
    private List<ClassB> myList;
}

So I have a List of custom object inside obj.

The question is: can I be sure that the order of the list is the same of the order of the element in the json file? If not, is there any mechanism inside Gson that allow to maintain the same order of json file?

NOTE: all the previous attempt produce a list sorted in the same order of the json file.

like image 585
GVillani82 Avatar asked Nov 11 '22 10:11

GVillani82


1 Answers

You can use LinkedList implementation to maintain the sort order, however i created a simple program, the order is maintained even in ArrayList. You can check the code below,

I created ClassB.java

package com.zack.demo;

public class ClassB {

    private String name;

    private int age;

    private String gender;


    public ClassB(String name, int age, String gender)
    {
        this.name = name;
        this.age = age;
        this.gender = gender;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

}

ClassA.java

package com.zack.demo;

import java.util.List;

public class ClassA {

    private String id;
    private List<ClassB> myList;

    public ClassA(String id, List<ClassB> myList)
    {
        this.id = id;
        this.myList = myList;
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public List<ClassB> getMyList() {
        return myList;
    }
    public void setMyList(List<ClassB> myList) {
        this.myList = myList;
    }



}

Sample test program for the list order

package com.zack.demo;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.google.gson.Gson;

public class GsonTest {

    public static void main(String[] args) {

        Gson gson = new Gson();


        ClassB o2 = new ClassB("Test2", 20, "Male");
        ClassB o1 = new ClassB("Test1", 10, "Male");


        List<ClassB> ls = new LinkedList<ClassB>();

        ls.add(o2);
        ls.add(o1);

        ClassA  a1 = new ClassA("T1", ls);

        String result = gson.toJson(a1, a1.getClass());

        System.out.println(result);

        ClassA obj = gson.fromJson(result.toString(), ClassA.class);

        System.out.println(gson.toJson(obj, obj.getClass()));




    }

}

Results

{"id":"T1","myList":[{"name":"Test2","age":20,"gender":"Male"},{"name":"Test1","age":10,"gender":"Male"}]}
{"id":"T1","myList":[{"name":"Test2","age":20,"gender":"Male"},{"name":"Test1","age":10,"gender":"Male"}]}
like image 133
Zack Dawood Avatar answered Nov 13 '22 09:11

Zack Dawood