
Gson cheat sheet for Map, List and Array
Constantly confronted with parsing, Json always peeps at old projects or at the encountered object implementation on stackoverflow.com .
I decided to collect three main types in a cheat sheet Map, List, Array.
Serialization / Deserialization class operations are considered:
Serialization / Deserialization is done using the Gson library. The GoodsItem class is considered as the “test subject”.
Hashmap
Serialization Result:
Result of Deserialization:
Arraylist
Serialization Result:
Result of Deserialization:
Array
Serialization Result:
Result of Deserialization:
As you can see, an ArrayList and a simple array of objects are converted to the same Json string.
Useful tools:
parcelabler.com enables you to generate parcelable methods for Android by class type. For example, for the GoodsItem class:
jsonschema2pojo.org - Generate Plain Old Java Objects from JSON or JSON-Schema.
It would be interesting to know what you use in your projects.
I decided to collect three main types in a cheat sheet Map, List, Array.
Type itemsMapType = new TypeToken
Serialization / Deserialization class operations are considered:
public class GoodsItem{
String name;
float price;
public GoodsItem(String name, float price) {
this.name = name;
this.price = price;
public String toString(){
return name + " : " + price;
}
}
Serialization / Deserialization is done using the Gson library. The GoodsItem class is considered as the “test subject”.
Hashmap
Map mapItems = new HashMap();
mapItems.put(1, new GoodsItem("Samsung", 51200.6f));
mapItems.put(2, new GoodsItem("Lg", 5400.6f));
mapItems.put(3, new GoodsItem("Alcatel", 4500.6f));
String jsonStr = new Gson().toJson(mapItems);
System.out.println(jsonStr);
Serialization Result:
{
"1":{"name":"Samsung","price":51200.6},
"2":{"name":"Lg","price":5400.6},
"3":{"name":"Alcatel","price":4500.6}
}
Map mapItemsDes = new Gson().fromJson(jsonStr, itemsMapType);
System.out.println(mapItemsDes.toString());
Result of Deserialization:
{1=Samsung : 51200.6, 2=Lg : 5400.6, 3=Alcatel : 4500.6}
Arraylist
List listItems = new ArrayList();
listItems.add( new GoodsItem("Samsung" , 51200.6f));
listItems.add( new GoodsItem("Lg" , 5400.6f));
listItems.add( new GoodsItem("Alcatel" , 4500.6f));
String jsonStr = new Gson().toJson(listItems);
System.out.println(jsonStr);
Serialization Result:
[
{"name":"Samsung","price":51200.6},
{"name":"Lg","price":5400.6},
{"name":"Alcatel","price":4500.6}
]
List listItemsDes = new Gson().fromJson(jsonStr,itemsListType);
System.out.println(listItemsDes.toString());
Result of Deserialization:
[Samsung : 51200.6, Lg : 5400.6, Alcatel : 4500.6]
Array
GoodsItem[] arrItems = new GoodsItem[3];
arrItems[0] = new GoodsItem("Samsung", 51200.6f);
arrItems[1] = new GoodsItem("Lg", 5400.6f);
arrItems[2] = new GoodsItem("Alcatel", 4500.6f);
String jsonStr = new Gson().toJson(arrItems);
System.out.println(jsonStr);
Serialization Result:
[
{"name":"Samsung","price":51200.6},
{"name":"Lg","price":5400.6},
{"name":"Alcatel","price":4500.6}
]
GoodsItem[] arrItemsDes = new Gson().fromJson(jsonStr, itemsArrType);
System.out.println(Arrays.toString(arrItemsDes));
Result of Deserialization:
[Samsung : 51200.6, Lg : 5400.6, Alcatel : 4500.6]
As you can see, an ArrayList and a simple array of objects are converted to the same Json string.
Useful tools:
parcelabler.com enables you to generate parcelable methods for Android by class type. For example, for the GoodsItem class:
public class GoodsItem implements Parcelable
public class GoodsItem implements Parcelable {
String name;
float price;
public GoodsItem(String name, float price) {
this.name = name;
this.price = price;
public String toString(){
return name + " : " + price;
}
protected GoodsItem(Parcel in) {
name = in.readString();
price = in.readFloat();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeFloat(price);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
@Override
public GoodsItem createFromParcel(Parcel in) {
return new GoodsItem(in);
}
@Override
public GoodsItem[] newArray(int size) {
return new GoodsItem[size];
}
};
}
jsonschema2pojo.org - Generate Plain Old Java Objects from JSON or JSON-Schema.
It would be interesting to know what you use in your projects.