Convert Sparky to Spring-Boot
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / util / JsonXmlConverter.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.sparky.util;
26
27 import org.json.JSONArray;
28 import org.json.JSONException;
29 import org.json.JSONObject;
30 import org.json.XML;
31
32 /**
33  * The Class JsonXmlConverter.
34  */
35 public class JsonXmlConverter {
36
37   /**
38    * Checks if is valid json.
39    *
40    * @param text the text
41    * @return true, if is valid json
42    */
43   public static boolean isValidJson(String text) {
44     try {
45       new JSONObject(text);
46     } catch (JSONException ex) {
47       try {
48         new JSONArray(text);
49       } catch (JSONException ex1) {
50         return false;
51       }
52     }
53
54     return true;
55   }
56
57   /**
58    * Convert jsonto xml.
59    *
60    * @param jsonText the json text
61    * @return the string
62    */
63   public static String convertJsontoXml(String jsonText) {
64     JSONObject jsonObj = new JSONObject(jsonText);
65     String xmlText = XML.toString(jsonObj);
66     return xmlText;
67   }
68
69   /**
70    * Convert xmlto json.
71    *
72    * @param xmlText the xml text
73    * @return the string
74    */
75   public static String convertXmltoJson(String xmlText) {
76     JSONObject jsonObj = XML.toJSONObject(xmlText);
77     return jsonObj.toString();
78   }
79 }