Skip to content

Commit a7a3dc0

Browse files
committed
Added StructuredJSON interface
1 parent 1efab19 commit a7a3dc0

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

src/main/java/dev/latvian/apps/json/JSONArray.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.Collection;
55
import java.util.List;
66

7-
public class JSONArray extends ArrayList<Object> implements JSONSerializable {
7+
public class JSONArray extends ArrayList<Object> implements StructuredJSON, JSONSerializable {
88
public static JSONArray of() {
99
return new JSONArray(4);
1010
}
@@ -49,6 +49,16 @@ public final Object toJSON() {
4949
return this;
5050
}
5151

52+
@Override
53+
public JSONObject asObject() {
54+
throw new ClassCastException("JSON object expected");
55+
}
56+
57+
@Override
58+
public JSONArray asArray() {
59+
return this;
60+
}
61+
5262
public <T> List<T> cast() {
5363
return (List) this;
5464
}

src/main/java/dev/latvian/apps/json/JSONObject.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.LinkedHashMap;
44
import java.util.Map;
55

6-
public class JSONObject extends LinkedHashMap<String, Object> implements JSONSerializable {
6+
public class JSONObject extends LinkedHashMap<String, Object> implements JSONSerializable, StructuredJSON {
77
public static JSONObject of() {
88
return new JSONObject(8);
99
}
@@ -46,6 +46,16 @@ public final Object toJSON() {
4646
return this;
4747
}
4848

49+
@Override
50+
public JSONObject asObject() {
51+
return this;
52+
}
53+
54+
@Override
55+
public JSONArray asArray() {
56+
throw new ClassCastException("JSON array expected");
57+
}
58+
4959
@Override
5060
public Object get(Object key) {
5161
var o = super.get(key);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package dev.latvian.apps.json;
2+
3+
public interface StructuredJSON {
4+
JSONObject asObject();
5+
6+
JSONArray asArray();
7+
}

0 commit comments

Comments
 (0)