将gson转换为JSON

时间:2015-05-29 23:00:20

标签: java json gson

我无法使用Google的库'gson'库将gson转换为JSON。我使用的是Java Spark Web框架,而ResponseTransformer是Spark lib的一部分。模型参数变量可以是任何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;
    }
}

当model是List对象时,存在某种问题,gson库无法将其转换/序列化为json String。

所以这只是一个快速修复试图调试这个:我试图将模型重新分配给模型List的第一个元素,如果model是List的实例。所以这更像是一个基本的Java问题......当我试图将模型重新分配给模型的第一个元素时,Java并没有让它发生。为什么会这样?

此外,如果有人知道为什么gson在这种情况下如此努力地失败,请LMK。这不应该发生。没有明显的无限递归发生。

为证明这一点,以下是内容:

enter image description here

我得到了无限递归堆栈跟踪:

java.lang.StackOverflowError的  在com.google.gson.internal。$ Gson $ Types.resolve($ Gson $ Types.java:375)         在com.google.gson.internal。$ Gson $ Types.resolve($ Gson $ Types.java:380)         在com.google.gson.internal。$ Gson $ Types.resolve($ Gson $ Types.java:375)         在com.google.gson.internal。$ Gson $ Types.resolve($ Gson $ Types.java:380)         在com.google.gson.internal。$ Gson $ Types.resolve($ Gson $ Types.java:375)         在com.google.gson.internal。$ Gson $ Types.resolve($ Gson $ Types.java:380)  在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1。(ReflectiveTypeAdapterFactory.java:82)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)         在com.google.gson.Gson.getAdapter(Gson.java:356)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1。(ReflectiveTypeAdapterFactory.java:82)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)         在com.google.gson.Gson.getAdapter(Gson.java:356)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1。(ReflectiveTypeAdapterFactory.java:82)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)         在com.google.gson.Gson.getAdapter(Gson.java:356)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1。(ReflectiveTypeAdapterFactory.java:82)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)         在com.google.gson.Gson.getAdapter(Gson.java:356)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ 1。(ReflectiveTypeAdapterFactory.java:82)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)         在com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)         在com.google.gson.Gson.getAdapter(Gson.java:356)

我试图从POJO转换为JSON的对象如下所示:

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");
    }
}

1 个答案:

答案 0 :(得分:1)

您的问题是BasicDBObject是默认构造函数参数。 Gson不喜欢它。删除它,它会工作。阅读:https://sites.google.com/site/gson/gson-user-guide#TOC-Writing-a-Deserializer