2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.listener.util;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.List;
31 import com.fasterxml.jackson.databind.JsonNode;
32 import org.json.JSONObject;
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
36 import com.fasterxml.jackson.databind.ObjectMapper;
40 private static final EELFLogger LOG = EELFManager.getInstance().getLogger(Mapper.class);
42 private static ObjectMapper mapper = new ObjectMapper();
45 * @return The object mapper that we are using.
47 public static ObjectMapper getMapper() {
52 * Convert a String to a DcaeMessage
55 * The json string to try and parse
56 * @return A DcaeMessage from the json string or null if it could not
58 public static <T> T mapOne(String data, Class<T> cls) {
60 return mapper.readValue(data, cls);
61 } catch (Exception e) {
62 LOG.warn(String.format("Could not map [ %s ] to %s", data, cls.getName()), e);
67 public static <T> List<T> mapList(List<String> data, Class<T> cls) {
68 List<T> out = new ArrayList<T>();
69 for (String s : data) {
70 T tmp = Mapper.mapOne(s, cls);
79 * Convenience method to try and convert objects to json String
82 * The object to try and convert
83 * @return A json string representing the object or null if it could not be converted
85 public static String toJsonString(Object obj) {
88 if (obj instanceof JSONObject) {
89 jsonStr = obj.toString();
91 jsonStr = mapper.writeValueAsString(obj);
94 } catch (Exception e) {
95 LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);
100 public static JSONObject toJsonObject(Object obj) {
103 if (obj.getClass().equals(String.class)) {
104 jsonStr = (String) obj;
106 jsonStr = mapper.writeValueAsString(obj);
108 return new JSONObject(jsonStr);
109 } catch (Exception e) {
110 LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);
114 public static JsonNode toJsonNodeFromJsonString(String jsonStr) {
115 JsonNode jsonNode = null;
116 if(jsonStr != null) {
118 jsonNode = mapper.readTree(jsonStr);
119 } catch (IOException e) {
120 LOG.warn(String.format("Could not map %s to jsonNode.", jsonStr), e);
125 public static JsonNode toJsonNode(Object obj) {
126 JsonNode jsonNode = null;
127 String jsonStr = toJsonString(obj);
128 if(jsonStr != null) {
130 jsonNode = mapper.readTree(jsonStr);
131 } catch (IOException e) {
132 LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);