Skip to content

Commit 876d0d3

Browse files
committed
support JSONSchema from class
1 parent 3965290 commit 876d0d3

10 files changed

Lines changed: 504 additions & 133 deletions

File tree

core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderAdapter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.lang.reflect.InvocationTargetException;
1010
import java.lang.reflect.Type;
1111
import java.util.*;
12+
import java.util.function.Consumer;
1213
import java.util.function.Function;
1314
import java.util.function.Supplier;
1415

@@ -216,6 +217,12 @@ public FieldReader[] getFieldReaders() {
216217
return Arrays.copyOf(this.fieldReaders, this.fieldReaders.length);
217218
}
218219

220+
public void apply(Consumer<FieldReader> fieldReaderConsumer) {
221+
for (FieldReader fieldReader : fieldReaders) {
222+
fieldReaderConsumer.accept(fieldReader);
223+
}
224+
}
225+
219226
public Object auoType(JSONReader jsonReader, Class expectClass, long features) {
220227
long typeHash = jsonReader.readTypeHashCode();
221228
JSONReader.Context context = jsonReader.getContext();

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

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public final class ArraySchema
1717

1818
final int maxLength;
1919
final int minLength;
20-
final JSONSchema itemSchema;
20+
JSONSchema itemSchema;
2121
final JSONSchema[] prefixItems;
2222
final boolean additionalItems;
2323
final JSONSchema additionalItem;
@@ -440,20 +440,61 @@ public ValidateResult validate(Object value) {
440440
return typed ? FAIL_TYPE_NOT_MATCH : SUCCESS;
441441
}
442442

443-
@Override
444-
public boolean equals(Object o) {
445-
if (this == o) {
446-
return true;
443+
public JSONObject toJSONObject() {
444+
JSONObject object = new JSONObject();
445+
object.put("type", "array");
446+
447+
if (maxLength != -1) {
448+
object.put("maxLength", maxLength);
447449
}
448-
if (o == null || getClass() != o.getClass()) {
449-
return false;
450+
451+
if (minLength != -1) {
452+
object.put("minLength", minLength);
450453
}
451-
com.alibaba.fastjson2.schema.ArraySchema that = (com.alibaba.fastjson2.schema.ArraySchema) o;
452-
return Objects.equals(title, that.title) && Objects.equals(description, that.description);
453-
}
454454

455-
@Override
456-
public int hashCode() {
457-
return Objects.hash(title, description);
455+
if (itemSchema != null) {
456+
object.put("items", itemSchema);
457+
}
458+
459+
if (prefixItems != null && prefixItems.length != 0) {
460+
object.put("prefixItems", prefixItems);
461+
}
462+
463+
if (!additionalItems) {
464+
object.put("additionalItems", additionalItems);
465+
}
466+
467+
if (additionalItem != null) {
468+
object.put("additionalItem", additionalItem);
469+
}
470+
471+
if (contains != null) {
472+
object.put("contains", contains);
473+
}
474+
475+
if (minContains != -1) {
476+
object.put("minContains", minContains);
477+
}
478+
479+
if (maxContains != -1) {
480+
object.put("maxContains", maxContains);
481+
}
482+
483+
if (uniqueItems) {
484+
object.put("uniqueItems", uniqueItems);
485+
}
486+
487+
if (allOf != null) {
488+
object.put("allOf", allOf);
489+
}
490+
491+
if (anyOf != null) {
492+
object.put("anyOf", anyOf);
493+
}
494+
495+
if (oneOf != null) {
496+
object.put("oneOf", oneOf);
497+
}
498+
return object;
458499
}
459500
}

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.alibaba.fastjson2.JSONObject;
44

5-
import java.util.Objects;
6-
75
final class BooleanSchema
86
extends JSONSchema {
97
BooleanSchema(JSONObject input) {
@@ -29,19 +27,7 @@ public ValidateResult validate(Object value) {
2927
}
3028

3129
@Override
32-
public boolean equals(Object o) {
33-
if (this == o) {
34-
return true;
35-
}
36-
if (o == null || getClass() != o.getClass()) {
37-
return false;
38-
}
39-
com.alibaba.fastjson2.schema.BooleanSchema that = (com.alibaba.fastjson2.schema.BooleanSchema) o;
40-
return Objects.equals(title, that.title) && Objects.equals(description, that.description);
41-
}
42-
43-
@Override
44-
public int hashCode() {
45-
return Objects.hash(title, description);
30+
public JSONObject toJSONObject() {
31+
return JSONObject.of("type", "boolean");
4632
}
4733
}

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import java.math.BigDecimal;
77
import java.math.BigInteger;
8-
import java.util.Objects;
98
import java.util.concurrent.atomic.AtomicInteger;
109
import java.util.concurrent.atomic.AtomicLong;
1110

@@ -265,25 +264,27 @@ public ValidateResult validate(Integer value) {
265264
}
266265

267266
@Override
268-
public boolean equals(Object o) {
269-
if (this == o) {
270-
return true;
267+
public JSONObject toJSONObject() {
268+
JSONObject object = new JSONObject();
269+
270+
object.put("type", "integer");
271+
272+
if (minimum != Long.MIN_VALUE) {
273+
object.put(exclusiveMinimum ? "exclusiveMinimum" : "minimum", minimum);
271274
}
272-
if (o == null || getClass() != o.getClass()) {
273-
return false;
275+
276+
if (maximum != Long.MIN_VALUE) {
277+
object.put(exclusiveMaximum ? "exclusiveMaximum" : "maximum", maximum);
274278
}
275-
com.alibaba.fastjson2.schema.IntegerSchema that = (com.alibaba.fastjson2.schema.IntegerSchema) o;
276-
return Objects.equals(title, that.title)
277-
&& Objects.equals(description, that.description)
278-
&& Objects.equals(minimum, that.minimum)
279-
&& Objects.equals(exclusiveMinimum, that.exclusiveMinimum)
280-
&& Objects.equals(maximum, that.maximum)
281-
&& Objects.equals(exclusiveMaximum, that.exclusiveMaximum)
282-
&& Objects.equals(multipleOf, that.multipleOf);
283-
}
284279

285-
@Override
286-
public int hashCode() {
287-
return Objects.hash(title, description, minimum, exclusiveMinimum, maximum, exclusiveMaximum, multipleOf);
280+
if (multipleOf != 0) {
281+
object.put("multipleOf", multipleOf);
282+
}
283+
284+
if (constValue != null) {
285+
object.put("const", constValue);
286+
}
287+
288+
return object;
288289
}
289290
}

0 commit comments

Comments
 (0)