All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eg.google.gson.JsonStreamParser Maven / Gradle / Ivy

There is a newer version: 2.1.3
Show newest version
package com.eg.google.gson;

import com.eg.google.gson.internal.Streams;
import com.eg.google.gson.stream.JsonReader;
import com.eg.google.gson.stream.JsonToken;
import com.eg.google.gson.stream.MalformedJsonException;

import java.io.EOFException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Iterator;
import java.util.NoSuchElementException;


public final class JsonStreamParser
  implements Iterator
{
  private final JsonReader parser;
  private final Object lock;

  public JsonStreamParser(String json)
  {
 this(new StringReader(json));
  }




  public JsonStreamParser(Reader reader)
  {
     this.parser = new JsonReader(reader);
     this.parser.setLenient(true);
     this.lock = new Object();
  }






  public JsonElement next()
    throws JsonParseException {
      if (!hasNext()) {
          throw new NoSuchElementException();
      }
      try {
          return Streams.parse(this.parser);
      } catch (StackOverflowError e) {
          throw new JsonParseException("Failed parsing JSON source to Json", e);
      } catch (OutOfMemoryError e) {
          throw new JsonParseException("Failed parsing JSON source to Json", e);
      } catch (JsonParseException e) {
          throw ((e.getCause() instanceof EOFException) ? new NoSuchElementException() : e);
      }
  }





  public boolean hasNext() {
      synchronized (this.lock) {
          try {
              return this.parser.peek() != JsonToken.END_DOCUMENT;
          } catch (MalformedJsonException e) {
              throw new JsonSyntaxException(e);
          } catch (IOException e) {
              throw new JsonIOException(e);
          }
      }
  }





  public void remove() {
      throw new UnsupportedOperationException();
  }
}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy