2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.listener.util;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.List;
29 import com.fasterxml.jackson.databind.JsonNode;
30 import org.json.JSONObject;
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
34 import com.fasterxml.jackson.databind.ObjectMapper;
38 private static final EELFLogger LOG = EELFManager.getInstance().getLogger(Mapper.class);
40 private static ObjectMapper mapper = new ObjectMapper();
43 * @return The object mapper that we are using.
45 public static ObjectMapper getMapper() {
50 * Convert a String to a DcaeMessage
53 * The json string to try and parse
54 * @return A DcaeMessage from the json string or null if it could not
56 public static <T> T mapOne(String data, Class<T> cls) {
58 return mapper.readValue(data, cls);
59 } catch (Exception e) {
60 LOG.warn(String.format("Could not map [ %s ] to %s", data, cls.getName()), e);
65 public static <T> List<T> mapList(List<String> data, Class<T> cls) {
66 List<T> out = new ArrayList<T>();
67 for (String s : data) {
68 T tmp = Mapper.mapOne(s, cls);
77 * Convenience method to try and convert objects to json String
80 * The object to try and convert
81 * @return A json string representing the object or null if it could not be converted
83 public static String toJsonString(Object obj) {
86 if (obj instanceof JSONObject) {
87 jsonStr = obj.toString();
89 jsonStr = mapper.writeValueAsString(obj);
92 } catch (Exception e) {
93 LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);
98 public static JSONObject toJsonObject(Object obj) {
101 if (obj.getClass().equals(String.class)) {
102 jsonStr = (String) obj;
104 jsonStr = mapper.writeValueAsString(obj);
106 return new JSONObject(jsonStr);
107 } catch (Exception e) {
108 LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);
112 public static JsonNode toJsonNodeFromJsonString(String jsonStr) {
113 JsonNode jsonNode = null;
114 if(jsonStr != null) {
116 jsonNode = mapper.readTree(jsonStr);
117 } catch (IOException e) {
118 LOG.warn(String.format("Could not map %s to jsonNode.", jsonStr), e);
123 public static JsonNode toJsonNode(Object obj) {
124 JsonNode jsonNode = null;
125 String jsonStr = toJsonString(obj);
126 if(jsonStr != null) {
128 jsonNode = mapper.readTree(jsonStr);
129 } catch (IOException e) {
130 LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);