Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | * not use this file except in compliance with the License. You may obtain |
| 6 | * a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations |
| 14 | * under the License. |
| 15 | **/ |
| 16 | |
| 17 | package net.floodlightcontroller.util; |
| 18 | |
| 19 | import java.io.IOException; |
| 20 | |
| 21 | import java.sql.Timestamp; |
| 22 | |
| 23 | |
| 24 | import org.codehaus.jackson.JsonGenerator; |
| 25 | import org.codehaus.jackson.JsonProcessingException; |
| 26 | import org.codehaus.jackson.map.JsonSerializer; |
| 27 | import org.codehaus.jackson.map.SerializerProvider; |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * @author subrata |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | public class EventHistoryBaseInfoJSONSerializer extends |
| 36 | JsonSerializer<EventHistoryBaseInfo> { |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * Performs the serialization of a EventHistory.BaseInfo object |
| 41 | */ |
| 42 | @Override |
| 43 | public void serialize(EventHistoryBaseInfo base_info, JsonGenerator jGen, |
| 44 | SerializerProvider serializer) |
| 45 | throws IOException, JsonProcessingException { |
| 46 | jGen.writeStartObject(); |
| 47 | jGen.writeNumberField("Idx", base_info.getIdx()); |
| 48 | Timestamp ts = new Timestamp(base_info.getTime_ms()); |
| 49 | String tsStr = ts.toString(); |
| 50 | while (tsStr.length() < 23) { |
| 51 | tsStr = tsStr.concat("0"); |
| 52 | } |
| 53 | jGen.writeStringField("Time", tsStr); |
| 54 | jGen.writeStringField("State", base_info.getState().name()); |
| 55 | String acStr = base_info.getAction().name().toLowerCase(); |
| 56 | // Capitalize the first letter |
| 57 | acStr = acStr.substring(0,1).toUpperCase().concat(acStr.substring(1)); |
| 58 | jGen.writeStringField("Action", acStr); |
| 59 | jGen.writeEndObject(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Tells SimpleModule that we are the serializer for OFMatch |
| 64 | */ |
| 65 | @Override |
| 66 | public Class<EventHistoryBaseInfo> handledType() { |
| 67 | return EventHistoryBaseInfo.class; |
| 68 | } |
| 69 | } |