From: Kai Moritz Date: Tue, 21 Jun 2016 11:46:12 +0000 (+0200) Subject: WIP X-Git-Tag: alt~5 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=bce221a3de2e3d3cdb28d3c56bad8f6760ae4900;p=maven-thymeleaf-skin WIP --- diff --git a/src/main/java/de/juplo/jackson/SimpleMapper.java b/src/main/java/de/juplo/jackson/SimpleMapper.java index 941c4b1..33133f6 100644 --- a/src/main/java/de/juplo/jackson/SimpleMapper.java +++ b/src/main/java/de/juplo/jackson/SimpleMapper.java @@ -58,9 +58,7 @@ public class SimpleMapper } 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() { @@ -71,7 +69,7 @@ public class SimpleMapper { 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)); @@ -107,97 +105,39 @@ public class SimpleMapper 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 spliterator = getArraySpliterator(parser); return new Iterator() { + private Object next = null; @Override - public boolean tryAdvance(Consumer 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() { - 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 { @@ -277,6 +217,6 @@ public class SimpleMapper location.getLineNr(), location.getColumnNr() ); - throw new RuntimeException("Cannot parse JSON: " + message); + throw new IllegalArgumentException("Cannot parse JSON: " + message); } }