}
if (!JsonToken.START_ARRAY.equals(token))
- {
- throw new IllegalArgumentException("The root-element must be an array!");
- }
+ fail(parser, "The root-element must be an array!");
return new Spliterator<Object>()
{
{
JsonToken token = parser.nextToken();
if (token == null)
- throw new IllegalArgumentException("Unexpected end of data!");
+ fail(parser, "Unexpected end of data!");
if (JsonToken.END_ARRAY.equals(token))
return false;
action.accept(convert(parser));
throws
IOException
{
- JsonToken token = parser.nextToken();
-
- if (token == null)
- {
- LOG.warn("empty input");
- return Collections.emptyIterator();
- }
-
- if (!JsonToken.START_ARRAY.equals(token))
- {
- throw new IllegalArgumentException("The root-element must be an array!");
- }
-
+ Spliterator<Object> spliterator = getArraySpliterator(parser);
return new Iterator<Object>()
{
+ private Object next = null;
@Override
- public boolean tryAdvance(Consumer<? super Object> action)
+ public boolean hasNext()
{
- try
- {
- JsonToken token = parser.nextToken();
- if (token == null)
- throw new IllegalArgumentException("Unexpected end of data!");
- if (JsonToken.END_ARRAY.equals(token))
- throw new NoSuchElementException();
- action.accept(convert(parser));
+ if (next != null)
return true;
- }
- catch (IOException e)
+
+ return spliterator.tryAdvance(new Consumer<Object>()
{
- throw new IllegalArgumentException(e);
- }
- }
- @Override
- public boolean hasNext()
- {
+ @Override
+ public void accept(Object o)
+ {
+ next = o;
+ }
+ });
}
@Override
public Object next()
{
- try
- {
- JsonToken token = parser.nextToken();
- if (token == null)
- throw new IllegalArgumentException("Unexpected end of data!");
- if (JsonToken.END_ARRAY.equals(token))
- return false;
- action.accept(convert(parser));
- return true;
- }
- catch (IOException e)
- {
- throw new IllegalArgumentException(e);
- }
+ if (next == null && !hasNext())
+ throw new NoSuchElementException();
+ Object o = next;
+ next = null;
+ return o;
}
};
}
- public Object processAttribute(InputStream is)
- {
- try
- {
- /**
- * Read the JSON and create the variables
- */
- JsonParser parser = factory.createParser(is);
-
- JsonToken token = parser.nextToken();
-
- if (token == null)
- {
- LOG.warn("empty input-stream");
- return null;
- }
-
- Object result = convert(parser);
-
- if (parser.nextToken() != null)
- fail(parser, "unexpected data after parsed variables");
-
- return result;
- }
- catch (IOException e)
- {
- LOG.error("cannot parse input-stream as JSON: {}", e.getMessage());
- throw new RuntimeException(e);
- }
- }
-
static Object convert(JsonParser parser) throws IOException
{
location.getLineNr(),
location.getColumnNr()
);
- throw new RuntimeException("Cannot parse JSON: " + message);
+ throw new IllegalArgumentException("Cannot parse JSON: " + message);
}
}