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
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
20 * ECOMP and OpenECOMP are trademarks
\r
21 * and service marks of AT&T Intellectual Property.
\r
23 package org.openecomp.modelloader.util;
\r
25 import org.json.JSONArray;
\r
26 import org.json.JSONException;
\r
27 import org.json.JSONObject;
\r
28 import org.json.XML;
\r
30 public class JsonXmlConverter {
\r
33 * Determines whether or not the supplied text string represents a valid
\r
34 * JSON structure or not.
\r
36 * @param text - The text to be evaluated.
\r
38 * @return - true if the string represents a valid JSON object,
\r
41 public static boolean isValidJson(String text) {
\r
43 new JSONObject(text);
\r
44 } catch (JSONException ex) {
\r
46 new JSONArray(text);
\r
47 } catch (JSONException ex1) {
\r
56 * Takes a text string representing a valid JSON structure and converts it to
\r
57 * an equivalent XML string.
\r
59 * @param jsonText - The JSON string to convert to XML.
\r
61 * @return - An XML string representation of the supplied JSON string.
\r
63 public static String convertJsonToXml(String jsonText) {
\r
64 JSONObject jsonObj = new JSONObject(jsonText);
\r
65 String xmlText = XML.toString(jsonObj);
\r
70 * Takes a text string representing a valid XML structure and converts it to
\r
71 * an equivalent JSON string.
\r
73 * @param xmlText - The XML string to convert to JSON.
\r
75 * @return - A JSON string representation of the supplied XML string.
\r
77 public static String convertXmlToJson(String xmlText) {
\r
78 JSONObject jsonObj = XML.toJSONObject(xmlText);
\r
79 return jsonObj.toString();
\r