Use Gson to serialize POJO

I am using GSON serialize POJO - both objects before and after the change.

The modified (name it A), which is configured by Struts2, can be easily serialized in Json.

So far, the POJO before it has been modified, which receives from the database via iBatis (name it B), cannot be serialized.

The error message says: "Forgot to register the type adapter?"

I read the Gson API. But I don’t think registering a type adapter for each POJO is a good idea. What makes B cannot be serialized?

I am writing clone () for my POJO, and an object cloned from B can also be executed.

This is confusing ... Can anyone answer me?

before change (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 change (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":"意向"} 
+6
source share
2 answers

Does your POJO seem to be of type Customer? When you clone your object, you create a new Client, and Gson can serialize it just fine. However, when you retrieve the same object from the database, it is not a standard Customer object. Instead, it is a subclass that contains some persistence information, such as an object class.

Probably the easiest solution is to use the Gson @Expose annotation. If you create your Gson object with new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create() , you can mark each of the Customer fields that you want to serialize with @Expose . Any other fields, including the framework persistence subclass, will not be serialized.

+10
source
Brandon was right. Here is another solution if you do not want to use annotation or modify the POJO class. It can help any other guys.
 Type typeOfSrc = new TypeToken<A>() {}.getType(); //this helps for generic one. gson.toJson(obj, typeOfSrc); or gson.toJson(obj, A.class); 
+2
source

Source: https://habr.com/ru/post/910466/


All Articles