简体   繁体   English

使用Gson序列化POJO

[英]Use Gson to serialize a POJO

I use GSON serialize POJO -- both the object before and after altered. 我使用GSON序列化POJO -更改对象之前和之后。

The altered one (call it A) which is setup by Struts2 could easily serialized to Json. 由Struts2设置的更改后的名称(称为A)可以轻松地序列化到Json。

While the POJO before altered which is get from database via iBatis (call it B) couldn't be serialized. 虽然更改前的POJO无法通过iBatis(称为B)从数据库中获取,但无法进行序列化。

The error message says: Forgot to register a type adapter? 错误消息显示:忘记注册类型适配器?

I have read the Gson API. 我已经阅读了Gson API。 But I don't think register a type adapter for every POJO is a good idea.What makes the B couldn't be serialized? 但是我不认为为每个POJO注册一个类型适配器是一个好主意,是什么使B无法序列化?

I write a clone() for my POJO, and the object cloned from B could be done also. 我为POJO编写了一个clone(),从B克隆的对象也可以完成。

This is confusing... Is there anybody could answer me? 这令人困惑...有人可以回答我吗?

before altered(B's clone): 更改之前(B的克隆):

{"id":"6429B5329C544711A9848AF243D10E4E","idType":"未选择","firstDate":"Feb 29, 2012 12:00:00 AM","name":"testetes","gender":"男","phone":"553223","city":"未选择","ocup":"未选择","nation":"未选择","famStru":"未选择","infSouc":"未选择","creater":"EE4783A6272A4B62A5CC68DB3C11FE1E","createDate":"Feb 29, 2012 12:00:00 AM","purpose":"未选择","education":"未选择","income":"未选择","cars":"未选择","acptCarpRent":"未选择","acptCarpPrice":"未选择","handStand":"未选择","intentHouse":"未选择","intentArea":"未选择","intentLayout":"未选择","nextDate":"Mar 7, 2012 12:00:00 AM","wuyeType":"未选择","attentionPro":"958B9E093A84415B901900C2DA25C712","ordinaryTraffic":"未选择","attentionPoint":"未选择","buyDate":"未选择","cityArea":"未选择","lastUpdate":"Feb 29, 2012 12:00:00 AM","lastModifier":"EE4783A6272A4B62A5CC68DB3C11FE1E","saler":"A4FB4877DC2945E980477544A955B57F","state":"意向","status":"0"}

After altered(A): 修改后(A):

{"id":"6429B5329C544711A9848AF243D10E4E","idType":"未选择","firstDate":"Feb 29, 2012 12:00:00 AM","visitMode":"","name":"testetes","gender":"男","telPhone":"","phone":"553223","fax":"","adrs":"","postCode":"","email":"","workUnit":"","city":"未选择","media_id":"","ocup":"未选择","idNum":"","nation":"未选择","famStru":"未选择","infSouc":"未选择","createDate":"Feb 29, 2012 12:00:00 AM","idAdr":"","purpose":"未选择","education":"未选择","income":"未选择","cars":"未选择","acptCarpRent":"未选择","acptCarpPrice":"未选择","handStand":"未选择","intentHouse":"未选择","intentArea":"未选择","intentLayout":"未选择","customerDetail":"","wuyeType":"未选择","attentionPro":"958B9E093A84415B901900C2DA25C712","ordinaryTraffic":"未选择","attentionPoint":"未选择","buyDate":"未选择","cityArea":"未选择","lastUpdate":"Mar 11, 2012 2:58:04 PM","lastModifier":"00000000000000000000000000000000","saler":"A4FB4877DC2945E980477544A955B57F","state":"意向"}

It sounds like your POJO is of type Customer? 听起来您的POJO属于客户类型? When you clone your object, you're creating a new Customer, and Gson can serialize that just fine. 克隆对象时,您正在创建一个新的Customer,Gson可以对其进行序列化。 When you fetch that same object from the DB, however, it's not a standard Customer object. 但是,当您从数据库中获取同一对象时,它不是标准的Customer对象。 Instead, it's a subclass that includes some persistence information, such as the class of the object. 相反,它是一个包含一些持久性信息的子类,例如对象的类。

Probably the simplest solution is to use Gson's @Expose annotation. 可能最简单的解决方案是使用Gson的@Expose批注。 If you create your Gson object with new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() , then you can mark each of the Customer fields that you would like to serialize with @Expose . 如果使用new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()创建Gson对象,则可以使用@Expose标记要序列化的每个Customer字段。 Any other fields, including those of your persistence framework's subclass, will not be serialized. 其他任何字段,包括持久性框架的子类的字段,都不会被序列化。

Brandon was right. 布兰登是对的。 Here is another solution if you don't want to use any annotation or modify your POJO class. 如果您不想使用任何注释或修改POJO类,这是另一个解决方案。 This may help for any other guys. 这可能对其他任何人都有帮助。

Type typeOfSrc = new TypeToken<A>() {}.getType(); //this helps for generic one.
gson.toJson(obj, typeOfSrc); or gson.toJson(obj, A.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM