Update license date and text
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / util / JsonXmlConverter.java
1 /**\r
2  * ============LICENSE_START==========================================\r
3  * org.onap.aai\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
11  *\r
12  *        http://www.apache.org/licenses/LICENSE-2.0\r
13  *\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
20  */\r
21 package org.onap.aai.modelloader.util;\r
22 \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
27 \r
28 public class JsonXmlConverter {\r
29 \r
30   /**\r
31    * Determines whether or not the supplied text string represents a valid\r
32    * JSON structure or not.\r
33    * \r
34    * @param text - The text to be evaluated.\r
35    * \r
36    * @return - true if the string represents a valid JSON object,\r
37    *           false, otherwise.\r
38    */\r
39   public static boolean isValidJson(String text) {\r
40     try {\r
41       new JSONObject(text);\r
42     } catch (JSONException ex) {\r
43       try {\r
44         new JSONArray(text);\r
45       } catch (JSONException ex1) {\r
46         return false;\r
47       }\r
48     }\r
49 \r
50     return true;\r
51   }\r
52 \r
53   /**\r
54    * Takes a text string representing a valid JSON structure and converts it to\r
55    * an equivalent XML string.\r
56    * \r
57    * @param jsonText - The JSON string to convert to XML.\r
58    * \r
59    * @return - An XML string representation of the supplied JSON string.\r
60    */\r
61   public static String convertJsonToXml(String jsonText) {\r
62     JSONObject jsonObj = new JSONObject(jsonText);\r
63     String xmlText = XML.toString(jsonObj);\r
64     return xmlText;\r
65   }\r
66 \r
67   /**\r
68    * Takes a text string representing a valid XML structure and converts it to\r
69    * an equivalent JSON string.\r
70    * \r
71    * @param xmlText - The XML string to convert to JSON.\r
72    * \r
73    * @return - A JSON string representation of the supplied XML string.\r
74    */\r
75   public static String convertXmlToJson(String xmlText) {\r
76     JSONObject jsonObj = XML.toJSONObject(xmlText);\r
77     return jsonObj.toString();\r
78   }\r
79 }\r