I am currently trying serialze and correctly deserialize an Object containing a List with tuples using googles Gson. I have found similar questions here here and here but I wasnt able to adjust their solutions to my problem, because my List is in an Object.
Here a quick example:
import com.google.gson.*;
import java.util.*;
public class SerializeTheCity {
public static void main(String[] args) {
HashMap <String, Integer> cityMap = new HashMap <>();
cityMap.put("Street A", 4);
cityMap.put("Street B", 3);
cityMap.put("Street C", 7);
cityMap.put("Street D", 8);
cityMap.put("Street E", 9);
City someCity = new City();
someCity.streets= new ArrayList<>();
someCity.streets.addAll(cityMap.entrySet());
System.out.println(someCity.streets.get(1).getValue()); //works fine, how do I serialize it?
Gson gson = new Gson();
String saveCity = gson.toJson(someCity);
System.out.println(saveCity); //does not work (empty List)
// here I tried to use a solution [link 1] that worked for a similar question.
Gson gson2 = new Gson();
JsonElement jsonTree = gson2.toJsonTree(cityMap, Map.class);
JsonObject jsonObject = new JsonObject();
jsonObject.add("city", jsonTree);
System.out.println("city = "+jsonObject); //how do I deserialize this into an object of city?
City thisCity = gson.fromJson(jsonObject, City.class);
System.out.println("streets = "+thisCity.streets); // doesnt work
//works like in [link 1]. But its not a city-object.
HashMap <String, Integer> thisStreets = gson.fromJson(jsonObject.get("city"), HashMap.class);
System.out.println("this streets = "+thisStreets);
System.out.println("is this street A?"+thisStreets.get("Street A"));
// this works, but I would like having the streets
//in a city object (I could build a new city object like "newCity.streets=thisStreets",
//but perhaps you know a smarter solution)
}
}
class City {
List<Map.Entry<String,Integer>> streets; //Street, HouseNumber
}
Thank you for your help.