Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 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, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 15 | */ |
| 16 | package org.onlab.onos.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.List; |
| 25 | |
| 26 | /** |
| 27 | * Abstraction of a codec capable for encoding/decoding arbitrary objects to/from JSON. |
| 28 | */ |
| 29 | public abstract class JsonCodec<T> { |
| 30 | |
| 31 | /** |
| 32 | * Encodes the specified entity into JSON. |
| 33 | * |
| 34 | * @param entity entity to encode |
| 35 | * @param mapper object mapper |
| 36 | * @return JSON node |
| 37 | * @throws java.lang.UnsupportedOperationException if the codec does not |
| 38 | * support encode operations |
| 39 | */ |
| 40 | public abstract ObjectNode encode(T entity, ObjectMapper mapper); |
| 41 | |
| 42 | /** |
| 43 | * Decodes the specified entity from JSON. |
| 44 | * |
| 45 | * @param json JSON to decode |
| 46 | * @return decoded entity |
| 47 | * @throws java.lang.UnsupportedOperationException if the codec does not |
| 48 | * support decode operations |
| 49 | */ |
| 50 | public abstract T decode(ObjectNode json); |
| 51 | |
| 52 | /** |
| 53 | * Encodes the collection of the specified entities. |
| 54 | * |
| 55 | * @param entities collection of entities to encode |
| 56 | * @param mapper object mapper |
| 57 | * @return JSON array |
| 58 | * @throws java.lang.UnsupportedOperationException if the codec does not |
| 59 | * support encode operations |
| 60 | */ |
| 61 | public ArrayNode encode(Iterable<T> entities, ObjectMapper mapper) { |
| 62 | ArrayNode result = mapper.createArrayNode(); |
| 63 | for (T entity : entities) { |
| 64 | result.add(encode(entity, mapper)); |
| 65 | } |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Decodes the specified JSON array into a collection of entities. |
| 71 | * |
| 72 | * @param json JSON array to decode |
| 73 | * @return collection of decoded entities |
| 74 | * @throws java.lang.UnsupportedOperationException if the codec does not |
| 75 | * support decode operations |
| 76 | */ |
| 77 | public List<T> decode(ArrayNode json) { |
| 78 | List<T> result = new ArrayList<>(); |
| 79 | for (JsonNode node : json) { |
| 80 | result.add(decode((ObjectNode) node)); |
| 81 | } |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | } |