Skip to content

Commit e83ec77

Browse files
committed
Changed: JSONObject.create(T) behavior...
JSONObject.create(T) will return the passed in object itself if that object is an instance of JSONObject. This was done so that JSONObjects can be generic members of deserialized/serialized data. Discussion will need to happen if this method should deep-copy the JSONObject instead.
1 parent feda395 commit e83ec77

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

docs/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ JSON (C) Black Rook Software 2019
33
by Matt Tropiano et al. (see AUTHORS.txt)
44

55

6+
Changed in 1.1.2
7+
----------------
8+
9+
- `Changed` JSONObject.create(T) will return the passed in object itself if that object is an instance of JSONObject.
10+
This was done so that JSONObjects can be generic members of deserialized/serialized data. Discussion will need to happen if this
11+
method should deep-copy the JSONObject instead.
12+
13+
614
Changed in 1.1.1
715
----------------
816

src/main/java/com/blackrook/json/JSONObject.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ public static <T> JSONObject create(T object)
167167
{
168168
if (object == null)
169169
return NULL;
170+
else if (object instanceof JSONObject) // TODO: maybe this should copy?
171+
return (JSONObject)object;
170172
else if (Utils.isArray(object))
171173
{
172174
int len = Array.getLength(object);
@@ -917,6 +919,11 @@ private static <T> T newClassInstance(String memberName, Class<T> type)
917919
@SuppressWarnings({ "unchecked", "rawtypes" })
918920
private static <T, K, V> T createForType(String memberName, JSONObject jsonObject, Class<T> type, Class<K> keyType, Class<V> valueType)
919921
{
922+
if (JSONObject.class.isAssignableFrom(type))
923+
{
924+
return (T)jsonObject;
925+
}
926+
920927
Type jsonType = jsonObject.getType();
921928

922929
switch (jsonType)

src/test/java/com/blackrook/json/JSONTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public static class Pair
5858
public int y;
5959
@JSONName("name")
6060
public int a;
61-
private int z;
61+
private JSONObject z;
6262

63-
public int getZ()
63+
public JSONObject getZ()
6464
{
6565
return z;
6666
}
6767

68-
public void setZ(int z)
68+
public void setZ(JSONObject z)
6969
{
7070
this.z = z;
7171
}

0 commit comments

Comments
 (0)