Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.codec; |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.List; |
| 24 | |
| 25 | /** |
| 26 | * Abstraction of a codec capable for encoding/decoding arbitrary objects to/from JSON. |
| 27 | */ |
| 28 | public abstract class JsonCodec<T> { |
| 29 | |
| 30 | /** |
| 31 | * Encodes the specified entity into JSON. |
| 32 | * |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 33 | * @param entity entity to encode |
| 34 | * @param context encoding context |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 35 | * @return JSON node |
| 36 | * @throws java.lang.UnsupportedOperationException if the codec does not |
| 37 | * support encode operations |
| 38 | */ |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 39 | public ObjectNode encode(T entity, CodecContext context) { |
| 40 | throw new UnsupportedOperationException("encode() not supported"); |
| 41 | } |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Decodes the specified entity from JSON. |
| 45 | * |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 46 | * @param json JSON to decode |
| 47 | * @param context decoding context |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 48 | * @return decoded entity |
| 49 | * @throws java.lang.UnsupportedOperationException if the codec does not |
| 50 | * support decode operations |
| 51 | */ |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 52 | public T decode(ObjectNode json, CodecContext context) { |
| 53 | throw new UnsupportedOperationException("decode() not supported"); |
| 54 | } |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Encodes the collection of the specified entities. |
| 58 | * |
| 59 | * @param entities collection of entities to encode |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 60 | * @param context encoding context |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 61 | * @return JSON array |
| 62 | * @throws java.lang.UnsupportedOperationException if the codec does not |
| 63 | * support encode operations |
| 64 | */ |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 65 | public ArrayNode encode(Iterable<T> entities, CodecContext context) { |
| 66 | ArrayNode result = context.mapper().createArrayNode(); |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 67 | for (T entity : entities) { |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 68 | result.add(encode(entity, context)); |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 69 | } |
| 70 | return result; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Decodes the specified JSON array into a collection of entities. |
| 75 | * |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 76 | * @param json JSON array to decode |
| 77 | * @param context decoding context |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 78 | * @return collection of decoded entities |
| 79 | * @throws java.lang.UnsupportedOperationException if the codec does not |
| 80 | * support decode operations |
| 81 | */ |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 82 | public List<T> decode(ArrayNode json, CodecContext context) { |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 83 | List<T> result = new ArrayList<>(); |
| 84 | for (JsonNode node : json) { |
Thomas Vachuska | ca60f2b | 2014-11-06 01:34:28 -0800 | [diff] [blame] | 85 | result.add(decode((ObjectNode) node, context)); |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 86 | } |
| 87 | return result; |
| 88 | } |
| 89 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Gets a child Object Node from a parent by name. If the child is not found |
| 92 | * or does nor represent an object, null is returned. |
| 93 | * |
| 94 | * @param parent parent object |
| 95 | * @param childName name of child to query |
| 96 | * @return child object if found, null if not found or if not an object |
| 97 | */ |
| 98 | protected static ObjectNode get(ObjectNode parent, String childName) { |
| 99 | JsonNode node = parent.path(childName); |
| 100 | return node.isObject() && !node.isNull() ? (ObjectNode) node : null; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Gets a child Object Node from a parent by index. If the child is not found |
| 105 | * or does nor represent an object, null is returned. |
| 106 | * |
| 107 | * @param parent parent object |
| 108 | * @param childIndex index of child to query |
| 109 | * @return child object if found, null if not found or if not an object |
| 110 | */ |
| 111 | protected static ObjectNode get(JsonNode parent, int childIndex) { |
| 112 | JsonNode node = parent.path(childIndex); |
| 113 | return node.isObject() && !node.isNull() ? (ObjectNode) node : null; |
| 114 | } |
Thomas Vachuska | d404c51 | 2014-10-23 14:19:46 -0700 | [diff] [blame] | 115 | } |