使用Gson序列化一个POJO [英] Use Gson to serialize a POJO

查看:86
本文介绍了使用Gson序列化一个POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



由Struts2设置的修改过的(称为A)可以很容易地序列化为JSON。

虽然通过iBatis从数据库获取的POJO(称为B)不能被序列化。



错误消息显示:忘记注册类型适配器了?



我已阅读Gson API。但我不认为为每个POJO注册一个类型适配器是一个好主意。什么使得B无法序列化?



我写一个clone()for我的POJO,以及从B克隆的对象也可以完成。



这是令人困惑的...有没有人可以回答我?

改变之前(B的克隆):

  {id:6429B5329C544711A9848AF243D10E4E,idType:未选择,firstDate:Feb 29, 2012:00:00:00 AM,name:testetes,gender:男,phone:553223,city:未选择,ocup:未选择 ,国家:未选择,famStru:未选择,infSouc:未选择,creater:EE4783A6272A4B62A5CC68DB3C11FE1E,createDate:2012年2月29日12:00:00 AM  目的: 未选择, 教育: 未选择, 收入: 未选择, 汽车: 未选择, acptCarpRent: 未选择, acptCarpPrice :未选择,handStand:未选择,intentHouse:未选择,intentArea:未选择,intentLayout:未选择,nextDate:Mar 7 ,2012 12:00:00 AM,wuyeType:未选择,attentionPro:958B9E093A84415B901900C2DA25C712,ordinaryTraffic:未选择,attentionPoint:未选择,buyDate未选择,cityArea:未选择,lastUpdate:2012年2月29日12:00:00上午,lastModifier: EE4783A6272A4B62A5CC68DB3C11FE1E,saler:A4FB4877DC2945E980477544A955B57F,state:意向,status:0} 

更改后(A):

  {id:6429B5329C544711A9848AF243D10E4E,idType:未选择,firstDate:2012年2月29日12:00:00 AM,visitMode:,name:testetes,gender:男,telPhone: , 手机: 553223, 传真: , 不良反应: , 邮编: , 电子邮件: , 工作单元: , 城市:未选择  media_id: , OCUP: 未选择, IDNUM: , 民族: 未选择, famStru: 未选择, infSouc: 未选择,createDate:2012年2月29日12:00:00,idAdr:,purpose:未选择,education:未选择,收入:未选择  汽车: 未选择, acptCarpRent: 未选择, acptCarpPrice: 未选择, 倒立: 未选择, intentHouse: 未选择, intentArea : 未选择, intentLayout: 未选择, 为CustomerDetail: , wuyeType: 未选择, attentionPro: 958B9E093A84415B901900C2DA25C712, ordinaryTraffic: 未选择, attentionPoint: 未选择, buyDate:未选择,cityArea:未选择,lastUpdate:2012年3月11日下午2:58:04,lastModifier:00000000000000000000000000000000,saler:A4FB4877DC2945E980477544A955B57F, state:意向} 


解决方案

您的POJO是客户类型?当你克隆你的对象时,你正在创建一个新的Customer,而Gson可以很好地序列化。但是,当您从数据库中获取相同的对象时,它不是标准的Customer对象。相反,它是一个包含一些持久性信息的子类,例如对象的类。

可能最简单的解决方案是使用Gson的 @Expose 注释。如果使用 new GsonBuilder()。excludeFieldsWithoutExposeAnnotation()。create()创建Gson对象,则可以标记每个您希望序列化的Customer字段 @expose 。任何其他字段(包括持久性框架的子类的字段)都不会被序列化。


I use GSON serialize POJO -- both the object before and after altered.

The altered one (call it A) which is setup by Struts2 could easily serialized to Json.

While the POJO before altered which is get from database via iBatis (call it B) couldn't be serialized.

The error message says: Forgot to register a type adapter?

I have read the 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?

I write a clone() for my POJO, and the object cloned from B could be done also.

This is confusing... Is there anybody could answer me?

before altered(B's clone):

{"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):

{"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? When you clone your object, you're creating a new Customer, and Gson can serialize that just fine. When you fetch that same object from the DB, however, it's not a standard Customer object. 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. 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. Any other fields, including those of your persistence framework's subclass, will not be serialized.

这篇关于使用Gson序列化一个POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆