2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017 AT&T Intellectual Property.
\r
6 * Copyright © 2017 Amdocs
\r
7 * All rights reserved.
\r
8 * ================================================================================
\r
9 * Licensed under the Apache License, Version 2.0 (the "License");
\r
10 * you may not use this file except in compliance with the License.
\r
11 * You may obtain a copy of the License at
\r
13 * http://www.apache.org/licenses/LICENSE-2.0
\r
15 * Unless required by applicable law or agreed to in writing, software
\r
16 * distributed under the License is distributed on an "AS IS" BASIS,
\r
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
18 * See the License for the specific language governing permissions and
\r
19 * limitations under the License.
\r
20 * ============LICENSE_END=========================================================
\r
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
24 package org.onap.aai.modelloader.util;
\r
26 import org.json.JSONArray;
\r
27 import org.json.JSONException;
\r
28 import org.json.JSONObject;
\r
29 import org.json.XML;
\r
31 public class JsonXmlConverter {
\r
34 * Determines whether or not the supplied text string represents a valid
\r
35 * JSON structure or not.
\r
37 * @param text - The text to be evaluated.
\r
39 * @return - true if the string represents a valid JSON object,
\r
42 public static boolean isValidJson(String text) {
\r
44 new JSONObject(text);
\r
45 } catch (JSONException ex) {
\r
47 new JSONArray(text);
\r
48 } catch (JSONException ex1) {
\r
57 * Takes a text string representing a valid JSON structure and converts it to
\r
58 * an equivalent XML string.
\r
60 * @param jsonText - The JSON string to convert to XML.
\r
62 * @return - An XML string representation of the supplied JSON string.
\r
64 public static String convertJsonToXml(String jsonText) {
\r
65 JSONObject jsonObj = new JSONObject(jsonText);
\r
66 String xmlText = XML.toString(jsonObj);
\r
71 * Takes a text string representing a valid XML structure and converts it to
\r
72 * an equivalent JSON string.
\r
74 * @param xmlText - The XML string to convert to JSON.
\r
76 * @return - A JSON string representation of the supplied XML string.
\r
78 public static String convertXmlToJson(String xmlText) {
\r
79 JSONObject jsonObj = XML.toJSONObject(xmlText);
\r
80 return jsonObj.toString();
\r