@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 '")); }
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; }
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; } }
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; }  }
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); }
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);  }