📜 ⬆️ ⬇️

Gson cheat sheet for Map, List and Array

Constantly confronted with parsing, Json is always peeping in old projects or on the encountered implementation of an object on stackoverflow.com .

I decided to collect three basic types in the Map, List, Array cheat sheet.

Type itemsMapType = new TypeToken<Map<Integer, GoodsItem>>() {}.getType(); Type itemsListType = new TypeToken<List<GoodsItem>>() {}.getType(); Type itemsArrType = new TypeToken<GoodsItem[]>() {}.getType(); 

')
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 performed using the Gson library. The GoodsItem class is considered as the “test subject”.

Hashmap

 Map<Integer, GoodsItem> mapItems = new HashMap<Integer, GoodsItem>(); 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); 

Result Serialization:

 { "1":{"name":"Samsung","price":51200.6}, "2":{"name":"Lg","price":5400.6}, "3":{"name":"Alcatel","price":4500.6} } 

 Map<Integer, GoodsItem> mapItemsDes = new Gson().fromJson(jsonStr, itemsMapType); System.out.println(mapItemsDes.toString()); 

Deserialization result:

 {1=Samsung : 51200.6, 2=Lg : 5400.6, 3=Alcatel : 4500.6} 


ArrayList

 List<GoodsItem> listItems = new ArrayList<GoodsItem>(); 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); 

Result Serialization:

 [ {"name":"Samsung","price":51200.6}, {"name":"Lg","price":5400.6}, {"name":"Alcatel","price":4500.6} ] 

 List<GoodsItem> listItemsDes = new Gson().fromJson(jsonStr,itemsListType); System.out.println(listItemsDes.toString()); 

Deserialization result:

 [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); 

Result Serialization:

 [ {"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)); 

Deserialization result:

 [Samsung : 51200.6, Lg : 5400.6, Alcatel : 4500.6] 


As you can see, ArrayList and a simple array of objects are converted to the same Json string.

Useful tools:
parcelabler.com makes it possible by class type to generate parcelable methods for Android. 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<GoodsItem> CREATOR = new Parcelable.Creator<GoodsItem>() { @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.

Source: https://habr.com/ru/post/253266/


All Articles