Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame] | 1 | package net.floodlightcontroller.restserver; |
| 2 | |
| 3 | import org.codehaus.jackson.Version; |
| 4 | import org.codehaus.jackson.map.JsonSerializer; |
| 5 | import org.codehaus.jackson.map.ObjectMapper; |
| 6 | import org.codehaus.jackson.map.module.SimpleModule; |
| 7 | import org.codehaus.jackson.map.ser.CustomSerializerFactory; |
| 8 | import org.restlet.ext.jackson.JacksonRepresentation; |
| 9 | import 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 | */ |
| 26 | public class CustomSerializerHelper { |
Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame] | 27 | private final SimpleModule customSerializerModule; |
| 28 | private CustomSerializerFactory sf; |
| 29 | |
| 30 | /** |
| 31 | * Constructor. |
| 32 | */ |
| 33 | public CustomSerializerHelper() { |
Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame] | 34 | customSerializerModule = new SimpleModule("custom-serializers", new Version(1, 0, 0, null)); |
Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame] | 35 | 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 Hart | 68e8dd6 | 2014-08-26 18:06:23 -0700 | [diff] [blame] | 58 | ObjectMapper mapper = jacksonRepresentation.getObjectMapper(); |
| 59 | |
Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame] | 60 | mapper.registerModule(customSerializerModule); |
Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame] | 61 | mapper.setSerializerFactory(sf); |
Jonathan Hart | 68e8dd6 | 2014-08-26 18:06:23 -0700 | [diff] [blame] | 62 | jacksonRepresentation.setObjectMapper(mapper); |
| 63 | |
Jonathan Hart | 77803d3 | 2014-08-19 08:53:00 -0700 | [diff] [blame] | 64 | return jacksonRepresentation; |
| 65 | } |
| 66 | } |