blob: d10c877170f9d6f21e3972cece99c06e3efd8736 [file] [log] [blame]
chengfan9d60b6e2016-12-01 11:06:39 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
chengfan9d60b6e2016-12-01 11:06:39 +08003 *
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
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,
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.
15 */
16
17package org.onosproject.provider.te.utils;
18
19
20import com.fasterxml.jackson.core.JsonProcessingException;
21import com.fasterxml.jackson.databind.ObjectMapper;
22import com.fasterxml.jackson.databind.ObjectWriter;
23import com.fasterxml.jackson.databind.node.ObjectNode;
24import org.slf4j.Logger;
25
26import java.io.IOException;
27import java.io.InputStream;
28
Ray Milkey86ee5e82018-04-02 15:33:07 -070029import static org.onlab.util.Tools.readTreeFromStream;
chengfan9d60b6e2016-12-01 11:06:39 +080030import static org.slf4j.LoggerFactory.getLogger;
31
32
33/**
34 * Convert utility methods for IETF SB.
35 */
36public final class CodecTools {
37 private static final Logger log = getLogger(CodecTools.class);
38 private static final ObjectMapper MAPPER = new ObjectMapper();
39
40 //no instantiation
41 private CodecTools() {
42 }
43
44 /**
45 * Returns an object node from a InputStream type input which usually comes
46 * from the HTTP response.
47 *
48 * @param stream stream data comes from a HTTP response
49 * @return object node
50 */
51 public static ObjectNode toJson(InputStream stream) {
52 ObjectNode response = null;
53 try {
Ray Milkey86ee5e82018-04-02 15:33:07 -070054 response = readTreeFromStream(MAPPER, stream);
chengfan9d60b6e2016-12-01 11:06:39 +080055 } catch (IOException e) {
56 log.error("Parse json string failed {}", e.getMessage());
57 }
58
59 return response;
60 }
61
62 /**
63 * Returns an object node from a string.
64 *
65 * @param jsonString string with JSON format
66 * @return object node
67 */
68 public static ObjectNode toJson(String jsonString) {
69 ObjectNode response = null;
70 try {
71 response = (ObjectNode) MAPPER.readTree(jsonString);
72 } catch (IOException e) {
73 log.error("Parse json string failed {}", e.getMessage());
74 }
75
76 return response;
77 }
78
79 /**
80 * Returns a JSON format string from a Jackson object node.
81 *
82 * @param node JSON object node
83 * @return string with JSON format
84 */
85 public static String jsonToString(ObjectNode node) {
86 ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
87 String jsonString = null;
88 try {
89 jsonString = ow.writeValueAsString(node);
90 } catch (JsonProcessingException e) {
91 log.error("Parse json to string failed {}", e.getMessage());
92 }
93
94 return jsonString;
95 }
96}