blob: 8f99be09041c9f1f2cdc455a946459a3a4fb8dd8 [file] [log] [blame]
Jonathan Hart77803d32014-08-19 08:53:00 -07001package net.floodlightcontroller.restserver;
2
3import org.codehaus.jackson.Version;
4import org.codehaus.jackson.map.JsonSerializer;
5import org.codehaus.jackson.map.ObjectMapper;
6import org.codehaus.jackson.map.module.SimpleModule;
7import org.codehaus.jackson.map.ser.CustomSerializerFactory;
8import org.restlet.ext.jackson.JacksonRepresentation;
9import org.restlet.representation.Representation;
10
11/**
12 * Helper class used to dynamically override the default serializers used for
13 * JSON serialization.
14 * <p/>
15 * Serializers can be added to the helper class at runtime using
16 * {@link #addSerializer(Class, JsonSerializer)}. The serializers contained by
17 * the helper class can then be added to a JacksonRepresentation prior to
18 * serializing it using {@link #applySerializers(JacksonRepresentation)}.
19 * <p/>
20 * This class enables the use of custom serializers for Java objects without
21 * having to hardcode the mapping of class to serializer using
22 * annotations on the class. Any serialization annotations on the class will be
23 * overridden by the serializers used here, so different serializers can be
24 * used on the class in different contexts.
25 */
26public class CustomSerializerHelper {
Jonathan Hart77803d32014-08-19 08:53:00 -070027 private final SimpleModule customSerializerModule;
28 private CustomSerializerFactory sf;
29
30 /**
31 * Constructor.
32 */
33 public CustomSerializerHelper() {
Jonathan Hart77803d32014-08-19 08:53:00 -070034 customSerializerModule = new SimpleModule("custom-serializers", new Version(1, 0, 0, null));
Jonathan Hart77803d32014-08-19 08:53:00 -070035 sf = new CustomSerializerFactory();
36 }
37
38 /**
39 * Adds a serializer to the set of serializers that will be used for JSON
40 * serialization.
41 *
42 * @param serializer the serializer to add
43 */
44 public <T> void addSerializer(Class<T> clazz, JsonSerializer<T> serializer) {
45 customSerializerModule.addSerializer(serializer);
46 sf.addGenericMapping(clazz, serializer);
47 }
48
49 /**
50 * Applies the list of serializers to the JacksonRepresentation so they
51 * will be used when the object in the representation is serialized
52 *
53 * @param jacksonRepresentation the representation to apply the serializers
54 * to
55 * @return a representation with the custom serializers applied
56 */
57 public Representation applySerializers(JacksonRepresentation<?> jacksonRepresentation) {
Jonathan Hart68e8dd62014-08-26 18:06:23 -070058 ObjectMapper mapper = jacksonRepresentation.getObjectMapper();
59
Jonathan Hart77803d32014-08-19 08:53:00 -070060 mapper.registerModule(customSerializerModule);
Jonathan Hart77803d32014-08-19 08:53:00 -070061 mapper.setSerializerFactory(sf);
Jonathan Hart68e8dd62014-08-26 18:06:23 -070062 jacksonRepresentation.setObjectMapper(mapper);
63
Jonathan Hart77803d32014-08-19 08:53:00 -070064 return jacksonRepresentation;
65 }
66}