The following code is working
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
class DataObject<T> {
private int data1 = 100;
private String data2 = "hello";
DataObject child;
private List<String> list = new ArrayList<String>() {
{
add("String 1");
add("String 2");
add("String 3");
}
};
private Map<String, DataObject> data=null;
public DataObject(int i){
this.data1 = i;
this.data = new HashMap<String, DataObject>();
}
//getter and setter methods
@Override
public String toString() {
return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list="
+ list + "]";
}
public Map<String, DataObject> getData() {
return data;
}
public void addData(final String key, DataObject value, Class<T> t) {
data.put(key, value);
}
}
public class test {
/**
* @param args
*/
public static void main(String[] args) {
DataObject obj = new DataObject(12);
obj.child = new DataObject(25);
obj.addData("myOtherData", new DataObject(32), DataObject.class);
Gson gson = new Gson();
System.out.println(gson.toJson(obj));
}
}
output:
{"data1":12,"data2":"hello","child":{"data1":25,"data2":"hello","list":["String 1","String 2","String 3"],"data":{}},"list":["String 1","String 2","String 3"],"data":{"myOtherData":{"data1":32,"data2":"hello","list":["String 1","String 2","String 3"],"data":{}}}}
but I actually need to make the work with a generic
Map<String, Object> data
So this code:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
class DataObject<T> {
private int data1 = 100;
private String data2 = "hello";
DataObject child;
private List<String> list = new ArrayList<String>() {
{
add("String 1");
add("String 2");
add("String 3");
}
};
private Map<String, Object> data=null;
public DataObject(int i){
this.data1 = i;
this.data = new HashMap<String, Object>();
}
//getter and setter methods
@Override
public String toString() {
return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list="
+ list + "]";
}
public Map<String, Object> getData() {
return data;
}
public void addData(final String key, Object value, Class<T> t) {
data.put(key, value);
}
}
public class test {
/**
* @param args
*/
public static void main(String[] args) {
DataObject obj = new DataObject(12);
obj.child = new DataObject(25);
obj.addData("myOtherData", new DataObject(32), DataObject.class);
Gson gson = new Gson();
System.out.println(gson.toJson(obj));
}
}
which is not working:
output:
{"data1":12,"data2":"hello","child":{"data1":25,"data2":"hello","list":["String 1","String 2","String 3"],"data":{}},"list":["String 1","String 2","String 3"],"data":{"myOtherData":{}}}
myOtherData object is missing, because Gson cannot work with general Object
That’s why I started putting the class as third argument of the addData method, I would need to put
Map<String, <T>> data;
I don’t know the way to declare a configurable type in the map
thanks for anyone who will make that work
edit : it’s almost working like that
I’m getting the error
Exception in thread “main” java.lang.ClassCastException: DataObject cannot be cast to java.lang.Class
at DataObject.addData(test.java:41)
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
class DataObject<T> {
private int data1 = 100;
private String data2 = "hello";
DataObject child;
private List<String> list = new ArrayList<String>() {
{
add("String 1");
add("String 2");
add("String 3");
}
};
private Map<String, Class<T>> data=null;
public DataObject(int i){
this.data1 = i;
this.data = new HashMap<String, Class<T>>();
}
//getter and setter methods
@Override
public String toString() {
return "DataObject [data1=" + data1 + ", data2=" + data2 + ", list="
+ list + "]";
}
public Map<String, Class<T>> getData() {
return data;
}
public void addData(final String key, Object value, Class<T> t) {
data.put(key, (Class<T>) value);
}
}
public class test {
/**
* @param args
*/
public static void main(String[] args) {
DataObject obj = new DataObject(12);
obj.child = new DataObject(25);
obj.addData("myOtherData", new DataObject(32), DataObject.class);
Gson gson = new Gson();
System.out.println(gson.toJson(obj));
}
}
EDIT:
Remove the class parameter of the addData() method, it is not needed. Simply type the value with
Tdirectly and typedatawithMap<String, T>. Initialize the map with:this.data = new HashMap<String, T>();This works for me (I typed your Map value with T and removed the unncesary Class parameter of addData). But your code actually works without those modifications. I ran it on my machine and I had the same output as the one you mark as “working”.
What is a little bit weird, is your typing of DataObject. It is confusing because you have also an inside member (child) which is of the same class. I don’t know if it should be of the same type
DataObject<T>or not. You should try to type properly your generics as much as possible. If you are unable to do so, it may be the sign of some incorrect use of generics.