Skip to content

Commit 4f2fe11

Browse files
authored
Add tester methods to JSON abstraction (#2850)
Added default methods to JsonElement interface: - isJsonObject() - isJsonArray() - isJsonPrimitive() - isJsonNull() Also added overriding implementations in GsonElement that delegate to the underlying com.google.gson.JsonElement methods. Closes #2316
1 parent c29eedb commit 4f2fe11

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonElement.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,25 @@ public <T extends JsonElement> T autoCast() {
119119
if (element.isJsonPrimitive()) return (T) new GsonPrimitive(element.getAsJsonPrimitive());
120120
return (T) new GsonElement(element);
121121
}
122+
123+
@Override
124+
public boolean isJsonObject() {
125+
return element != null && element.isJsonObject();
126+
}
127+
128+
@Override
129+
public boolean isJsonArray() {
130+
return element != null && element.isJsonArray();
131+
}
132+
133+
@Override
134+
public boolean isJsonPrimitive() {
135+
return element != null && element.isJsonPrimitive();
136+
}
137+
138+
@Override
139+
public boolean isJsonNull() {
140+
return isNull(element);
141+
}
122142

123143
}

jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,36 @@ public interface JsonElement {
9898
* @return the casted JsonElement
9999
*/
100100
public <T extends JsonElement> T autoCast();
101+
102+
/**
103+
* Check if this element is a JsonObject
104+
* @return true if this element is a JsonObject, false otherwise
105+
*/
106+
default boolean isJsonObject() {
107+
return this instanceof JsonObject;
108+
}
109+
110+
/**
111+
* Check if this element is a JsonArray
112+
* @return true if this element is a JsonArray, false otherwise
113+
*/
114+
default boolean isJsonArray() {
115+
return this instanceof JsonArray;
116+
}
117+
118+
/**
119+
* Check if this element is a JsonPrimitive
120+
* @return true if this element is a JsonPrimitive, false otherwise
121+
*/
122+
default boolean isJsonPrimitive() {
123+
return this instanceof JsonPrimitive;
124+
}
125+
126+
/**
127+
* Check if this element is a JSON null
128+
* @return true if this element represents a null value, false otherwise
129+
*/
130+
default boolean isJsonNull() {
131+
return false;
132+
}
101133
}

0 commit comments

Comments
 (0)