]> git.basschouten.com Git - openhab-addons.git/blob
bbddbc4ae761673a239156212a091c16e28683c8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.opensprinkler.internal.util;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19
20 import com.google.gson.JsonArray;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonObject;
23 import com.google.gson.JsonParser;
24
25 /**
26  * The {@link Parse} class contains static methods for parsing JSON
27  * output based on key names.
28  *
29  * @author Chris Graham - Initial contribution
30  */
31 @NonNullByDefault
32 public class Parse {
33     /**
34      * Parses an integer from a JSON string given its key name.
35      *
36      * @param jsonData The JSON formatted string to parse from.
37      * @param keyName The name of the object data to return.
38      * @return int value of the objects data.
39      */
40     public static int jsonInt(String jsonData, String keyName) {
41         JsonElement jelement = JsonParser.parseString(jsonData);
42         JsonObject jobject = jelement.getAsJsonObject();
43         jelement = jobject.get(keyName);
44         if (jelement == null) {
45             return 0;// prevents a NPE if the key does not exist.
46         }
47         return jelement.getAsInt();
48     }
49
50     /**
51      * Parses a string from a JSON string given its key name.
52      *
53      * @param jsonData The JSON formatted string to parse from.
54      * @param keyName The name of the object data to return.
55      * @return String value of the objects data.
56      */
57     public static String jsonString(String jsonData, String keyName) {
58         JsonElement jelement = JsonParser.parseString(jsonData);
59         JsonObject jobject = jelement.getAsJsonObject();
60         return jobject.get(keyName).getAsString();
61     }
62
63     /**
64      * Parses an int from a JSON array given its key name in the JSON string.
65      *
66      * @param jsonData The JSON formatted string to parse from.
67      * @param keyName The name of the object array to search through.
68      * @param index Index (starting at 0) number of the item in the JSON array to return.
69      * @return int value of the objects data.
70      */
71     public static int jsonIntAtArrayIndex(String jsonData, String keyName, int index) {
72         JsonElement jelement = JsonParser.parseString(jsonData);
73         JsonObject jobject = jelement.getAsJsonObject();
74         JsonArray jarray = jobject.get(keyName).getAsJsonArray();
75         return jarray.get(index).getAsInt();
76     }
77
78     /**
79      * Parses a String from a JSON array given its key name in the JSON string.
80      *
81      * @param jsonData The JSON formatted string to parse from.
82      * @param keyName The name of the object array to search through.
83      * @param index Index (starting at 0) number of the item in the JSON array to return.
84      * @return String value of the objects data.
85      */
86     public static String jsonStringAtArrayIndex(String jsonData, String keyName, int index) {
87         JsonElement jelement = JsonParser.parseString(jsonData);
88         JsonObject jobject = jelement.getAsJsonObject();
89         JsonArray jarray = jobject.get(keyName).getAsJsonArray();
90         return jarray.get(index).getAsString();
91     }
92
93     /**
94      * Parses an int array from a JSON string given its key name.
95      *
96      * @param jsonData The JSON formatted string to parse from.
97      * @param keyName The name of the object array to return.
98      * @return List of Integers with the values of a JSON Array.
99      */
100     public static List<Integer> jsonIntArray(String jsonData, String keyName) {
101         List<Integer> returnList = new ArrayList<>();
102
103         JsonElement jelement = JsonParser.parseString(jsonData);
104         JsonObject jobject = jelement.getAsJsonObject();
105         JsonArray jarray = jobject.get(keyName).getAsJsonArray();
106
107         for (int i = 0; i < jarray.size(); i++) {
108             returnList.add(jarray.get(i).getAsInt());
109         }
110
111         return returnList;
112     }
113
114     /**
115      * Parses a String array from a JSON string given its key name.
116      *
117      * @param jsonData The JSON formatted string to parse from.
118      * @param keyName The name of the object array to search through.
119      * @return List of Strings with the values of a JSON Array.
120      */
121     public static List<String> jsonStringArray(String jsonData, String keyName) {
122         List<String> returnList = new ArrayList<>();
123
124         JsonElement jelement = JsonParser.parseString(jsonData);
125         JsonObject jobject = jelement.getAsJsonObject();
126         JsonArray jarray = jobject.get(keyName).getAsJsonArray();
127
128         for (int i = 0; i < jarray.size(); i++) {
129             returnList.add(jarray.get(i).getAsString());
130         }
131
132         return returnList;
133     }
134 }