我想做定制的嵌套序列化。这是一个示例。我的申请中也有类似情况。 我认为我对这里的事情没有很好的了解。 似乎有一个SerializationContext传递到serialize方法中。我认为,它可以用于嵌套序列化。我还没有在网上找到一个简单的例子...
但是,我在这里所做的没有用。如果看到输出,它将输出Property对象的所有字段,而我只想获取几个,并且使用不同的名称,等等。
public class Outer {
private String name;
private String objectId;
private List<Property> properties;
// getters setters
}
public class Property {
private Integer id;
private String info;
private Date createdOn;
private String dontCare;
Property (Integer id) {
this.createdOn = new Date();
this.dontCare = ( (id%2 == 0) ? "Y" : "N");
this.id = id;
SimpleDateFormat dt = new SimpleDateFormat("mm-dd hh:mm:ss");
this.info = dt.format(createdOn);
}
// getters setters
}
public class Practice {
public static void main (String args[])
{
JsonSerializer<List<Property>> listSerializer = new JsonSerializer<List<Property>>() {
@Override
public JsonElement serialize(List<Property> src, Type typeOfSource, JsonSerializationContext context) {
JsonSerializer<Property> serializer = new JsonSerializer<Property>() {
@Override
public JsonElement serialize(Property src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject property = new JsonObject();
property.addProperty("Identifier", src.getId());
return property;
}
}; // will implement in a second
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Property.class, serializer);
Gson customGson = gsonBuilder.setPrettyPrinting().create();
JsonObject jsonObject = new JsonObject();
List<Property> properties = new ArrayList<Property>(src.size());
String propertiesAsString = "";
for (Property aProperty : src) {
propertiesAsString +=customGson.toJson(aProperty);
}
JsonObject propertiesJsonObject = new JsonObject();
propertiesJsonObject.addProperty("properties", propertiesAsString);
return propertiesJsonObject;
}
};
GsonBuilder gsonBuilder4List = new GsonBuilder();
Type propertyListType = new TypeToken<List<Property>> () {}.getType();
gsonBuilder4List.registerTypeAdapter(propertyListType, listSerializer);
Gson customGson4List = gsonBuilder4List.setPrettyPrinting().create();
String customJSON2 = customGson4List.toJson(giveMePropertyList(3));
System.out.println("Property List...");
System.out.println (customJSON2);
}
public static ArrayList<Property> giveMePropertyList (int count)
{
ArrayList<Property> list = new ArrayList<Property>();
for (int i = 0; i < count; i++) {
int random = (int )(Math.random() * 98768 + 1);
list.add(new Property(random));
}
return list;
}
}
输出为
Property List...
[
{
"id": 43875,
"info": "51-06 08:51:38",
"createdOn": "Nov 6, 2018 8:51:38 AM",
"dontCare": "N"
},
{
"id": 30511,
"info": "51-06 08:51:38",
"createdOn": "Nov 6, 2018 8:51:38 AM",
"dontCare": "N"
},
{
"id": 17348,
"info": "51-06 08:51:38",
"createdOn": "Nov 6, 2018 8:51:38 AM",
"dontCare": "Y"
}
]
答案 0 :(得分:0)
这就是我想要的 这是输出
Custom adapters
{
"Identification": "3",
"properties": [
{
"Identifier": 16034,
"Information": "43-06 02:43:48 (Y)"
},
{
"Identifier": 46046,
"Information": "43-06 02:43:48 (Y)"
},
{
"Identifier": 91351,
"Information": "43-06 02:43:48 (N)"
}
]
}
这是我所缺少的代码。我仍然需要了解接线方式... 如果有人在评论中简要解释一下,那将非常有帮助
TypeToken<List<Property>> typeDescription = new TypeToken<List<Property>>() {};
JsonElement properties = context.serialize(src.getProperties(), typeDescription.getType());
outer.add("properties", properties);
完整代码(与TypeToken相关的代码,我来自另一个stackoverflow答案...)
package wrk;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;
public class Practice {
public static void main (String args[])
{
JsonSerializer<Property> serializer4property = new JsonSerializer<Property>() {
@Override
public JsonElement serialize(Property src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject property = new JsonObject();
property.addProperty("Identifier", src.getId());
property.addProperty("Information", src.getInfo() + " (" + src.getDontCare() + ")");
return property;
}
};
JsonSerializer<Outer> serializer4outer = new JsonSerializer<Outer>() {
@Override
public JsonElement serialize(Outer src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject outer = new JsonObject();
outer.addProperty("Identification", src.getName());
TypeToken<List<Property>> typeDescription = new TypeToken<List<Property>>() {};
JsonElement properties = context.serialize(src.getProperties(), typeDescription.getType());
outer.add("properties", properties);
return outer;
}
};
System.out.println("An outer object...");
Outer outer = giveMeAnOuter(3);
System.out.println("");
System.out.println("Custom adapters");
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Property.class, serializer4property);
gsonBuilder.registerTypeAdapter(Outer.class, serializer4outer);
Gson customGson = gsonBuilder.setPrettyPrinting().create();
String customJSON = customGson.toJson(outer);
System.out.println (customJSON);
/*
System.out.println("\n");
System.out.println ("Default behavior");
GsonBuilder gsonBuilder2 = new GsonBuilder();
Gson customGson2 = gsonBuilder2.setPrettyPrinting().create();
String customJSON2 = customGson2.toJson(outer);
System.out.println (customJSON2);
*/
}
public static Outer giveMeAnOuter (int count)
{
Outer anOuter = new Outer();
anOuter.setName(String.valueOf(count));
ArrayList<Property> properties = new ArrayList<Property>();
for (int i = 1; i <= count; i++) {
int random = (int )(Math.random() * 98768 + 1);
properties.add(new Property(random));
}
anOuter.setFirstProperty(properties.get(0));
anOuter.setProperties(properties);
return anOuter;
}
}