Skip to content

Commit 8aab7c6

Browse files
committed
support JSONSchema from value
1 parent 876d0d3 commit 8aab7c6

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

core/src/main/java/com/alibaba/fastjson2/schema/JSONSchema.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.net.URL;
1717
import java.net.URLDecoder;
1818
import java.util.Collection;
19+
import java.util.Iterator;
1920
import java.util.Map;
2021
import java.util.concurrent.ConcurrentHashMap;
2122
import java.util.concurrent.atomic.AtomicBoolean;
@@ -173,7 +174,36 @@ public static JSONSchema of(java.lang.reflect.Type type) {
173174
return of(type, null);
174175
}
175176

176-
public static JSONSchema of(java.lang.reflect.Type type, JSONSchema root) {
177+
public static JSONSchema ofValue(Object value) {
178+
return ofValue(value, null);
179+
}
180+
181+
static JSONSchema ofValue(Object value, JSONSchema root) {
182+
if (value == null) {
183+
return null;
184+
}
185+
186+
if (value instanceof Map) {
187+
JSONObject object = JSONObject.of("type", "object");
188+
ObjectSchema schema = new ObjectSchema(object, root);
189+
190+
Map map = (Map) value;
191+
for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
192+
Map.Entry entry = (Map.Entry) it.next();
193+
Object entryKey = entry.getKey();
194+
if (entryKey instanceof String) {
195+
JSONSchema valueSchema = ofValue(entry.getValue(), root == null ? schema : root);
196+
schema.properties.put((String) entryKey, valueSchema);
197+
}
198+
}
199+
200+
return schema;
201+
}
202+
203+
return of(value.getClass(), root);
204+
}
205+
206+
static JSONSchema of(java.lang.reflect.Type type, JSONSchema root) {
177207
if (type instanceof ParameterizedType) {
178208
ParameterizedType paramType = (ParameterizedType) type;
179209

core/src/test/java/com/alibaba/fastjson2/schema/FromClass.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
import java.math.BigDecimal;
1010
import java.math.BigInteger;
11-
import java.util.Collection;
12-
import java.util.Map;
11+
import java.util.*;
1312
import java.util.concurrent.atomic.AtomicBoolean;
1413
import java.util.concurrent.atomic.AtomicInteger;
1514
import java.util.concurrent.atomic.AtomicLong;
@@ -28,6 +27,10 @@ public void test() {
2827
);
2928
JSONSchema pased = JSONSchema.of(JSON.parseObject(string));
3029
assertTrue(Differ.diff(schema, pased));
30+
31+
Bean bean = new Bean();
32+
JSONSchema valueSchema = JSONSchema.ofValue(bean);
33+
assertTrue(Differ.diff(schema, valueSchema));
3134
}
3235

3336
public static class Bean {
@@ -87,4 +90,18 @@ public void testEishay() {
8790
JSONSchema pased = JSONSchema.of(JSON.parseObject(string));
8891
assertTrue(Differ.diff(schema, pased));
8992
}
93+
94+
@Test
95+
public void fromValueMap() {
96+
Map map = new HashMap();
97+
map.put("id", 123);
98+
assertEquals("{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"}}}", JSONSchema.ofValue(map).toString());
99+
}
100+
101+
@Test
102+
public void fromValueList() {
103+
List list = new ArrayList();
104+
list.add("xxx");
105+
assertEquals("{\"type\":\"array\"}", JSONSchema.ofValue(list).toString());
106+
}
90107
}

0 commit comments

Comments
 (0)