2012-10-18 110 views
6

我想将下面的gson序列化转换为JACKSON序列化。请让我知道我需要改变以使其适用于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); 
     } 
    } 
} 

回答

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

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

+0

如果你有AbstractDocument.AbstractElement?我们只有AbstractElement类。 – user1595858

+0

如果只是使用AbstractElement,它将进入无限循环 – user1595858

+0

对不起,它只是AbstractAlement,自动导入添加了其他 –

相关问题