ArraysParser { public static <T> T[] parseArray(Class<T[]> arrayTypeClass, String input) throws IllegalArgumentException { if (input == null) { return null; } @SuppressWarnings("unchecked") Class<T> componentType = (Class<T>) arrayTypeClass.getComponentType(); if (input.equalsIgnoreCase(EMPTY_VALUE)) { @SuppressWarnings("unchecked") T[] result = (T[]) Array.newInstance(componentType, 0); return result; } ArrayList<String> values = new ArrayList<String>(); try { StringReader reader = new StringReader(input + ' '); int r; char c; while ((r = reader.read()) != -1) { c = (char) r; switch (c) { case RIGHT_BOUND_SQUARE_BRACKET: case RIGHT_BOUND_BRACKET: case LEFT_BOUND_BRACKET: case LEFT_BOUND_SQUARE_BRACKET: case ' ': case '\t': case '\r': case '\n': case COMMA: break; case '"': case '\'': values.add(FormattingAndParsingUtils.parseLiteral(reader, c)); break; default: reader.skip(-1); String value = FormattingAndParsingUtils.parseValue(reader); if (value.equals("null")) { value = null; } values.add(value); } } } catch (IOException ex) { throw new RuntimeException("Unexpected expection while parsing array value", ex); } @SuppressWarnings("unchecked") T[] result = (T[]) Array.newInstance(componentType, values.size()); for (int i = 0; i < values.size(); i++) { if (values.get(i) != null) { result[i] = FormattingAndParsingUtils.convertValue(componentType, values.get(i)); } else { result[i] = null; } } return result; }  static T[] parseArray(Class<T[]> arrayTypeClass, String input); static Object parseArrayAsPrimitiveArray(Class<T[]> arrayTypeClass, String input);  }
@Test public void testParseString() { String[] a1 = ArraysParser.parseArray(String[].class, "[-1, ' value', value2]"); Assert.assertEquals(new String[] { "-1", " value", "value2" }, a1); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseIntegerIncorrect() { ArraysParser.parseArray(Integer[].class, "[1, b]"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseIntegerOutOfBounds() { ArraysParser.parseArray(Integer[].class, "[10000000000000000]"); }
@Test public void testParseBigInteger() { BigInteger[] a1 = ArraysParser .parseArray(BigInteger[].class, "[123456789123456789123456789, -123456789123456789123456789]"); Assert.assertEquals(new BigInteger[] { new BigInteger("123456789123456789123456789"), new BigInteger( "-123456789123456789123456789") }, a1); }
@Test public void testParseBigDecimal() { BigDecimal[] a1 = ArraysParser .parseArray(BigDecimal[].class, "[123456789123456789123456789.123456789123456789123456789, -123456789123456789123456789.123456789123456789123456789]"); Assert.assertEquals(new BigDecimal[] { new BigDecimal("123456789123456789123456789.123456789123456789123456789"), new BigDecimal( "-123456789123456789123456789.123456789123456789123456789") }, a1); }
@Test public void testParseNull() { Boolean[] a1 = ArraysParser.parseArray(Boolean[].class, "[false, null, false]"); String[] a2 = ArraysParser.parseArray(String[].class, "[\"null\", null, 'null', value]"); Assert.assertEquals(new Boolean[] { false, null, false }, a1); Assert.assertEquals(new String[] { "null", null, "null", "value" }, a2); }
@Test public void testParseComplexFormats() { Assert.assertEquals(new String[] { "value1", "value2", " value 'b' \" 3 " }, ArraysParser .parseArray(String[].class, "value1,value2, \" value 'b' \\\" 3 \"")); Assert.assertEquals(new String[] { "value1", "value2", " value \"b\" ' 3 " }, ArraysParser .parseArray(String[].class, "value1, value2,' value \"b\" \\' 3 '")); }