Implement support for v10 model entities.
[aai/model-loader.git] / src / main / java / org / openecomp / modelloader / util / JsonXmlConverter.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * Model Loader\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
19  *\r
20  * ECOMP and OpenECOMP are trademarks\r
21  * and service marks of AT&T Intellectual Property.\r
22  */\r
23 package org.openecomp.modelloader.util;\r
24 \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
29 \r
30 public class JsonXmlConverter {\r
31 \r
32   /**\r
33    * Determines whether or not the supplied text string represents a valid\r
34    * JSON structure or not.\r
35    * \r
36    * @param text - The text to be evaluated.\r
37    * \r
38    * @return - true if the string represents a valid JSON object,\r
39    *           false, otherwise.\r
40    */\r
41   public static boolean isValidJson(String text) {\r
42     try {\r
43       new JSONObject(text);\r
44     } catch (JSONException ex) {\r
45       try {\r
46         new JSONArray(text);\r
47       } catch (JSONException ex1) {\r
48         return false;\r
49       }\r
50     }\r
51 \r
52     return true;\r
53   }\r
54 \r
55   /**\r
56    * Takes a text string representing a valid JSON structure and converts it to\r
57    * an equivalent XML string.\r
58    * \r
59    * @param jsonText - The JSON string to convert to XML.\r
60    * \r
61    * @return - An XML string representation of the supplied JSON string.\r
62    */\r
63   public static String convertJsonToXml(String jsonText) {\r
64     JSONObject jsonObj = new JSONObject(jsonText);\r
65     String xmlText = XML.toString(jsonObj);\r
66     return xmlText;\r
67   }\r
68 \r
69   /**\r
70    * Takes a text string representing a valid XML structure and converts it to\r
71    * an equivalent JSON string.\r
72    * \r
73    * @param xmlText - The XML string to convert to JSON.\r
74    * \r
75    * @return - A JSON string representation of the supplied XML string.\r
76    */\r
77   public static String convertXmlToJson(String xmlText) {\r
78     JSONObject jsonObj = XML.toJSONObject(xmlText);\r
79     return jsonObj.toString();\r
80   }\r
81 }\r