-
Notifications
You must be signed in to change notification settings - Fork 64
/
JsonObjectHelper.java
163 lines (139 loc) · 4.21 KB
/
JsonObjectHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* Copyright (c) 2014 Red Hat, Inc. and others
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
package io.vertx.config.spi.utils;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
/**
* Some utility methods to create json objects from a set of String.
*
* @author <a href="http://escoffier.me">Clement Escoffier</a>
*/
public class JsonObjectHelper {
/**
* Deprecated. {@link JsonObject} now has a {@link JsonObject#toBuffer()} method.
*
* @deprecated use {@link JsonObject#toBuffer()} instead
*/
@Deprecated
public static Buffer toBuffer(JsonObject json) {
return json.toBuffer();
}
public static void put(JsonObject json, String name, String value, boolean rawData) {
json.put(name, rawData ? value : convert(value));
}
public static Object convert(String value) {
Objects.requireNonNull(value);
Boolean bool = asBoolean(value);
if (bool != null) {
return bool;
}
Object number = asNumber(value);
if (number != null) {
return number;
}
JsonObject obj = asJsonObject(value);
if (obj != null) {
return obj;
}
JsonArray arr = asJsonArray(value);
if (arr != null) {
return arr;
}
return value;
}
private static Object asNumber(String s) {
try {
return new BigInteger(s);
} catch (NumberFormatException ignore) {
}
try {
return new BigDecimal(s);
} catch (NumberFormatException ignore) {
}
return null;
}
private static Boolean asBoolean(String s) {
if (s.equalsIgnoreCase("true")) {
return Boolean.TRUE;
} else if (s.equalsIgnoreCase("false")) {
return Boolean.FALSE;
} else {
return null;
}
}
private static JsonObject asJsonObject(String s) {
if (s.startsWith("{") && s.endsWith("}")) {
try {
return new JsonObject(s);
} catch (Exception e) {
return null;
}
}
return null;
}
private static JsonArray asJsonArray(String s) {
if (s.startsWith("[") && s.endsWith("]")) {
try {
return new JsonArray(s);
} catch (Exception e) {
return null;
}
} else if (!s.startsWith("[") && !s.endsWith("]") && s.contains(",")) {
// Allow comma-separated syntax
return asJsonArray("[" + s + "]");
}
return null;
}
public static JsonObject from(Properties props) {
return from(props, false);
}
public static JsonObject from(Properties props, boolean rawData) {
return from(props, rawData, false);
}
public static JsonObject from(Properties props, boolean rawData, boolean hierarchical) {
if (!hierarchical) {
JsonObject json = new JsonObject();
props.stringPropertyNames()
.forEach(name -> put(json, name, props.getProperty(name), rawData));
return json;
} else {
return props.stringPropertyNames()
.stream()
.map(path -> toJson(Arrays.asList(path.split("\\.")), props.getProperty(path), rawData))
.reduce((json, other) -> json.mergeIn(other, true))
.orElse(new JsonObject());
}
}
public static JsonObject toJson(List<String> paths, String value, boolean rawData) {
if (paths.size() == 0) {
return new JsonObject();
}
if (paths.size() == 1) {
JsonObject json = new JsonObject();
put(json, paths.get(0), value, rawData);
return json;
}
return new JsonObject().put(paths.get(0), toJson(paths.subList(1, paths.size()), value, rawData));
}
}