ObjectBuilder { public static Object createArray(Class<?> aClass, JsonValue jsonArray) throws JsonException { if (jsonArray == null || jsonArray.isNull()) { return null; } Class<?> componentType = aClass.getComponentType(); Object array = Array.newInstance(componentType, jsonArray.size()); Iterator<JsonValue> values = jsonArray.getElements(); int i = 0; if (componentType.isArray()) { if (isKnownType(componentType)) { while (values.hasNext()) { Array.set(array, i++, createObjectKnownTypes(componentType, values.next())); } } else { while (values.hasNext()) { Array.set(array, i++, createArray(componentType, values.next())); } } } else { if (isKnownType(componentType)) { while (values.hasNext()) { Array.set(array, i++, createObjectKnownTypes(componentType, values.next())); } } else { while (values.hasNext()) { Array.set(array, i++, createObject(componentType, values.next())); } } } return array; } private  ObjectBuilder(); static Object createArray(Class<?> aClass, JsonValue jsonArray); @SuppressWarnings("unchecked") static T createCollection(Class<T> collectionClass, Type genericType, JsonValue jsonArray); @SuppressWarnings("unchecked") static T createObject(Class<T> mapClass, Type genericType, JsonValue jsonObject); @SuppressWarnings({"unchecked"}) static T createObject(Class<T> aClass, JsonValue jsonValue);  }
@Test public void returnsNullWhenCreateArrayFromNull() throws Exception { Object array = ObjectBuilder.createArray(String[].class, null); assertNull(array); }
@Test public void returnsNullWhenCreateArrayFromNullValue() throws Exception { Object array = ObjectBuilder.createArray(String[].class, new NullValue()); assertNull(array); }
@Test public void createsArrayOfStrings() throws Exception { ArrayValue jsonArray = createJsonArray("to", "be", "or", "not", "to", "be"); Object array = ObjectBuilder.createArray(String[].class, jsonArray); assertArrayEquals(new String[]{"to", "be", "or", "not", "to", "be"}, (Object[])array); }
@Test public void createsArrayOfObjects() throws Exception { ArrayValue jsonArray = createJsonArray(createJsonBook(createJunitBook()), createJsonBook(createCSharpBook()) ); Object array = ObjectBuilder.createArray(Book[].class, jsonArray); assertArrayEquals(new Object[]{createJunitBook(), createCSharpBook()}, (Object[])array); }
@Test public void createsMultiDimensionArrayOfStrings() throws Exception { ArrayValue jsonArray = new ArrayValue(); jsonArray.addElement(createJsonArray("to", "be", "or")); jsonArray.addElement(createJsonArray("not", "to", "be")); Object array = ObjectBuilder.createArray(String[][].class, jsonArray); assertArrayEquals(new String[][]{{"to", "be", "or"}, {"not", "to", "be"}}, (Object[][])array); }
@Test public void createsMultiDimensionArrayOfObjects() throws Exception { ArrayValue jsonArray = new ArrayValue(); ArrayValue childArray1 = createJsonArray( createJsonBook(createJunitBook()), createJsonBook(createCSharpBook())); ArrayValue childArray2 = createJsonArray(createJsonBook(createJavaScriptBook())); jsonArray.addElement(childArray1); jsonArray.addElement(childArray2); Object array = ObjectBuilder.createArray(Book[][].class, jsonArray); assertArrayEquals(new Book[][]{{createJunitBook(), createCSharpBook()}, {createJavaScriptBook()}}, (Object[][])array); }