gson:json字符串包含神秘的“处理程序”?

时间:2014-04-29 15:44:28

标签: gson

我一直在使用gson序列化对象并将json数据发送到前端。它在过去总是按预期工作。

现在我上课了。它有一个名为" destination"的对象字段。拥有类是正确序列化的,但是对象字段没有被序列化,而且,它的json数据有一个名为" handler" ("目的地"的类没有这个"处理程序"字段)。

这是json数据(它显示了这个想法):

{
    name: my name (this gets serialized)

    destination: {
        seq: 0   (this does not get serizlied. 0 is the default)
        validation: true (this does not get serialized. true is the default)

        (don't konw why this handler is here)
        handler: {
            Object { interfaces=[1], constructed=true, persistentClass={...}, more...}
            constructed: true
            entityName:"abc.MyClass"
            initialized: true
            interfaces: [Object {}]
            overridesEquals: false
            persistentClass: Object {}
            readOnly: false
            specjLazyLoad: false
            target: Object { validation=false, seq=2,  more...}   (this object contains all the actual values)
            unwrap: false   
        }
    }   
}

有趣的是,"处理程序"有一个名为" target"的属性,它包含所有序列化数据。

这是我的Java代码:

    Gson gson = new GsonBuilder().create();
    String  json = gson.toJson(myData); 

有没有人见过这个"处理程序"情况?我做的可能是错的?

我使用gson 1.7.1。

谢谢!

1 个答案:

答案 0 :(得分:1)

Gson仅通过Java Reflection API

使用字段private,public,protected
(don't konw why this handler is here)
    handler: { ....}
在序列化过程中,Gson没有做神奇,在JSON中添加额外的处理程序字段对象,如果它生成json意味着你在处理器属性中某些内部目标类,请再次检查你的代码。如果找到它,则可以忽略使用Expose注释

@Expose(serialize = false)//在序列化过程中不要序列化处理程序字段 私有处理程序处理程序;

Interestingly, the "handler" has a property called "target", which has all the serialized data.

没有什么有趣的,意味着你在目标对象中有处理程序属性,这就是你在生成的json中看到它的原因。