]> git.basschouten.com Git - openhab-addons.git/blob
f313fcf70a8d38498f00d9a9a341c4494bd0ac98
[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.mqtt.espmilighthub.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16
17 /**
18  * The {@link Helper} Removes the need for any external JSON libs
19  *
20  * @author Matthew Skinner - Initial contribution
21  */
22 @NonNullByDefault
23 public class Helper {
24     /**
25      * resolveJSON will return a value from any key/path that you give and the string can be terminated by any ,}"
26      * characters.
27      *
28      */
29     public static String resolveJSON(String messageJSON, String jsonPath, int resultMaxLength) {
30         String result = "";
31         int index = 0;
32         index = messageJSON.indexOf(jsonPath);
33         if (index != -1) {
34             if ((index + jsonPath.length() + resultMaxLength) > messageJSON.length()) {
35                 result = (messageJSON.substring(index + jsonPath.length(), messageJSON.length()));
36             } else {
37                 result = (messageJSON.substring(index + jsonPath.length(),
38                         index + jsonPath.length() + resultMaxLength));
39             }
40             index = result.indexOf(',');
41             if (index == -1) {
42                 index = result.indexOf('"');
43                 if (index == -1) {
44                     index = result.indexOf('}');
45                     if (index == -1) {
46                         return result;
47                     } else {
48                         return result.substring(0, index);
49                     }
50                 } else {
51                     return result.substring(0, index);
52                 }
53             } else {
54                 result = result.substring(0, index);
55                 index = result.indexOf('"');
56                 if (index == -1) {
57                     return result;
58                 } else {
59                     return result.substring(0, index);
60                 }
61             }
62         }
63         return "";
64     }
65 }