From bf57b6bc4dd9ac19b748b7e6fd8e4f4ba37c697a Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Tue, 21 Jun 2016 18:29:43 +0200 Subject: [PATCH] WIP --- .../java/de/juplo/jackson/SimpleMapper.java | 134 +++++++++++++++++- 1 file changed, 131 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/juplo/jackson/SimpleMapper.java b/src/main/java/de/juplo/jackson/SimpleMapper.java index 33133f6..db2df37 100644 --- a/src/main/java/de/juplo/jackson/SimpleMapper.java +++ b/src/main/java/de/juplo/jackson/SimpleMapper.java @@ -6,13 +6,12 @@ import com.fasterxml.jackson.core.JsonLocation; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; import java.io.IOException; -import java.io.InputStream; -import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.NoSuchElementException; import java.util.Spliterator; import static java.util.Spliterator.IMMUTABLE; @@ -109,7 +108,8 @@ public class SimpleMapper return new Iterator() { private Object next = null; - + + @Override public boolean hasNext() { @@ -139,6 +139,134 @@ public class SimpleMapper } + public static Stream> getObjectStream(final JsonParser parser) + throws + IOException + { + return StreamSupport.stream(getObjectSpliterator(parser), false); + } + + public static Spliterator> getObjectSpliterator(final JsonParser parser) + throws + IOException + { + JsonToken token = parser.nextToken(); + + if (token == null) + { + LOG.warn("empty input"); + return Spliterators.emptySpliterator(); + } + + if (!JsonToken.START_OBJECT.equals(token)) + fail(parser, "The root-element must be an array!"); + + return new Spliterator>() + { + @Override + public boolean tryAdvance(Consumer> action) + { + try + { + JsonToken token = parser.nextToken(); + if (token == null) + fail(parser, "Unexpected end of data!"); + if (JsonToken.END_OBJECT.equals(token)) + return false; + if (!JsonToken.FIELD_NAME.equals(token)) + fail(parser, "expected a field-name"); + final String key = parser.getText(); + parser.nextToken(); + final Object value = convert(parser); + action.accept(new Entry() + { + @Override + public String getKey() + { + return key; + } + + @Override + public Object getValue() + { + return value; + } + + @Override + public Object setValue(Object value) + { + throw new UnsupportedOperationException("Not supported."); + } + }); + return true; + } + catch (IOException e) + { + throw new IllegalArgumentException(e); + } + } + + @Override + public Spliterator> trySplit() + { + return null; + } + + @Override + public long estimateSize() + { + return Long.MAX_VALUE; + } + + @Override + public int characteristics() + { + return IMMUTABLE; + } + }; + } + + public static Iterator> getObjectIterator( + final JsonParser parser + ) + throws + IOException + { + Spliterator> spliterator = getObjectSpliterator(parser); + return new Iterator>() + { + private Entry next = null; + + + @Override + public boolean hasNext() + { + if (next != null) + return true; + + return spliterator.tryAdvance(new Consumer>() + { + @Override + public void accept(Entry e) + { + next = e; + } + }); + } + + @Override + public Entry next() + { + if (next == null && !hasNext()) + throw new NoSuchElementException(); + Entry e = next; + next = null; + return e; + } + }; + } + + static Object convert(JsonParser parser) throws IOException { JsonToken token = parser.getCurrentToken(); -- 2.20.1