HIGUCHI Yuta | f086d8a | 2013-06-12 15:26:35 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.util.serializers; |
Pavlin Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 2 | |
| 3 | import java.io.IOException; |
| 4 | |
| 5 | import org.codehaus.jackson.JsonNode; |
| 6 | import org.codehaus.jackson.JsonParser; |
| 7 | import org.codehaus.jackson.JsonToken; |
| 8 | import org.codehaus.jackson.JsonProcessingException; |
| 9 | import org.codehaus.jackson.ObjectCodec; |
| 10 | import org.codehaus.jackson.map.JsonDeserializer; |
| 11 | import org.codehaus.jackson.map.DeserializationContext; |
| 12 | |
| 13 | import net.floodlightcontroller.util.MACAddress; |
| 14 | |
| 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
| 17 | |
| 18 | /** |
| 19 | * Deserialize a MAC address from a string. |
| 20 | */ |
| 21 | public class MACAddressDeserializer extends JsonDeserializer<MACAddress> { |
| 22 | |
| 23 | protected static Logger log = LoggerFactory.getLogger(MACAddressDeserializer.class); |
| 24 | |
| 25 | @Override |
| 26 | public MACAddress deserialize(JsonParser jp, |
| 27 | DeserializationContext ctxt) |
| 28 | throws IOException, JsonProcessingException { |
| 29 | |
| 30 | MACAddress mac = null; |
| 31 | |
| 32 | jp.nextToken(); // Move to JsonToken.START_OBJECT |
| 33 | while (jp.nextToken() != JsonToken.END_OBJECT) { |
| 34 | String fieldname = jp.getCurrentName(); |
| 35 | if ("value".equals(fieldname)) { |
| 36 | String value = jp.getText(); |
| 37 | log.debug("Fieldname: " + fieldname + " Value: " + value); |
| 38 | mac = MACAddress.valueOf(value); |
| 39 | } |
| 40 | } |
| 41 | return mac; |
| 42 | } |
| 43 | } |