Reputation: 100290
I am having trouble converting gson to JSON, using Google's library 'gson' library. I am using the Java Spark web framework, and ResponseTransformer is a part of the Spark lib. The model argument variable can be any POJO.
public class JsonTransformer implements ResponseTransformer {
private Gson gson = new Gson();
@Override
public String render(Object model) {
String json = null;
if (model instanceof java.util.List) {
model = ((java.util.List) model).get(0);
}
try {
json = gson.toJson(model);
} catch (Exception e) {
e.printStackTrace();
System.exit(5);
}
return json;
}
}
there is some sort of problem when model is a List object, the gson library is having trouble converting/serializing it to a json String.
So this is just a quick fix to try to debug this: I am trying to reassign model to the first element of the model List, if model is an instance of List. So this is more of a basic Java question... When I attempt to reassign model to the first element of model, Java is not letting that happen. Why is that so?
Also, if anyone knows why gson is failing so hard in this case, please LMK. This should not be happening. There is no obvious infinite recursion happening.
To prove it, here is the contents:
I get a infinite recursion stacktrace though:
java.lang.StackOverflowError at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375) at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.(ReflectiveTypeAdapterFactory.java:82) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72) at com.google.gson.Gson.getAdapter(Gson.java:356) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.(ReflectiveTypeAdapterFactory.java:82) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72) at com.google.gson.Gson.getAdapter(Gson.java:356) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.(ReflectiveTypeAdapterFactory.java:82) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72) at com.google.gson.Gson.getAdapter(Gson.java:356) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.(ReflectiveTypeAdapterFactory.java:82) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72) at com.google.gson.Gson.getAdapter(Gson.java:356) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.(ReflectiveTypeAdapterFactory.java:82) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72) at com.google.gson.Gson.getAdapter(Gson.java:356)
the object I am trying to convert from a POJO to JSON looks like this:
public class Model {
private String id;
private String title;
private boolean done;
private Date createdOn;
public Model(BasicDBObject dbObject) {
this.id = ((ObjectId) dbObject.get("_id")).toString();
this.title = dbObject.getString("title") == null ? "placeholder title" : dbObject.getString("title");
this.done = ((Boolean)dbObject.getBoolean("done")) == null ? false : dbObject.getBoolean("done");
this.createdOn = dbObject.getDate("createdOn") == null ? new Date() : dbObject.getDate("createdOn");
}
}
Upvotes: 0
Views: 1484
Reputation: 38899
Your problem is with BasicDBObject
being a default constructor argument.
Gson doesn't like it. Remove that and it will work. Read: https://sites.google.com/site/gson/gson-user-guide#TOC-Writing-a-Deserializer
Upvotes: 1