2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.modelloader.util;
23 import org.json.JSONArray;
24 import org.json.JSONException;
25 import org.json.JSONObject;
28 public class JsonXmlConverter {
31 * Determines whether or not the supplied text string represents a valid
32 * JSON structure or not.
34 * @param text - The text to be evaluated.
36 * @return - true if the string represents a valid JSON object,
39 public static boolean isValidJson(String text) {
42 } catch (JSONException ex) {
45 } catch (JSONException ex1) {
54 * Takes a text string representing a valid JSON structure and converts it to
55 * an equivalent XML string.
57 * @param jsonText - The JSON string to convert to XML.
59 * @return - An XML string representation of the supplied JSON string.
61 public static String convertJsonToXml(String jsonText) {
62 JSONObject jsonObj = new JSONObject(jsonText);
63 String xmlText = XML.toString(jsonObj);
68 * Takes a text string representing a valid XML structure and converts it to
69 * an equivalent JSON string.
71 * @param xmlText - The XML string to convert to JSON.
73 * @return - A JSON string representation of the supplied XML string.
75 public static String convertXmlToJson(String xmlText) {
76 JSONObject jsonObj = XML.toJSONObject(xmlText);
77 return jsonObj.toString();