blob: 8ac961605527336dbb92274d6384cb30135204a9 [file] [log] [blame]
Thomas Vachuskad404c512014-10-23 14:19:46 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuskad404c512014-10-23 14:19:46 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuskad404c512014-10-23 14:19:46 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuskad404c512014-10-23 14:19:46 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.codec;
Thomas Vachuskad404c512014-10-23 14:19:46 -070017
18import com.fasterxml.jackson.databind.JsonNode;
Thomas Vachuskad404c512014-10-23 14:19:46 -070019import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21
22import java.util.ArrayList;
23import java.util.List;
24
25/**
26 * Abstraction of a codec capable for encoding/decoding arbitrary objects to/from JSON.
27 */
28public abstract class JsonCodec<T> {
29
30 /**
31 * Encodes the specified entity into JSON.
32 *
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080033 * @param entity entity to encode
34 * @param context encoding context
Thomas Vachuskad404c512014-10-23 14:19:46 -070035 * @return JSON node
36 * @throws java.lang.UnsupportedOperationException if the codec does not
37 * support encode operations
38 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080039 public ObjectNode encode(T entity, CodecContext context) {
40 throw new UnsupportedOperationException("encode() not supported");
41 }
Thomas Vachuskad404c512014-10-23 14:19:46 -070042
43 /**
44 * Decodes the specified entity from JSON.
45 *
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080046 * @param json JSON to decode
47 * @param context decoding context
Thomas Vachuskad404c512014-10-23 14:19:46 -070048 * @return decoded entity
49 * @throws java.lang.UnsupportedOperationException if the codec does not
50 * support decode operations
51 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080052 public T decode(ObjectNode json, CodecContext context) {
53 throw new UnsupportedOperationException("decode() not supported");
54 }
Thomas Vachuskad404c512014-10-23 14:19:46 -070055
56 /**
57 * Encodes the collection of the specified entities.
58 *
59 * @param entities collection of entities to encode
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080060 * @param context encoding context
Thomas Vachuskad404c512014-10-23 14:19:46 -070061 * @return JSON array
62 * @throws java.lang.UnsupportedOperationException if the codec does not
63 * support encode operations
64 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080065 public ArrayNode encode(Iterable<T> entities, CodecContext context) {
66 ArrayNode result = context.mapper().createArrayNode();
Thomas Vachuskad404c512014-10-23 14:19:46 -070067 for (T entity : entities) {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080068 result.add(encode(entity, context));
Thomas Vachuskad404c512014-10-23 14:19:46 -070069 }
70 return result;
71 }
72
73 /**
74 * Decodes the specified JSON array into a collection of entities.
75 *
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080076 * @param json JSON array to decode
77 * @param context decoding context
Thomas Vachuskad404c512014-10-23 14:19:46 -070078 * @return collection of decoded entities
79 * @throws java.lang.UnsupportedOperationException if the codec does not
80 * support decode operations
81 */
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080082 public List<T> decode(ArrayNode json, CodecContext context) {
Thomas Vachuskad404c512014-10-23 14:19:46 -070083 List<T> result = new ArrayList<>();
84 for (JsonNode node : json) {
Thomas Vachuskaca60f2b2014-11-06 01:34:28 -080085 result.add(decode((ObjectNode) node, context));
Thomas Vachuskad404c512014-10-23 14:19:46 -070086 }
87 return result;
88 }
89
Ray Milkeyb82c42b2015-06-30 09:42:20 -070090 /**
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 Vachuskad404c512014-10-23 14:19:46 -0700115}