Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.util.serializers; |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 2 | |
Yuta HIGUCHI | 5c8cbeb | 2014-06-27 11:13:48 -0700 | [diff] [blame] | 3 | import net.onrc.onos.core.util.SwitchPort; |
| 4 | |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 5 | import org.codehaus.jackson.JsonGenerator; |
| 6 | import org.codehaus.jackson.map.SerializerProvider; |
| 7 | import org.codehaus.jackson.map.ser.std.SerializerBase; |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 8 | import java.io.IOException; |
| 9 | |
| 10 | /** |
| 11 | * JSON Serializer for SwitchPorts. |
| 12 | */ |
| 13 | public class SwitchPortSerializer extends SerializerBase<SwitchPort> { |
| 14 | |
| 15 | /** |
| 16 | * Public constructor - just calls its super class constructor. |
| 17 | */ |
| 18 | public SwitchPortSerializer() { |
| 19 | super(SwitchPort.class); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Serializes a SwitchPort object. |
| 24 | * |
| 25 | * @param switchPort object to serialize |
| 26 | * @param jsonGenerator generator to add the serialized object to |
| 27 | * @param serializerProvider not used |
| 28 | * @throws IOException if the serialization fails |
| 29 | */ |
| 30 | @Override |
| 31 | public void serialize(final SwitchPort switchPort, |
| 32 | final JsonGenerator jsonGenerator, |
| 33 | final SerializerProvider serializerProvider) |
| 34 | throws IOException { |
| 35 | jsonGenerator.writeStartObject(); |
| 36 | |
| 37 | jsonGenerator.writeStringField("dpid", |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 38 | switchPort.getDpid().toString()); |
Pavlin Radoslavov | 5cf1fe0 | 2014-07-03 22:52:25 -0700 | [diff] [blame] | 39 | // |
| 40 | // FIXME: The solution below to preresent the "short" port number |
| 41 | // as an unsigned value is a hack. The fix should be elsewhere |
| 42 | // (e.g., in class PortNumber itself). |
| 43 | // |
| 44 | jsonGenerator.writeNumberField("portNumber", |
Yuta HIGUCHI | 9da3a6e | 2014-06-10 22:11:58 -0700 | [diff] [blame] | 45 | switchPort.getPortNumber().value()); |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 46 | |
| 47 | jsonGenerator.writeEndObject(); |
| 48 | } |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 49 | } |