chengfan | 9d60b6e | 2016-12-01 11:06:39 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
chengfan | 9d60b6e | 2016-12-01 11:06:39 +0800 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.provider.te.utils; |
| 18 | |
| 19 | |
| 20 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | import com.fasterxml.jackson.databind.ObjectWriter; |
| 23 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 24 | import org.slf4j.Logger; |
| 25 | |
| 26 | import java.io.IOException; |
| 27 | import java.io.InputStream; |
| 28 | |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 29 | import static org.onlab.util.Tools.readTreeFromStream; |
chengfan | 9d60b6e | 2016-12-01 11:06:39 +0800 | [diff] [blame] | 30 | import static org.slf4j.LoggerFactory.getLogger; |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Convert utility methods for IETF SB. |
| 35 | */ |
| 36 | public 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 Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame] | 54 | response = readTreeFromStream(MAPPER, stream); |
chengfan | 9d60b6e | 2016-12-01 11:06:39 +0800 | [diff] [blame] | 55 | } 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 | } |