2012-10-18 111 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,自動導入添加了其他 –

相關問題