我正在尝试将以下gson序列化转换为JACKSON序列化。请告诉我需要更改的内容,以使其适用于JACKSON
public class AbstractElementAdapter
implements JsonSerializer<AbstractElement>, JsonDeserializer<AbstractElement>
{
@Override
public JsonElement serialize(AbstractElement src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject result = new JsonObject();
JsonObject properties = context.serialize(src, src.getClass()).getAsJsonObject();
if (src instanceof TruncatedElement) {
result.add("type", new JsonPrimitive(((TruncatedElement) src).getClassName()));
properties.remove("className");
} else {
result.add("type", new JsonPrimitive(src.getClass().getSimpleName()));
}
result.add("properties", properties);
return result;
}
@Override
public AbstractElement deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
String type = jsonObject.get("type").getAsString();
JsonElement element = jsonObject.get("properties");
try {
return context.deserialize(element, Class.forName("com.zreflect.emyed.whiteboard.model.element." + type));
} catch (ClassNotFoundException cnfe) {
throw new JsonParseException("Unknown element type: " + type, cnfe);
}
}
}
答案 0 :(得分:2)
您可以创建自定义序列化程序,如下所示:
public class ItemSerializer extends JsonSerializer<AbstractElement> {
@Override
public void serialize(AbstractElement src, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
jgen.writeStartObject();
if (src instanceof TruncatedElement) {
jgen.writeStringField("type",((TruncatedElement) src).getClassName());
jgen.writeObjectFieldStart("properties");
//use jgen.writeStringField();
//jgen.writeNumberField();
//etc to every one of the values,
//but skipping className
jgen.writeEndObject();
} else {
jgen.writeStringField("type", src.getClass().getSimpleName() );
//write everythin
jgen.writeObjectField("properties", src);
}
jgen.writeEndObject();
}
}
并使用ObjectMapper注册它,然后执行序列化:
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(yourObject.class, new ItemSerializer());
mapper.registerModule(module);
String serialized = mapper.writeValueAsString(yourObject);
对于跳过className
的技巧,您可能还想使用自定义字段过滤器,这里有一个很好的示例:
http://www.baeldung.com/jackson-ignore-properties-on-serialization
答案 1 :(得分:1)
Jackson允许您通过注释指定序列化程序。例如,请参阅下面的简单示例:
@JsonSerialize(using FooToStringSerializer)
public class Foo implements Serializable {
private String bar;
public Foo(String bar) {
this.bar = bar;
}
然后,如果我想要查看对象序列化的时间是bar
,我会像这样创建序列化器:
public class FooToStringSerializer extends JsonSerializer<Foo> {
@Override
public void serialize(final Foo value, final JsonGenerator jgen,
final SerializerProvider provider) throws IOException
{
jgen.writeObject(value.getBar());
}
对于反序列化,您可以创建一个反序列化器并将其注册到将要进行反序列化的ObjectMapper
。
要使用对象映射器注册反序列化器,请执行以下操作:
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Item.class, new FooDeserializer());
mapper.registerModule(module);
有关自定义反序列化的一个非常容易理解的示例,请参阅此链接: http://www.baeldung.com/jackson-deserialization