Reputation: 3870
I am trying to convert the following gson serialization to JACKSON serialization. Please let me know what i need to change to make it 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);
}
}
}
Upvotes: 7
Views: 2789
Reputation: 10947
You can create a custom serializer, something like this:
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 the ObjectMapper and then do the serialization:
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(yourObject.class, new ItemSerializer());
mapper.registerModule(module);
String serialized = mapper.writeValueAsString(yourObject);
To the trick of skipping className
, you could also want to use a custom field filter, you have a great example here:
http://www.baeldung.com/jackson-ignore-properties-on-serialization
Upvotes: 3
Reputation: 4121
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 the serializer like so:
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 with the ObjectMapper
that will be doing the deserialization.
To register a deserializer with an object mapper, do the below:
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Item.class, new FooDeserializer());
mapper.registerModule(module);
For a really easy to follow example of custom deserialization, see this link: http://www.baeldung.com/jackson-deserialization
Upvotes: 1