Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1878 | jmachado | 1 | package com.owlike.genson; |
2 | |||
3 | import java.io.IOException; |
||
4 | |||
5 | import com.owlike.genson.stream.ObjectReader; |
||
6 | |||
7 | /** |
||
8 | * Converter interface is a shorthand for classes who want to implement both serialization and |
||
9 | * deserialization. You should always privilege Converter instead of low level Serializer and |
||
10 | * Deseriliazer as they will be wrapped into a converter and all the ChainedFactory mechanism is |
||
11 | * designed for converters. Here is an example of a Converter of URLs. |
||
12 | * <p/> |
||
13 | * <pre> |
||
14 | * Genson genson = new Genson.Builder().with(new Converter<URL>() { |
||
15 | * |
||
16 | * @Override |
||
17 | * public void serialize(URL url, ObjectWriter writer, Context ctx) { |
||
18 | * // you don't have to worry about null objects, as the library will handle them. |
||
19 | * writer.writeValue(obj.toExternalForm()); |
||
20 | * } |
||
21 | * |
||
22 | * @Override |
||
23 | * public URL deserialize(ObjectReader reader, Context ctx) { |
||
24 | * return new URL(reader.valueAsString()); |
||
25 | * } |
||
26 | * |
||
27 | * }).create(); |
||
28 | * |
||
29 | * String serializedUrl = genson.serialize(new URL("http://www.google.com")); |
||
30 | * URL url = genson.deserialize(serializedUrl, URL.class); |
||
31 | * </pre> |
||
32 | * <p/> |
||
33 | * As you can see it is quite straightforward to create and register new Converters. Here is an |
||
34 | * example dealing with more complex objects. |
||
35 | * |
||
36 | * @param <T> type of objects handled by this converter. |
||
37 | * @author eugen |
||
38 | */ |
||
39 | public interface Converter<T> extends Serializer<T>, Deserializer<T> { |
||
40 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
41 | public void serialize(T object, com.owlike.genson.stream.ObjectWriter writer, 5+0%2Fdocs%2Fapi+Context">Context ctx) throws 1.5.0/docs/api/java/lang/Exception.html">Exception; |
||
42 | |||
43 | @1.5.0/docs/api/java/lang/Override.html">Override |
||
44 | public T deserialize(ObjectReader reader, 5+0%2Fdocs%2Fapi+Context">Context ctx) throws 1.5.0/docs/api/java/lang/Exception.html">Exception; |
||
45 | } |