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 17, 2022
1 parent 24847e1 commit e4497d2
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class JSONSchemaBenchmark {
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);
final static JSONSchema SCHEMA_INTEGER = JSONObject.of("type", "integer", "minimum", 10).to(JSONSchema::of);

@Benchmark
public void format_uuid(Blackhole bh) {
Expand Down Expand Up @@ -43,12 +44,17 @@ public void format_time(Blackhole bh) {

public static void format_perf() {
long start = System.currentTimeMillis();
for (int i = 0; i < 1000 * 1000 * 10; ++i) {
for (int i = 0; i < 1000 * 1000 * 100; ++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); //
// SCHEMA_NUMBER.isValid(9); // 42
// SCHEMA_NUMBER.isValid(11); // 302 120
// SCHEMA_NUMBER.isValid(11D); //
SCHEMA_NUMBER.isValid(9D); //
// SCHEMA_INTEGER.isValid(9); // 87
// SCHEMA_INTEGER.isValid(11); //
}
long millis = System.currentTimeMillis() - start;
System.out.println("millis : " + millis);
Expand Down
43 changes: 39 additions & 4 deletions core/src/main/java/com/alibaba/fastjson2/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ public boolean containsKey(Object key) {
*/
@SuppressWarnings("unchecked")
public Object getOrDefault(String key, Object defaultValue) {
return super.getOrDefault(
key, defaultValue
);
return super.getOrDefault(key, defaultValue);
}

/**
Expand Down Expand Up @@ -515,6 +513,43 @@ public long getLongValue(String key) {
throw new JSONException("Can not cast '" + value.getClass() + "' to long value");
}

/**
* Returns a long value of the associated keys in this {@link JSONObject}.
*
* @param key the key whose associated value is to be returned
* @param defaultValue the default mapping of the key
* @return long
* @throws NumberFormatException If the value of get is {@link String} and it contains no parsable long
* @throws JSONException Unsupported type conversion to long value
*/
public long getLongValue(String key, long defaultValue) {
Object value = super.get(key);

if (value == null) {
return defaultValue;
}

if (value instanceof Number) {
return ((Number) value).longValue();
}

if (value instanceof String) {
String str = (String) value;

if (str.isEmpty() || "null".equalsIgnoreCase(str)) {
return defaultValue;
}

if (str.indexOf('.') != -1) {
return (long) Double.parseDouble(str);
}

return Long.parseLong(str);
}

throw new JSONException("Can not cast '" + value.getClass() + "' to long value");
}

/**
* Returns the {@link Integer} of the associated keys in this {@link JSONObject}.
*
Expand Down Expand Up @@ -615,7 +650,7 @@ public int getIntValue(String key, int defaultValue) {
String str = (String) value;

if (str.isEmpty() || "null".equalsIgnoreCase(str)) {
return 0;
return defaultValue;
}

if (str.indexOf('.') != -1) {
Expand Down
Loading

0 comments on commit e4497d2

Please sign in to comment.