Saving Map In Gson - java

I would like to save/load a map/hashmap into gson without using reflection (Java 9 is restricting this). Also not sure if I can still use the TypeToken since it depends on the reflection libary.
private TestMap map = new TestMap();
//Using this as TestMap.class as The Type parm causes ClassCAstException
private class TestMap extends HashMap<Integer, TeleportDialog[]> {}
#Test
public void loadingMapAndCheckingValues(){
File file = new File("./data/saves/teleports/teleports.gson");
Gson gson = new GsonBuilder().create();
Type token = new TypeToken<Map<Integer, TeleportDialog[]>>(){}.getType(); //This Works but violates what I'm after.
try {
Map<Integer, TeleportDialog[]> map = gson.fromJson(new FileReader(file), token);
map.forEach((integer, dialogs) -> {
System.out.println("Key: " + integer);
for(TeleportDialog d : dialogs){
System.out.println(d);
}
});
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

Related

Create, Write, Edit JSON file in Android Studio

I would like to create a JSON file in the internal storage of the phone, to store data.
I want to be able to add objects ("configX") to the file and then read the data.
It should look something like this:
{
"config1": {
"component1": "url",
"component2": "url",
"component3": "url"
},
"config2": {
"component1": "url",
"component2": "url",
"component3": "url"
}
}
I can create a JSON file like this :
public void saveToJson(){
JSONObject json = new JSONObject();
try {
json.put("component1", "url");
json.put("component2", "url");
String jsonString = json.toString();
FileOutputStream fos = this.openFileOutput("jsonfile", Context.MODE_PRIVATE);
fos.write(jsonString.getBytes());
fos.close();
Log.d("JSON" , json.toString());
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
But how to put the components in the config object ? And how to retrieve the data ?
EDIT 1 :
https://stackoverflow.com/a/62474912/11652860
Thanks for the very detailed answer, I'm doing something wrong. I have an Activity where I put and save data to the json file:
public class Data {
private Map<String, Map<String, String>> map;
public Data() {
}
public Data(Map<String, Map<String, String>> map) {
this.map = map;
}
public Map<String, Map<String, String>> getMap() {
return map;
}
public void setMap(Map<String, Map<String, String>> map) {
this.map = map;
}
}
Map<String, String> config1 = new HashMap<>();
config1.put("component1", "url1");
config1.put("component2", "url1");
config1.put("component3", "url1");
Map<String, Map<String, String>> map = new HashMap<>();
map.put("config1", config1);
Data data = new Data(map);
Gson gson = new Gson();
String json = gson.toJson(data);
FileOutputStream fos = null;
try {
fos = webViewActivity.this.openFileOutput("jsonfile", Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fos.write(json.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
And a fragment where I load the data :
public class Data {
private Map<String, Map<String, String>> map;
public Data() {
}
public Data(Map<String, Map<String, String>> map) {
this.map = map;
}
public Map<String, Map<String, String>> getMap() {
return map;
}
public void setMap(Map<String, Map<String, String>> map) {
this.map = map;
}
}
public void load(){
FileInputStream fis = null;
try {
fis = getContext().openFileInput("jsonfile.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String text;
while ((text = br.readLine()) != null){
sb.append(text).append("\n");
Gson gson = new Gson();
String json = gson.toJson(text);
Data data = gson.fromJson(json, Data.class);
String url = data.getMap().get("config1").get("component1");
frameTV.setText(url);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
The saving and loading parts must be wrong, but they worked for getting text out a text file
EDIT 2 :
I found the problem, I wasn't loading and saving properly :
SAVING:
String filename = "jsonfile.txt";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(json.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
LOADING :
FileInputStream fis = getContext().openFileInput("jsonfile.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
String json = sb.toString();
Gson gson = new Gson();
Data data = gson.fromJson(json, Data.class);
String priceURL = data.getMap().get("config1").get("url1");
EDIT 3 :
My problem now is that I need to create the file once and then check if the file exists, if it does I need to check if config1 exists if it doesn't I need to put config in the file.
But I can't check if config1 exists because I get : java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Map com.a.app.ui.app.appFragment$Data.getMap()
I check if it exists by doing :
Boolean configTest = data.getMap().containsKey("config1");
if(!configTest){}
How can I create the file and check the data without getting a NullPointerException ?
Thank you for helping me !
Google's Gson library will be helpful in this case.
Add dependency for Google Gson in your radle file.
dependencies {
implementation 'com.google.code.gson:gson:2.8.6'
}
Create a class for your data container
public class Data {
private Map<String, Map<String, String>> map;
public Data() {
}
public Data(Map<String, Map<String, String>> map) {
this.map = map;
}
public Map<String, Map<String, String>> getMap() {
return map;
}
public void setMap(Map<String, Map<String, String>> map) {
this.map = map;
}
}
Add data to your class
Map<String, String> config1 = new HashMap<>();
config1.put("component1", "url1");
config1.put("component2", "url1");
config1.put("component3", "url1");
Map<String, String> config2 = new HashMap<>();
config2.put("component1", "url1");
config2.put("component2", "url1");
config2.put("component3", "url1");
Map<String, Map<String, String>> map = new HashMap<>();
map.put("config1", config1);
map.put("config2", config2);
Data data = new Data(map);
Get gson from data class
Gson gson = new Gson();
String json = gson.toJson(data);
You can now save this json in a file in a text format.
Now when reading, load the content of the text file in a String say 'jsonString'.
Deserialize the jsonString to Java Object
Data data = gson.fromJson(json, Data.class);
Access configurations
String url = data.getMap().get("config1").get("component1");
Add new configurations
Map<String, String> config3 = new HashMap<>();
config3.put("component1", "url1");
config3.put("component2", "url1");
config3.put("component3", "url1");
data.getMap().put("config3", config3);
Follow again these steps to save configs
Or You can manually edit the text file to add configs according to the predefined format.
{
"maps":{
"config2":{
"component1":"url1",
"component2":"url1",
"component3":"url1"
},
"config1":{
"component1":"url1",
"component2":"url1",
"component3":"url1"
}
}
}
This is how you create multiple Objects in a single JSON object:
//Creating first Object
JSONObject config1 = new JSONObject();
try {
json.put("component1", "url");
json.put("component2", "url");
json.put("component2", "url");
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Creating second object
JSONObject config2 = new JSONObject();
try {
json.put("component1", "url");
json.put("component2", "url");
json.put("component2", "url");
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject finalJSON = new JSONObject();
try {
//Adding both objects in one single object
json.put("config1", config1);
json.put("config2", config2);
String jsonString = finalJSON.toString();
FileOutputStream fos = this.openFileOutput("jsonfile", Context.MODE_PRIVATE);
fos.write(jsonString.getBytes());
fos.close();
Log.d("JSON" , json.toString());
} catch (IOException | JSONException e) {
e.printStackTrace();
}
This will give you the desired output. Also, if in case you want to make any object an array, you can use JSONArray for that.
Please consider using https://github.com/google/gson. You will be working with class instance rather than with JSONObject. Much more convenient.
Just to give you the idea of what you can do:
public class TestClass {
private final Map<String, String> config1;
private final Map<String, String> config2;
public TestClass(Map<String, String> config1, Map<String, String> config2) {
this.config1 = config1;
this.config2 = config2;
}
}
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Map<String, String> config1 = new HashMap<String, String>();
config1.put("hello1.1", "world1.1");
config1.put("hello1.2", "world1.2");
Map<String, String> config2 = new HashMap<String, String>();
config2.put("hello2.1", "world2.1");
config2.put("hello2.2", "world2.2");
TestClass testClass = new TestClass(config1, config2);
Log.d("zzz", gson.toJson(testClass));
The above prints:
{
"config1": {
"hello1.1": "world1.1",
"hello1.2": "world1.2"
},
"config2": {
"hello2.1": "world2.1",
"hello2.2": "world2.2"
}
}
You can go back and force between json string and the entity itself. To edit, you only need to work with object - natural and convenient way.

Java Nashorn build json array object in java can't find way

I try to create java json array ,
can't find any way to create them using Nashorn
i can create simple objects ...
private void createJsonObject() {
try {
final Map<String, Object> newMap = new HashMap<>();
newMap.put("foo",1);
newMap.put("bar", true);
ScriptObjectMirror json = (ScriptObjectMirror) this.engine.eval("JSON");
json.putAll(newMap);
this.engine.put("jsonObject", json);
String result = (String) this.engine.eval("JSON.stringify(jsonObject)");
System.out.println(result);
} catch (ScriptException e) {
e.printStackTrace();
}
}
Result : {"bar":true,"foo":1}
Here i try to create array but im getting empty json
private void createJsonObject() {
try {
List<String> returnList = new ArrayList<>();
returnList.add("x");
returnList.add("y");
ScriptObjectMirror json = (ScriptObjectMirror) this.engine.eval("JSON");
json.put("test",returnList);
this.engine.put("jsonObject", json);
String result = (String) this.engine.eval("JSON.stringify(jsonObject)");
System.out.println(result);
} catch (ScriptException e) {
e.printStackTrace();
}
}
Result: {}
The end goal is to build array of objects in memory using java native tools without using dependencies

How to use Map as dataset element in JasperReports?

I'm passing a parameter to Jasper which is a collection of Maps. I would like to use this collection as dataset for a table.
If I had a collection of normal Java objects I would do:
$F{description}
to get the field. I would like to do something like:
get(description)
or
values().get(1)
Is it possible to retrieve a map value in a table like this?
As I really wanted to keep the service generic I decided for dynamic class creation with Javassist. It's an ugly solution but at least it works.
HashMap<String, Object> element = (HashMap<String, Object>) ((ArrayCollection) v).get(0);
ClassPool pool = ClassPool.getDefault();
CtClass ctClass = pool.makeClass("Dynamic"+System.currentTimeMillis());
element.keySet().forEach(s -> {
try {
//need only Strings
ctClass.addField(CtField.make("public String "+s+";", ctClass));
ctClass.addMethod(CtMethod.make("public String get" + StringUtils.capitalize(s) + "() { return " + s + "; }", ctClass));
} catch (Exception e) {
e.printStackTrace();
}
});
Class clazz = ctClass.toClass();
ArrayList<Object> objects = new ArrayList<>();
((Collection) v).forEach(m -> {
HashMap<String, Object> hm = (HashMap<String, Object>) m;
try {
Object obj = clazz.newInstance();
element.keySet().forEach(s -> {
try {
obj.getClass().getDeclaredField(s).set(obj, hm.get(s));
} catch (Exception e) {
e.printStackTrace();
}
});
objects.add(obj);
} catch (Exception e) {
e.printStackTrace();
}
});
parameters.put(k, new JRBeanCollectionDataSource(objects));

Rename map keys in GSON according to FieldNamingPolicy

Say I'm building my GSON object like this
new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
and now I want to deserialize the following JSON
{
"MyMap" : {
"Key1" : "Foo",
"Key2" : "Bar"
}
}
into the following class (which is working just fine)
public class MapClass {
Map<String,String> myMap;
}
but I also would like the keys to be named "key1" and "key2". How would I go about this?
You can try in this way:
try {
JSONObject jObj = new JSONObject("{"
+ " \"MyMap\" : {"
+ " \"Key1\" : \"Foo\","
+ " \"Key2\" : \"Bar\""
+ " }"
+ "}"); // this parses the json
JSONObject jObjt = jObj.getJSONObject("MyMap");
//old version with out GSON
Map<String, String> map = new HashMap();
Iterator itr = jObjt.keys();
while (itr.hasNext()) {
String key = (String) itr.next();
String value = jObjt.getString(key);
map.put(key, value);
}
//desalinized one with GSON
Map<String, String> map1 = new Gson().fromJson(jObjt.toString(), HashMap.class);
for (String str : map1.keySet()) {
System.out.println("k:" + str + " v:" + map1.get(str));
}
} catch (JSONException ex) {
//log the error
}
FieldNamingPolicy is applied to fields of json value. It is not possible to apply this to maps' keys (a map which has key, value pairs) at json.
Easy solution:
After deserialization, iterate over your map and rename key names. i.e. Key1 to key1 and Key2 to key2.
Other solution:
Write a custom TypeAdapter which handles deserialization process and renames keys.
public class MapClassTypeAdapter extends TypeAdapter<MapClass> {
#Override
public MapClass read(JsonReader in) throws IOException {
final MapClass mapClassInstance = new MapClass();
mapClassInstance.myMap = new HashMap<String, String>();
in.beginObject();
if("myMap".equalsIgnoreCase(in.nextName())) {
in.beginObject();
while (in.hasNext()) {
String key = in.nextName();
// You want keys as camel case
String newKey = key.substring(0,1).toLowerCase() + key.substring(1);
String value = in.nextString();
mapClassInstance.myMap.put(newKey, value);
}
in.endObject();
}
in.endObject();
return mapClassInstance;
}
#Override
public void write(JsonWriter out, MapClass mapClass) throws IOException {
throw new RuntimeException("MapClassTypeAdapter.write method not implemented yet!");
}
}
Test Other solution:
String json = "{\"myMap\":{\"Key1\":\"Foo\",\"Key2\":\"Bar\"}}";
final GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(MapClass.class, new MapClassTypeAdapter());
final Gson gson = gsonBuilder.create();
MapClass mapClass = gson.fromJson(json, MapClass.class);

How i can recover a Map of objects from json?

I have this class
public static class SomeClass {
public SomeClass(String field) {
this.field = field;
}
private final String field;
public String getField() {
return field;
}
}
I have also this test (edited)
#Test
public void testStringifyMapOfObjects() {
Map<String, SomeClass> original = Maps.newTreeMap();
original.put("first", new SomeClass("a"));
original.put("second", new SomeClass("b"));
String encoded = JsonUtil.toJson(original);
Map<String, SomeClass> actual = JsonUtil.fromJson(encoded, Map.class);
Assert.assertEquals("{'first':{'field':'a'},'second':{'field':'b'}}", encoded.replaceAll("\\s", "").replaceAll("\"", "'"));
Assert.assertEquals(original.get("first"), actual.get("first"));
}
The test fails with
junit.framework.AssertionFailedError: expected:<eu.ec.dgempl.eessi.facade.transport.test.TestToolTest$SomeClass#6e3ed98c> but was:<{field=a}>
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.Assert.failNotEquals(Assert.java:277)
at junit.framework.Assert.assertEquals(Assert.java:64)
at junit.framework.Assert.assertEquals(Assert.java:71)
at eu.ec.dgempl.eessi.facade.transport.test.TestToolTest.testStringifyMapOfObjects(TestToolTest.java:90)
Can I make json to properly serialize objects as the values of the map or should I use something else?
edited
public class JsonUtil {
private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(JsonUtil.class);
public static <T> String toJson(T data) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(Feature.INDENT_OUTPUT, true);
try {
return mapper.writeValueAsString(data);
} catch (IOException e) {
LOG.warn("can't format a json object from [" + data + "]", e);
return null;
}
//
// return Json.stringify(Json.toJson(data));
}
public static <T> T fromJson(String description, Class<T> theClass) {
try {
JsonNode parse = new ObjectMapper().readValue(description, JsonNode.class);
T fromJson = new ObjectMapper().treeToValue(parse, theClass);
return fromJson;
} catch (JsonParseException e) {
// throw new RuntimeException("can't parse a json object of type " + theClass.getName() + " from [" + description + "]", e);
LOG.warn("can't parse a json object from [" + description + "]", e);
return null;
} catch (JsonMappingException e) {
// throw new RuntimeException("can't parse a json object of type " + theClass.getName() + " from [" + description + "]", e);
LOG.warn("can't parse a json object from [" + description + "]", e);
return null;
} catch (IOException e) {
// throw new RuntimeException("can't parse a json object of type " + theClass.getName() + " from [" + description + "]", e);
LOG.warn("can't parse a json object from [" + description + "]", e);
return null;
}
}
}
You are running into a problem related to Java generics. To summarize, when deserializing data into a non-reifiable type (aka a type for which actual type information is not available at runtime) you need to use a supertype token. You can get more detail about what a supertype token is (and why you need to use one) by reading these SO posts:
Pass parameterized type to method as argument
Error using Jackson and JSON
Deserialize JSON to ArrayList using Jackson
And also from the Jackson documentation:
Data Binding With Generics
TypeReference Javadoc
The basic problem is that when you use a typical generic object, the actual type parameters for the object aren't available at runtime. Therefore Jackson doesn't know which actual class to instantiate and deserialize your data into.
The easiest way to get around the problem would be adding an overload to your JSON utility class, that accepts a type reference (as opposed to a Class<T>). For example:
public static <T> T fromJson(String json, TypeReference<T> typeRef) {
if(json == null || typeRef == null) return null;
return new ObjectMapper().readValue(json, typeRef);
}
To be used as such:
Map<String, SomeClass> actual = JsonUtil.fromJson(
encoded,
new TypeReference<Map<String, SomeClass>>(){});
I discovered that the simplest solution is to create a "container" class that will contain the map. This is working probably because the container has enough type details for the map, as opposed to the case when a map is used directly.
public static class SomeClass {
private final String field;
private SomeClass() {
this("wrong");
}
public SomeClass(String field) {
this.field = field;
}
public String getField() {
return field;
}
#Override
public String toString() {
return "SomeClass[" + field + "]";
}
}
public static class SomeClassContainer {
private final Map<String, SomeClass> all = Maps.newTreeMap();
public Map<String, SomeClass> getAll() {
return all;
}
}
After this ... the updated test is
#Test
public void testStringifyMapOfObjects() {
SomeClassContainer original = new SomeClassContainer();
original.getAll().put("first", new SomeClass("a"));
original.getAll().put("second", new SomeClass("b"));
String encoded = JsonUtil.toJson(original);
System.out.println(encoded);
SomeClassContainer actual = JsonUtil.fromJson(encoded, SomeClassContainer.class);
System.out.println(ObjectUtils.toString(actual));
Assert.assertEquals("{'all':{'first':{'field':'a'},'second':{'field':'b'}}}", encoded.replaceAll("\\s", "").replaceAll("[\"]", "'"));
Assert.assertEquals("class eu.ec.dgempl.eessi.facade.transport.test.TestToolTest$SomeClass", actual.getAll().get("first").getClass().toString());
Assert.assertEquals(original.getAll().get("first").toString(), actual.getAll().get("first").toString());
Assert.assertEquals(original.getAll().get("second").toString(), actual.getAll().get("second").toString());
}

Categories