T - type of objects handled by this converter.public interface Converter<T> extends Serializer<T>, Deserializer<T>
Genson genson = new Genson.Builder().with(new Converter<URL>() {
@Override
public void serialize(URL url, ObjectWriter writer, Context ctx) {
// you don't have to worry about null objects, as the library will handle them.
writer.writeValue(obj.toExternalForm());
}
@Override
public URL deserialize(ObjectReader reader, Context ctx) {
return new URL(reader.valueAsString());
}
}).create();
String serializedUrl = genson.serialize(new URL("http://www.google.com"));
URL url = genson.deserialize(serializedUrl, URL.class);
As you can see it is quite straightforward to create and register new Converters. Here is an
example dealing with more complex objects.| Modifier and Type | Method and Description |
|---|---|
T |
deserialize(ObjectReader reader,
Context ctx) |
void |
serialize(T object,
ObjectWriter writer,
Context ctx) |
void serialize(T object, ObjectWriter writer, Context ctx) throws Exception
serialize in interface Serializer<T>object - we want to serialize. The object is of type T or a subclass (if this serializer
has been registered for subclasses).writer - to use to write data to the output stream.ctx - the current context.JsonBindingExceptionJsonStreamExceptionExceptionT deserialize(ObjectReader reader, Context ctx) throws Exception
deserialize in interface Deserializer<T>reader - used to read data from.ctx - the current context.JsonBindingExceptionJsonStreamExceptionExceptionCopyright © 2019. All rights reserved.