Skip to content

Commit

Permalink
support json schema #239
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 16, 2022
1 parent fb510ea commit b71baf8
Show file tree
Hide file tree
Showing 71 changed files with 1,885 additions and 568 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.alibaba.fastjson2.benchmark.schema;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONSchema;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.RunnerException;

public class JSONSchemaBenchmark {
final static JSONSchema SCHEMA_UUID = JSONObject.of("type", "string", "format", "uuid").to(JSONSchema::of);
final static JSONSchema SCHEMA_DATETIME = JSONObject.of("type", "string", "format", "date-time").to(JSONSchema::of);
final static JSONSchema SCHEMA_DATE = JSONObject.of("type", "string", "format", "date").to(JSONSchema::of);
final static JSONSchema SCHEMA_TIME = JSONObject.of("type", "string", "format", "time").to(JSONSchema::of);
final static JSONSchema SCHEMA_NUMBER = JSONObject.of("type", "number", "minimum", 10).to(JSONSchema::of);

@Benchmark
public void format_uuid(Blackhole bh) {
bh.consume(
SCHEMA_UUID.isValid("a7f41390-39a9-4ca6-a13b-88cf07a41108")
);
}

@Benchmark
public void format_datetime(Blackhole bh) {
bh.consume(
SCHEMA_DATETIME.isValid("2017-07-21 12:13:14")
);
}

@Benchmark
public void format_date(Blackhole bh) {
bh.consume(
SCHEMA_DATE.isValid("2017-07-21")
);
}

@Benchmark
public void format_time(Blackhole bh) {
bh.consume(
SCHEMA_TIME.isValid("12:13:14")
);
}

public static void format_perf() {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000 * 10; ++i) {
// SCHEMA_UUID.isValid("a7f41390-39a9-4ca6-a13b-88cf07a41108");
// SCHEMA_DATETIME.isValid("2017-07-21 12:13:14"); // 123
// SCHEMA_DATE.isValid("2017-07-21"); // 48
// SCHEMA_TIME.isValid("12:13:14"); //
SCHEMA_NUMBER.isValid(9); //
}
long millis = System.currentTimeMillis() - start;
System.out.println("millis : " + millis);
// zulu17.32.13 :
// zulu11.52.13 :
// zulu8.58.0.13 :
}

public static void format_perf_test() {
for (int i = 0; i < 10; i++) {
format_perf();
}
}

public static void main(String[] args) throws RunnerException {
format_perf_test();
//
// Options options = new OptionsBuilder()
// .include(JSONSchemaBenchmark.class.getName())
// .mode(Mode.Throughput)
// .timeUnit(TimeUnit.MILLISECONDS)
// .forks(1)
// .build();
// new Runner(options).run();
}
}
6 changes: 4 additions & 2 deletions core/src/main/java/com/alibaba/fastjson2/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -1236,8 +1236,10 @@ public JSONArray fluentAddAll(Collection<?> c) {
/**
* @since 2.0.3
*/
public void validate(JSONSchema schema) {
schema.validate(this);
public boolean isValid(JSONSchema schema) {
return schema
.validate(this)
.isSuccess();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/com/alibaba/fastjson2/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -1537,8 +1537,8 @@ public JSONObject fluentPut(String key, Object value) {
/**
* @since 2.0.4
*/
public void validate(JSONSchema schema) {
schema.validate(this);
public boolean isValid(JSONSchema schema) {
return schema.isValid(this);
}

/**
Expand Down
74 changes: 74 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,39 @@ public BigDecimal readBigDecimal() {

public abstract UUID readUUID();

public boolean isLocalDate() {
if (!isString()) {
return false;
}

LocalDateTime localDateTime;
int len = getStringLength();
switch (len) {
case 8:
localDateTime = readLocalDate8();
break;
case 9:
localDateTime = readLocalDate9();
break;
case 10:
localDateTime = readLocalDate10();
break;
case 11:
localDateTime = readLocalDate11();
break;
default:
return false;
}

if (localDateTime == null) {
return false;
}
return localDateTime.getHour() == 0
&& localDateTime.getMinute() == 0
&& localDateTime.getSecond() == 0
&& localDateTime.getNano() == 0;
}

public LocalDate readLocalDate() {
if (isInt()) {
long millis = readInt64Value();
Expand Down Expand Up @@ -691,6 +724,37 @@ public LocalDate readLocalDate() {
throw new JSONException("not support input : " + str);
}

public boolean isLocalDateTime() {
if (!isString()) {
return false;
}

int len = getStringLength();
switch (len) {
case 16:
return readLocalDateTime16() != null;
case 17:
return readLocalDateTime17() != null;
case 18:
return readLocalDateTime18() != null;
case 19:
return readLocalDateTime19() != null;
case 21:
case 22:
case 23:
case 24:
case 25:
case 26:
case 27:
case 28:
case 29:
return readLocalDateTimeX(len) != null;
default:
break;
}
return false;
}

public LocalDateTime readLocalDateTime() {
if (isInt()) {
long millis = readInt64Value();
Expand Down Expand Up @@ -1592,6 +1656,16 @@ public static JSONReader of(char[] chars) {
, chars.length);
}

public static JSONReader of(Context context, char[] chars) {
return new JSONReaderUTF16(
context,
null,
chars,
0,
chars.length
);
}

public static JSONReader ofJSONB(byte[] jsonbBytes) {
return new JSONReaderJSONB(
JSONFactory.createReadContext()
Expand Down
12 changes: 10 additions & 2 deletions core/src/main/java/com/alibaba/fastjson2/JSONReaderUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -1951,7 +1951,11 @@ public UUID readUUID() {
long lsb4 = parse4Nibbles(chars, offset + 28);
if ((msb1 | msb2 | msb3 | msb4 | lsb1 | lsb2 | lsb3 | lsb4) >= 0) {
offset += 33;
ch = chars[offset++];
if (offset < end) {
ch = chars[offset++];
} else {
ch = EOI;
}

if (ch == ',') {
next();
Expand All @@ -1977,7 +1981,11 @@ public UUID readUUID() {
long lsb4 = parse4Nibbles(chars, offset + 32);
if ((msb1 | msb2 | msb3 | msb4 | lsb1 | lsb2 | lsb3 | lsb4) >= 0) {
offset += 37;
ch = chars[offset++];
if (offset < end) {
ch = chars[offset++];
} else {
ch = EOI;
}

if (ch == ',') {
next();
Expand Down
Loading

0 comments on commit b71baf8

Please sign in to comment.