blob: 5dcf25c15bc6219e9c40d476cf095f70c33a8c05 [file] [log] [blame]
Thomas Vachuskad404c512014-10-23 14:19:46 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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 */
16package org.onlab.onos.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22
23import java.util.ArrayList;
24import java.util.List;
25
26/**
27 * Abstraction of a codec capable for encoding/decoding arbitrary objects to/from JSON.
28 */
29public 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}