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
31 * Determines whether or not the supplied text string represents a valid
\r
32 * JSON structure or not.
\r
34 * @param text - The text to be evaluated.
\r
36 * @return - true if the string represents a valid JSON object,
\r
39 public static boolean isValidJson(String text) {
\r
41 new JSONObject(text);
\r
42 } catch (JSONException ex) {
\r
44 new JSONArray(text);
\r
45 } catch (JSONException ex1) {
\r
54 * Takes a text string representing a valid JSON structure and converts it to
\r
55 * an equivalent XML string.
\r
57 * @param jsonText - The JSON string to convert to XML.
\r
59 * @return - An XML string representation of the supplied JSON string.
\r
61 public static String convertJsonToXml(String jsonText) {
\r
62 JSONObject jsonObj = new JSONObject(jsonText);
\r
63 String xmlText = XML.toString(jsonObj);
\r
68 * Takes a text string representing a valid XML structure and converts it to
\r
69 * an equivalent JSON string.
\r
71 * @param xmlText - The XML string to convert to JSON.
\r
73 * @return - A JSON string representation of the supplied XML string.
\r
75 public static String convertXmlToJson(String xmlText) {
\r
76 JSONObject jsonObj = XML.toJSONObject(xmlText);
\r
77 return jsonObj.toString();
\r