Fix adding null json values (#818)

This commit is contained in:
Luck 2018-03-12 19:01:38 +00:00
parent 5db3820df4
commit 1689a8abe2
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 28 additions and 5 deletions

View File

@ -27,6 +27,7 @@ package me.lucko.luckperms.common.utils.gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonPrimitive;
import java.util.function.Consumer;
@ -40,17 +41,26 @@ public class JArray implements JElement {
return this.array;
}
public JArray add(String value) {
this.array.add(new JsonPrimitive(value));
return this;
}
public JArray add(JsonElement value) {
if (value == null) {
return add(JsonNull.INSTANCE);
}
this.array.add(value);
return this;
}
public JArray add(String value) {
if (value == null) {
return add(JsonNull.INSTANCE);
}
this.array.add(new JsonPrimitive(value));
return this;
}
public JArray add(JElement value) {
if (value == null) {
return add(JsonNull.INSTANCE);
}
return add(value.toJson());
}

View File

@ -26,6 +26,7 @@
package me.lucko.luckperms.common.utils.gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
@ -46,18 +47,30 @@ public class JObject implements JElement {
}
public JObject add(String key, String value) {
if (value == null) {
return add(key, JsonNull.INSTANCE);
}
return add(key, new JsonPrimitive(value));
}
public JObject add(String key, Number value) {
if (value == null) {
return add(key, JsonNull.INSTANCE);
}
return add(key, new JsonPrimitive(value));
}
public JObject add(String key, Boolean value) {
if (value == null) {
return add(key, JsonNull.INSTANCE);
}
return add(key, new JsonPrimitive(value));
}
public JObject add(String key, JElement value) {
if (value == null) {
return add(key, JsonNull.INSTANCE);
}
return add(key, value.toJson());
}