优化JsonObject

This commit is contained in:
redkale
2023-07-15 11:09:19 +08:00
parent 6b27653a99
commit f9463c0f82
3 changed files with 19 additions and 5 deletions

View File

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

View File

@@ -34,12 +34,21 @@ public class JsonObject extends LinkedHashMap<String, Object> implements JsonEnt
return convertFrom(text, 0, text.length);
}
public static JsonObject convertFrom(char[] text, final int offset, final int length) {
public static JsonObject convertFrom(char[] text, int offset, int length) {
return (JsonObject) JsonEntityDecoder.instance.convertFrom(new JsonReader(text, offset, length));
}
public static JsonObject of(Object javaBean) {
return convertFrom(JsonConvert.root().convertTo(javaBean));
public static JsonObject of(Object bean) {
if (bean instanceof CharSequence) {
return convertFrom(bean.toString());
}
if (bean instanceof JsonObject) {
return (JsonObject) bean;
}
if (bean instanceof Map) {
return new JsonObject((Map) bean);
}
return convertFrom(JsonConvert.root().convertTo(bean));
}
public JsonObject append(String key, Object value) {
@@ -47,6 +56,11 @@ public class JsonObject extends LinkedHashMap<String, Object> implements JsonEnt
return this;
}
public JsonObject append(String key, Collection value) {
super.put(key, value == null || value instanceof JsonArray ? value : new JsonArray(value));
return this;
}
public JsonObject putObject(String key) {
JsonObject val = new JsonObject();
super.put(key, val);

View File

@@ -55,7 +55,7 @@ public class JsonString implements CharSequence, JsonEntity, Comparable<JsonStri
@Override
public int compareTo(JsonString o) {
return o == null ? -1 : CharSequence.compare(this, o.getValue());
return o == null || o.value == null ? (value == null ? 0 : 1) : (this.value == null ? -1 : this.value.compareTo(o.value));
}
@Override