WIP
authorKai Moritz <kai@juplo.de>
Tue, 21 Jun 2016 16:29:43 +0000 (18:29 +0200)
committerKai Moritz <kai@juplo.de>
Tue, 21 Jun 2016 16:29:43 +0000 (18:29 +0200)
src/main/java/de/juplo/jackson/SimpleMapper.java

index 33133f6..db2df37 100644 (file)
@@ -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<Object>()
     {
       private Object next = null;
-      
+
+
       @Override
       public boolean hasNext()
       {
@@ -139,6 +139,134 @@ public class SimpleMapper
   }
 
 
+  public static Stream<Entry<String, Object>> getObjectStream(final JsonParser parser)
+      throws
+        IOException
+  {
+    return StreamSupport.stream(getObjectSpliterator(parser), false);
+  }
+
+  public static Spliterator<Entry<String, Object>> 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<Entry<String, Object>>()
+    {
+      @Override
+      public boolean tryAdvance(Consumer<? super Entry<String, Object>> 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<String, Object>()
+          {
+            @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<Entry<String, Object>> trySplit()
+      {
+        return null;
+      }
+
+      @Override
+      public long estimateSize()
+      {
+        return Long.MAX_VALUE;
+      }
+
+      @Override
+      public int characteristics()
+      {
+        return IMMUTABLE;
+      }
+    };
+  }
+
+  public static Iterator<Entry<String, Object>> getObjectIterator(
+      final JsonParser parser
+      )
+      throws
+        IOException
+  {
+    Spliterator<Entry<String, Object>> spliterator = getObjectSpliterator(parser);
+    return new Iterator<Entry<String, Object>>()
+    {
+      private Entry<String, Object> next = null;
+
+
+      @Override
+      public boolean hasNext()
+      {
+        if (next != null)
+          return true;
+
+        return spliterator.tryAdvance(new Consumer<Entry<String, Object>>()
+        {
+          @Override
+          public void accept(Entry<String, Object> e)
+          {
+            next = e;
+          }
+        });
+      }
+
+      @Override
+      public Entry<String, Object> next()
+      {
+        if (next == null && !hasNext())
+          throw new NoSuchElementException();
+        Entry<String, Object> e = next;
+        next = null;
+        return e;
+      }
+    };
+  }
+
+
   static Object convert(JsonParser parser) throws IOException
   {
     JsonToken token = parser.getCurrentToken();