Skip to content

Commit db3c717

Browse files
committed
Simplifying array processing in json-io
1 parent 93d6bf3 commit db3c717

File tree

11 files changed

+82
-65
lines changed

11 files changed

+82
-65
lines changed

src/main/java/com/cedarsoftware/io/JsonObject.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public boolean isArray() {
7979
// Return the array that this JSON object wraps. This is used when there is a Collection class (like ArrayList)
8080
// represented in the JSON. This also occurs if a specified array type is used (not Object[], but Integer[], for
8181
// example).
82-
public Object[] getJsonArray() {
83-
return (Object[]) get(ITEMS);
82+
public Object getJsonArray() {
83+
return get(ITEMS);
8484
}
8585

8686
public void setJsonArray(Object[] jsonArray) {
@@ -98,8 +98,8 @@ public int getLength() {
9898
private Integer getLenientSize() {
9999
if (isArray()) {
100100
if (target == null) {
101-
Object[] items = getJsonArray();
102-
return items == null ? 0 : items.length;
101+
Object items = getJsonArray();
102+
return items == null ? 0 : Array.getLength(items);
103103
}
104104
if (char[].class.isAssignableFrom(target.getClass())) {
105105
// Verify this for Character[]
@@ -108,8 +108,8 @@ private Integer getLenientSize() {
108108
return Array.getLength(target);
109109
}
110110
if (isCollection() || isMap()) {
111-
Object[] items = getJsonArray();
112-
return items == null ? 0 : items.length;
111+
Object items = getJsonArray();
112+
return items == null ? 0 : Array.getLength(items);
113113
}
114114
return null;
115115
}
@@ -128,10 +128,7 @@ public boolean hasValue() {
128128

129129
public int size() {
130130
if (containsKey(ITEMS)) {
131-
if (getJsonArray() == null) {
132-
return 0;
133-
}
134-
return getJsonArray().length;
131+
return getLength();
135132
}
136133

137134
return jsonStore.size();

src/main/java/com/cedarsoftware/io/JsonParser.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,11 @@ private Object readArray(Class<?> suggestedType) throws IOException {
298298
}
299299

300300
--curParseDepth;
301-
301+
302+
// JsonObject jsonArray = new JsonObject();
303+
// jsonArray.setJsonArray(list.toArray());
304+
// jsonArray.setTarget(Array.newInstance(suggestedType == null ? Object.class : suggestedType, list.size()));
305+
// return jsonArray;
302306
if (suggestedType == null || suggestedType == Object.class) {
303307
// No suggested type, so use Object[]
304308
return list.toArray();

src/main/java/com/cedarsoftware/io/JsonReader.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,6 @@ public <T> T readObject(Class<T> rootType) {
217217
throw new JsonIoException(getErrorMessage("error parsing JSON value"), e);
218218
}
219219

220-
// JSON {} at root
221-
if (returnValue instanceof JsonObject) {
222-
return determineReturnValueWhenJsonObjectRoot(rootType, returnValue);
223-
}
224-
225220
// JSON [] at root
226221
if (returnValue instanceof Object[]) {
227222
JsonObject rootObj = new JsonObject();
@@ -232,6 +227,11 @@ public <T> T readObject(Class<T> rootType) {
232227
return asMaps ? returnValue : graph;
233228
}
234229

230+
// JSON {} at root
231+
if (returnValue instanceof JsonObject) {
232+
return determineReturnValueWhenJsonObjectRoot(rootType, returnValue);
233+
}
234+
235235
// JSON Primitive (String, Boolean, Double, Long), or convertible types
236236
if (rootType != null) {
237237
Converter converter = resolver.getConverter();

src/main/java/com/cedarsoftware/io/JsonWriter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -974,11 +974,11 @@ private void writeJsonObjectArray(JsonObject jObj, boolean showType) throws IOEx
974974
}
975975
tabIn();
976976

977-
Object[] items = jObj.getJsonArray();
977+
Object items = jObj.getJsonArray();
978978
final int lenMinus1 = len - 1;
979979

980980
for (int i = 0; i < len; i++) {
981-
final Object value = items[i];
981+
final Object value = Array.get(items, i);
982982

983983
if (value == null) {
984984
output.write("null");
@@ -1053,13 +1053,13 @@ private void writeJsonObjectCollection(JsonObject jObj, boolean showType) throws
10531053

10541054
beginCollection(showType, referenced);
10551055

1056-
Object[] items = jObj.getJsonArray();
1057-
final int itemsLen = items.length;
1056+
Object items = jObj.getJsonArray();
1057+
final int itemsLen = Array.getLength(items);
10581058
final int itemsLenMinus1 = itemsLen - 1;
10591059

10601060
for (int i=0; i < itemsLen; i++)
10611061
{
1062-
writeCollectionElement(items[i]);
1062+
writeCollectionElement(Array.get(items, i));
10631063

10641064
if (i != itemsLenMinus1)
10651065
{

src/main/java/com/cedarsoftware/io/MapResolver.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cedarsoftware.io;
22

3+
import java.lang.reflect.Array;
34
import java.math.BigDecimal;
45
import java.math.BigInteger;
56
import java.util.Date;
@@ -137,16 +138,16 @@ public void traverseFields(final JsonObject jsonObj)
137138
*/
138139
protected void traverseCollection(final JsonObject jsonObj)
139140
{
140-
final Object[] items = jsonObj.getJsonArray();
141-
if (items == null || items.length == 0) {
141+
final Object items = jsonObj.getJsonArray();
142+
if (items == null || Array.getLength(items) == 0) {
142143
return;
143144
}
144145

145146
Converter converter = getConverter();
146-
int len = items.length;
147+
int len = Array.getLength(items);
147148

148149
for (int i=0; i < len; i++) {
149-
Object element = items[i];
150+
Object element = Array.get(items, i);
150151

151152
if (element instanceof Object[]) { // array element inside Collection
152153
JsonObject jsonObject = new JsonObject();
@@ -163,7 +164,7 @@ protected void traverseCollection(final JsonObject jsonObj)
163164
// will be placed on the value-side of the Map
164165
Class<?> type = jsonObject.getJavaType();
165166
if (type != null && converter.isConversionSupportedFor(Map.class, type)) {
166-
items[i] = converter.convert(jsonObject, type);
167+
Array.set(items, i, converter.convert(jsonObject, type));
167168
jsonObject.setFinished();
168169
} else {
169170
push(jsonObject);
@@ -174,9 +175,9 @@ protected void traverseCollection(final JsonObject jsonObj)
174175

175176
if (type != null && converter.isConversionSupportedFor(Map.class, type)) {
176177
refObject.setFinishedTarget(converter.convert(refObject, type), true);
177-
items[i] = refObject.getTarget();
178+
Array.set(items, i, refObject.getTarget());
178179
} else {
179-
items[i] = refObject;
180+
Array.set(items, i, refObject);
180181
}
181182
}
182183
}

src/main/java/com/cedarsoftware/io/ObjectResolver.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ private static String safeToString(Object o)
273273
*/
274274
protected void traverseCollection(final JsonObject jsonObj)
275275
{
276-
Object[] items = jsonObj.getJsonArray();
276+
Object items = jsonObj.getJsonArray();
277277

278278
Class mayEnumClass = null;
279279
String mayEnumClasName = (String)jsonObj.get("@enum");
@@ -286,7 +286,9 @@ protected void traverseCollection(final JsonObject jsonObj)
286286
int idx = 0;
287287

288288
if (items != null) {
289-
for (final Object element : items) {
289+
int len = Array.getLength(items);
290+
for (int i=0; i < len; i++) {
291+
Object element = Array.get(items, i);
290292
Object special;
291293
if (element == null) {
292294
col.add(null);
@@ -353,11 +355,11 @@ protected void traverseArray(final JsonObject jsonObj)
353355

354356
final Object array = jsonObj.getTarget();
355357
final Class compType = array.getClass().getComponentType();
356-
final Object[] jsonItems = jsonObj.getJsonArray();
358+
final Object jsonItems = jsonObj.getJsonArray();
357359
// Primitive arrays never make it here, as the ArrayFactory (ClassFactory) processes them in assignField.
358360

359361
for (int i = 0; i < len; i++) {
360-
final Object element = jsonItems[i];
362+
final Object element = Array.get(jsonItems, i);
361363
Object special;
362364

363365
if (element == null) {
@@ -575,9 +577,11 @@ private void markUntypedObjects(final Type type, final Object rhs, final Class<?
575577
}
576578
} else if (instance instanceof JsonObject) {
577579
final JsonObject jObj = (JsonObject) instance;
578-
final Object[] array = jObj.getJsonArray();
580+
final Object array = jObj.getJsonArray();
579581
if (array != null) {
580-
for (Object o : array) {
582+
int len = Array.getLength(array);
583+
for (int i=0; i < len; i++) {
584+
Object o = Array.get(array, i);
581585
stack2.addFirst(new Object[]{typeArgs[0], o});
582586
}
583587
}

src/main/java/com/cedarsoftware/io/Resolver.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,9 @@ Object createInstance(JsonObject jsonObj) {
409409
}
410410

411411
// Arrays
412-
Object[] items = jsonObj.getJsonArray();
412+
Object items = jsonObj.getJsonArray();
413413
if (c.isArray() || (items != null && c == Object.class && !jsonObj.containsKey(KEYS))) { // Handle []
414-
int size = (items == null) ? 0 : items.length;
414+
int size = (items == null) ? 0 : Array.getLength(items);
415415
mate = Array.newInstance(c.isArray() ? c.getComponentType() : Object.class, size);
416416
jsonObj.setTarget(mate);
417417
return mate;
@@ -498,8 +498,8 @@ private EnumSet<?> extractEnumSet(JsonObject jsonObj) {
498498
? evaluateEnumSetTypeFromItems(jsonObj)
499499
: ClassUtilities.forName(enumClassName, readOptions.getClassLoader());
500500

501-
Object[] items = jsonObj.getJsonArray();
502-
if (items == null || items.length == 0) {
501+
Object items = jsonObj.getJsonArray();
502+
if (items == null || Array.getLength(items) == 0) {
503503
if (enumClass != null) {
504504
return EnumSet.noneOf(enumClass);
505505
} else {
@@ -510,7 +510,9 @@ private EnumSet<?> extractEnumSet(JsonObject jsonObj) {
510510
}
511511

512512
EnumSet enumSet = null;
513-
for (Object item : items) {
513+
int len = Array.getLength(items);
514+
for (int i=0; i < len; i++) {
515+
Object item = Array.get(items, i);
514516
Enum enumItem;
515517
if (item instanceof String) {
516518
enumItem = Enum.valueOf(enumClass, (String) item);
@@ -541,10 +543,11 @@ private EnumSet<?> extractEnumSet(JsonObject jsonObj) {
541543
*}</pre>
542544
*/
543545
private Class<?> evaluateEnumSetTypeFromItems(final JsonObject json) {
544-
final Object[] items = json.getJsonArray();
545-
if (items != null && items.length != 0) {
546-
if (items[0] instanceof JsonObject) {
547-
return ((JsonObject) items[0]).getJavaType();
546+
final Object items = json.getJsonArray();
547+
if (items != null && Array.getLength(items) != 0) {
548+
Object value = Array.get(items, 0);
549+
if (value instanceof JsonObject) {
550+
return ((JsonObject) value).getJavaType();
548551
}
549552
}
550553

@@ -617,23 +620,25 @@ public boolean valueToTarget(JsonObject jsonObject) {
617620
// TODO: Support multiple dimensions
618621
// TODO: Support char
619622
if (jsonObject.javaType.isArray() && isConvertable(jsonObject.javaType.getComponentType())) {
620-
Object[] jsonItems = jsonObject.getJsonArray();
623+
Object jsonItems = jsonObject.getJsonArray();
621624
Class<?> componentType = jsonObject.javaType.getComponentType();
622625
if (jsonItems == null) { // empty array
623626
jsonObject.setFinishedTarget(null, true);
624627
return true;
625628
}
626-
Object javaArray = Array.newInstance(componentType, jsonItems.length);
627-
for (int i = 0; i < jsonItems.length; i++) {
629+
int len = Array.getLength(jsonItems);
630+
Object javaArray = Array.newInstance(componentType, len);
631+
for (int i = 0; i < len; i++) {
628632
try {
629633
Class<?> type = componentType;
630-
if (jsonItems[i] instanceof JsonObject) {
631-
JsonObject jObj = (JsonObject) jsonItems[i];
634+
Object item = Array.get(jsonItems, i);
635+
if (item instanceof JsonObject) {
636+
JsonObject jObj = (JsonObject) item;
632637
if (jObj.getJavaType() != null) {
633638
type = jObj.getJavaType();
634639
}
635640
}
636-
Array.set(javaArray, i, converter.convert(jsonItems[i], type));
641+
Array.set(javaArray, i, converter.convert(item, type));
637642
} catch (Exception e) {
638643
JsonIoException jioe = new JsonIoException(e.getMessage());
639644
jioe.setStackTrace(e.getStackTrace());

src/main/java/com/cedarsoftware/io/factory/ArrayFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ public ArrayFactory(Class<T> c) {
3333
}
3434

3535
public T newInstance(Class<?> c, JsonObject jObj, Resolver resolver) {
36-
Object[] items = jObj.getJsonArray();
36+
Object items = jObj.getJsonArray();
3737
Converter converter = resolver.getConverter();
3838
if (items == null) {
3939
jObj.setTarget(null);
4040
return null;
4141
}
42-
int len = items.length;
42+
int len = Array.getLength(items);
4343
Class<?> arrayType = getType();
4444
Class<?> componentType = arrayType.getComponentType();
4545
Object array = Array.newInstance(componentType, len);
4646

4747
for (int i = 0; i < len; i++) {
48-
Object val = items[i];
48+
Object val = Array.get(items, i);
4949
if (val == null) {
5050
} else if (val instanceof JsonObject) {
5151
Class<?> type;

src/main/java/com/cedarsoftware/io/factory/CharacterPrimArrayFactory.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.cedarsoftware.io.factory;
22

3+
import java.lang.reflect.Array;
4+
35
import com.cedarsoftware.io.JsonIoException;
46
import com.cedarsoftware.io.JsonObject;
57
import com.cedarsoftware.io.Resolver;
@@ -28,18 +30,21 @@ public CharacterPrimArrayFactory() {
2830
}
2931

3032
public char[] newInstance(Class<?> c, JsonObject jObj, Resolver resolver) {
31-
Object[] items = jObj.getJsonArray();
33+
Object items = jObj.getJsonArray();
3234
Object value;
33-
35+
3436
if (items == null) {
3537
value = null;
36-
} else if (items.length == 0) {
37-
value = new char[0];
38-
} else if (items.length == 1) {
39-
String s = (String) items[0];
40-
value = s.toCharArray();
4138
} else {
42-
throw new JsonIoException("char[] should only have one String in the [], found " + items.length + ", line " + jObj.getLine() + ", col " + jObj.getCol());
39+
int len = Array.getLength(items);
40+
if (len == 0) {
41+
value = new char[0];
42+
} else if (len == 1) {
43+
String s = (String) Array.get(items, 0);
44+
value = s.toCharArray();
45+
} else {
46+
throw new JsonIoException("char[] should only have one String in the [], found " + len + ", line " + jObj.getLine() + ", col " + jObj.getCol());
47+
}
4348
}
4449
jObj.setTarget(value);
4550
return (char[]) jObj.getTarget();

src/test/java/com/cedarsoftware/io/ArrayTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ void testReconstituteEmptyArray()
642642
JsonObject e1 = (JsonObject) list[0];
643643
JsonObject e2 = (JsonObject) list[1];
644644
assertEquals(e1.getJsonArray(), e2.getJsonArray());
645-
assertEquals(0, e1.getJsonArray().length);
645+
assertEquals(0, e1.getLength());
646646

647647
json1 = TestUtil.toJson(list);
648648
TestUtil.printLine("json1=" + json1);

0 commit comments

Comments
 (0)