2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 European Software Marketing Ltd.
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=========================================================
21 package org.onap.aai.modelloader.util;
23 import org.json.JSONArray;
24 import org.json.JSONException;
25 import org.json.JSONObject;
28 public final class JsonXmlConverter {
30 private JsonXmlConverter() {
31 throw new AssertionError("Instantiating utility class.");
35 * Determines whether or not the supplied text string represents a valid JSON structure or not.
37 * @param text The text to be evaluated.
38 * @return - true if the string represents a valid JSON object, false, otherwise.
40 public static boolean isValidJson(String text) {
45 } catch (JSONException ex) { // NOSONAR
49 } catch (JSONException ex1) { // NOSONAR
58 * Takes a text string representing a valid JSON structure and converts it to an equivalent XML string.
60 * @param jsonText The JSON string to convert to XML.
61 * @return an XML string representation of the supplied JSON string.
63 public static String convertJsonToXml(String jsonText) {
64 return XML.toString(new JSONObject(jsonText));
68 * Takes a text string representing a valid XML structure and converts it to an equivalent JSON string.
70 * @param xmlText The XML string to convert to JSON.
71 * @return a JSON string representation of the supplied XML string.
73 public static String convertXmlToJson(String xmlText) {
74 JSONObject jsonObj = XML.toJSONObject(xmlText);
75 return jsonObj.toString();