2 * ============LICENSE_START==========================================
\r
4 * ===================================================================
\r
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
\r
6 * Copyright © 2017-2018 Amdocs
\r
7 * ===================================================================
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
19 * ============LICENSE_END============================================
\r
21 package org.onap.aai.modelloader.util;
\r
23 import org.json.JSONArray;
\r
24 import org.json.JSONException;
\r
25 import org.json.JSONObject;
\r
26 import org.json.XML;
\r
28 public class JsonXmlConverter {
\r
30 private JsonXmlConverter() {
\r
31 throw new AssertionError("Instantiating utility class.");
\r
35 * Determines whether or not the supplied text string represents a valid JSON structure or not.
\r
37 * @param text The text to be evaluated.
\r
38 * @return - true if the string represents a valid JSON object, false, otherwise.
\r
40 public static boolean isValidJson(String text) {
\r
42 new JSONObject(text);
\r
43 } catch (JSONException ex) {
\r
45 new JSONArray(text);
\r
46 } catch (JSONException ex1) {
\r
55 * Takes a text string representing a valid JSON structure and converts it to an equivalent XML string.
\r
57 * @param jsonText The JSON string to convert to XML.
\r
58 * @return an XML string representation of the supplied JSON string.
\r
60 public static String convertJsonToXml(String jsonText) {
\r
61 return XML.toString(new JSONObject(jsonText));
\r
65 * Takes a text string representing a valid XML structure and converts it to an equivalent JSON string.
\r
67 * @param xmlText The XML string to convert to JSON.
\r
68 * @return a JSON string representation of the supplied XML string.
\r
70 public static String convertXmlToJson(String xmlText) {
\r
71 JSONObject jsonObj = XML.toJSONObject(xmlText);
\r
72 return jsonObj.toString();
\r