How to do custom serialization / deserialization using JACKSON?

I am trying to convert the following gson serialization to JACKSON serialization. Please let me know what I need to change for it to work for 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); } } } 
+6
source share
2 answers

You can create your own serializer, for example:

  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(); } } 

And register it with ObjectMapper, and then serialize:

 ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addSerializer(yourObject.class, new ItemSerializer()); mapper.registerModule(module); String serialized = mapper.writeValueAsString(yourObject); 

To the className skip className , you can also use a custom field filter, you have a great example: http://www.baeldung.com/jackson-ignore-properties-on-serialization

+2
source

Jackson allows you to specify serializers through annotations. For example, see the Trivial example below:

 @JsonSerialize(using FooToStringSerializer) public class Foo implements Serializable { private String bar; public Foo(String bar) { this.bar = bar; } 

Then, if all I wanted to see when the object was serialized was bar , I would create a serializer as follows:

 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()); } 

For deserialization, you can create a deserializer and register it using the ObjectMapper , which will perform the deserialization.

To register a deserializer with an object mapper, follow these steps:

 ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addDeserializer(Item.class, new FooDeserializer()); mapper.registerModule(module); 

For a very simple example of custom deserialization see this link: http://www.baeldung.com/jackson-deserialization

+1
source

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


All Articles