Convert project from AJSC to Spring Boot
[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 European Software Marketing Ltd.\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     private JsonXmlConverter() {\r
31         throw new AssertionError("Instantiating utility class.");\r
32     }\r
33 \r
34     /**\r
35      * Determines whether or not the supplied text string represents a valid JSON structure or not.\r
36      * \r
37      * @param text The text to be evaluated.\r
38      * @return - true if the string represents a valid JSON object, false, otherwise.\r
39      */\r
40     public static boolean isValidJson(String text) {\r
41         try {\r
42             new JSONObject(text);\r
43         } catch (JSONException ex) {\r
44             try {\r
45                 new JSONArray(text);\r
46             } catch (JSONException ex1) {\r
47                 return false;\r
48             }\r
49         }\r
50 \r
51         return true;\r
52     }\r
53 \r
54     /**\r
55      * Takes a text string representing a valid JSON structure and converts it to an equivalent XML string.\r
56      * \r
57      * @param jsonText The JSON string to convert to XML.\r
58      * @return an XML string representation of the supplied JSON string.\r
59      */\r
60     public static String convertJsonToXml(String jsonText) {\r
61         return XML.toString(new JSONObject(jsonText));\r
62     }\r
63 \r
64     /**\r
65      * Takes a text string representing a valid XML structure and converts it to an equivalent JSON string.\r
66      * \r
67      * @param xmlText The XML string to convert to JSON.\r
68      * @return a JSON string representation of the supplied XML string.\r
69      */\r
70     public static String convertXmlToJson(String xmlText) {\r
71         JSONObject jsonObj = XML.toJSONObject(xmlText);\r
72         return jsonObj.toString();\r
73     }\r
74 }\r