优化jsonobject

This commit is contained in:
redkale
2023-07-21 14:55:09 +08:00
parent ede8f40b0d
commit f9bee11001
5 changed files with 13 additions and 3 deletions

View File

@@ -39,7 +39,7 @@ public class JsonArray extends ArrayList<Object> implements JsonEntity {
}
public static JsonArray convertFrom(char[] text, int offset, int length) {
return (JsonArray) JsonEntityDecoder.instance.convertFrom(new JsonReader(text, offset, length));
return JsonConvert.root().convertFrom(JsonArray.class, text, offset, length);
}
public JsonArray append(Object value) {

View File

@@ -31,7 +31,7 @@ public interface JsonEntity extends java.io.Serializable {
}
public static JsonEntity convertFrom(char[] text, final int offset, final int length) {
Object val = JsonEntityDecoder.instance.convertFrom(new JsonReader(text, offset, length));
Object val = JsonConvert.root().convertFrom(JsonEntity.class, text, offset, length);
if (val instanceof CharSequence) {
return new JsonString(val.toString());
}

View File

@@ -45,6 +45,11 @@ public final class JsonFactory extends ConvertFactory<JsonReader, JsonWriter> {
this.register(java.time.LocalDate.class, LocalDateSimpledCoder.LocalDateJsonSimpledCoder.instance);
this.register(java.time.LocalTime.class, LocalTimeSimpledCoder.LocalTimeJsonSimpledCoder.instance);
this.register(java.time.LocalDateTime.class, LocalDateTimeSimpledCoder.LocalDateTimeJsonSimpledCoder.instance);
this.register(JsonEntity.class, (Decodeable) JsonEntityDecoder.instance);
this.register(JsonString.class, (Decodeable) JsonEntityDecoder.instance);
this.register(JsonObject.class, (Decodeable) JsonEntityDecoder.instance);
this.register(JsonArray.class, (Decodeable) JsonEntityDecoder.instance);
}
}

View File

@@ -35,7 +35,7 @@ public class JsonObject extends LinkedHashMap<String, Object> implements JsonEnt
}
public static JsonObject convertFrom(char[] text, int offset, int length) {
return (JsonObject) JsonEntityDecoder.instance.convertFrom(new JsonReader(text, offset, length));
return JsonConvert.root().convertFrom(JsonObject.class, text, offset, length);
}
public static JsonObject of(Object bean) {

View File

@@ -27,6 +27,7 @@ public class JsonMainTest {
test.run3();
test.run4();
test.run5();
test.run6();
}
@Test
@@ -137,6 +138,10 @@ public class JsonMainTest {
+ " ]"
+ "}";
JsonObject obj = JsonObject.convertFrom(str);
JsonObject obj2 = JsonConvert.root().convertFrom(JsonObject.class, str);
System.out.println("结果1: " + obj);
System.out.println("结果2: " + obj2);
System.out.println("结果3: " + JsonConvert.root().convertTo(obj2));
Assertions.assertEquals(JsonObject.class.getName(), obj.get("media").getClass().getName());
Assertions.assertEquals(JsonArray.class.getName(), obj.get("images").getClass().getName());
}