Java Code Examples for com.google.gson.stream.JsonToken#END_DOCUMENT

The following examples show how to use com.google.gson.stream.JsonToken#END_DOCUMENT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RealSandboxfs02Process.java    From bazel with Apache License 2.0 6 votes vote down vote up
/** Waits for responses and dispatches them. */
private void processResponses() throws IOException {
  while (!Thread.interrupted() && reader.peek() != JsonToken.END_DOCUMENT) {
    Response response = readResponse(reader);
    if (response.id == null) {
      // Non-recoverable error: abort.
      throw new IOException(response.error != null ? response.error : "No error reported");
    }

    SettableFuture<Void> future = inFlightRequests.remove(response.id);
    if (future == null) {
      throw new IOException("sandboxfs returned response for unknown id " + response.id);
    }
    if (response.error == null) {
      future.set(null);
    } else {
      future.setException(new IOException(response.error));
    }
  }
}
 
Example 2
Source File: JsonIngestFlattener.java    From datawave with Apache License 2.0 6 votes vote down vote up
private void run() throws IOException {
    
    JsonReader reader = new JsonReader(new FileReader(jsonInputFile));
    reader.setLenient(true);
    setupIterator(reader);
    
    try {
        while (true) {
            
            if (!jsonIterator.hasNext()) {
                
                if (reader.peek() == JsonToken.END_DOCUMENT) {
                    // If we're here, then we're done. No more objects left in the file
                    return;
                }
                setupIterator(reader);
            }
            
            if (jsonIterator.hasNext()) {
                flattenAndPrint(jsonIterator.next().getAsJsonObject());
            }
        }
    } finally {
        reader.close();
    }
}
 
Example 3
Source File: JsonParser.java    From letv with Apache License 2.0 6 votes vote down vote up
public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException {
    try {
        JsonReader jsonReader = new JsonReader(json);
        JsonElement element = parse(jsonReader);
        if (element.isJsonNull() || jsonReader.peek() == JsonToken.END_DOCUMENT) {
            return element;
        }
        throw new JsonSyntaxException("Did not consume the entire document.");
    } catch (Throwable e) {
        throw new JsonSyntaxException(e);
    } catch (Throwable e2) {
        throw new JsonIOException(e2);
    } catch (Throwable e22) {
        throw new JsonSyntaxException(e22);
    }
}
 
Example 4
Source File: ViccJsonReader.java    From hmftools with GNU General Public License v3.0 6 votes vote down vote up
@NotNull
public static List<ViccEntry> readSelection(@NotNull String jsonPath, @NotNull ViccQuerySelection querySelection) throws IOException {
    List<ViccEntry> entries = Lists.newArrayList();

    JsonParser parser = new JsonParser();
    JsonReader reader = new JsonReader(new FileReader(jsonPath));
    reader.setLenient(true);

    while (reader.peek() != JsonToken.END_DOCUMENT && (querySelection.maxEntriesToInclude() == null
            || entries.size() < querySelection.maxEntriesToInclude())) {
        JsonObject viccEntryObject = parser.parse(reader).getAsJsonObject();
        ViccSource source = ViccSource.fromViccKnowledgebaseString(string(viccEntryObject, "source"));
        if (querySelection.sourcesToFilterOn() == null || querySelection.sourcesToFilterOn().contains(source)) {
            entries.add(createViccEntry(viccEntryObject));
        }
    }

    reader.close();

    return entries;
}
 
Example 5
Source File: JsonParser.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public JsonElement parse(Reader reader)
{
    JsonElement jsonelement;
    try
    {
        JsonReader jsonreader = new JsonReader(reader);
        jsonelement = parse(jsonreader);
        if (!jsonelement.isJsonNull() && jsonreader.peek() != JsonToken.END_DOCUMENT)
        {
            throw new JsonSyntaxException("Did not consume the entire document.");
        }
    }
    catch (MalformedJsonException malformedjsonexception)
    {
        throw new JsonSyntaxException(malformedjsonexception);
    }
    catch (IOException ioexception)
    {
        throw new JsonIOException(ioexception);
    }
    catch (NumberFormatException numberformatexception)
    {
        throw new JsonSyntaxException(numberformatexception);
    }
    return jsonelement;
}
 
Example 6
Source File: BsonReader.java    From immutables with Apache License 2.0 5 votes vote down vote up
private static JsonToken toGsonToken(BsonType type) {
  switch (type) {
  case END_OF_DOCUMENT:
    return JsonToken.END_DOCUMENT;
  case DOCUMENT:
    return JsonToken.BEGIN_OBJECT;
  case ARRAY:
    return JsonToken.BEGIN_ARRAY;
  case BOOLEAN:
    return JsonToken.BOOLEAN;
  case STRING:
  case SYMBOL:
  case OBJECT_ID:
  case BINARY:
  case REGULAR_EXPRESSION:
    return JsonToken.STRING;
  case DATE_TIME:
  case DOUBLE:
  case INT32:
  case INT64:
  case TIMESTAMP:
  case DECIMAL128:
    return JsonToken.NUMBER;
  case NULL:
  case UNDEFINED:
    return JsonToken.NULL;
  default:
    // not really sure what to do with this type
    return JsonToken.NULL;
  }
}
 
Example 7
Source File: JsonParserX.java    From goclipse with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isEndOfInput(JsonReader jsonReader) throws IOException {
	try {
		return jsonReader.peek() == JsonToken.END_DOCUMENT;
	} catch(IOException e) {
		// Why the hell GSON doesn't have a proper way to check for empty document?
		// Need to review on future versions
		if(e.getMessage().startsWith("End of input")) {
			return true;
		}
		throw e;
	}
}
 
Example 8
Source File: JsonUtils.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
public static boolean endJson(final JsonReader reader, final int openJsonObjects) throws IOException,
    EntityProviderException {

  for (int closedJsonObjects = 0; closedJsonObjects < openJsonObjects; closedJsonObjects++) {
    reader.endObject();
  }
  return reader.peek() == JsonToken.END_DOCUMENT;
}
 
Example 9
Source File: GsonConverterFactory.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
@Override
public T convert(ResponseBody value) throws IOException {
    try {
        final JsonReader reader = mGson.newJsonReader(value.charStream());
        T result = mGson.fromJson(reader, mType);
        if (reader.peek() != JsonToken.END_DOCUMENT) {
            throw new JsonIOException("JSON document was not fully consumed.");
        }
        return result;
    } finally {
        value.close();
    }
}
 
Example 10
Source File: JsonUtils.java    From cloud-odata-java with Apache License 2.0 5 votes vote down vote up
public static boolean endJson(final JsonReader reader, final int openJsonObjects) throws IOException, EntityProviderException {

    for (int closedJsonObjects = 0; closedJsonObjects < openJsonObjects; closedJsonObjects++) {
      reader.endObject();
    }
    return reader.peek() == JsonToken.END_DOCUMENT;
  }
 
Example 11
Source File: EntityStreamReaderUtil.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
public static void processJsonStream(JsonEntityResponseHandler handler, InputStream stream) throws IOException {
    handler.start();
    try(JsonReader json = new JsonReader(new InputStreamReader(stream))) {
        JsonParser parser = new JsonParser();

        json.setLenient(true);
        while (json.peek() != JsonToken.END_DOCUMENT) {
            JsonElement element = parser.parse(json);
            handler.process(element.getAsJsonObject());
        }
    } finally {
        handler.stop();
    }
}
 
Example 12
Source File: JsonStreamParser.java    From letv with Apache License 2.0 5 votes vote down vote up
public boolean hasNext() {
    boolean z;
    synchronized (this.lock) {
        try {
            z = this.parser.peek() != JsonToken.END_DOCUMENT;
        } catch (Throwable e) {
            throw new JsonSyntaxException(e);
        } catch (Throwable e2) {
            throw new JsonIOException(e2);
        }
    }
    return z;
}
 
Example 13
Source File: Gson.java    From letv with Apache License 2.0 5 votes vote down vote up
private static void assertFullConsumption(Object obj, JsonReader reader) {
    if (obj != null) {
        try {
            if (reader.peek() != JsonToken.END_DOCUMENT) {
                throw new JsonIOException("JSON document was not fully consumed.");
            }
        } catch (Throwable e) {
            throw new JsonSyntaxException(e);
        } catch (Throwable e2) {
            throw new JsonIOException(e2);
        }
    }
}
 
Example 14
Source File: JsonRecordReader.java    From datawave with Apache License 2.0 5 votes vote down vote up
@Override
public boolean nextKeyValue() throws IOException {
    
    event.clear();
    currentKey.set(pos);
    currentValue.clear();
    counter++;
    
    if (!jsonIterator.hasNext()) {
        /*
         * Note that for streaming purposes we support files containing multiple distinct json objects concatenated together, where each object will
         * represent a distinct event/document in our shard table. For example, the file might look like the following...
         * 
         * { "doc1": ... }{ "doc2": ... }...{ "docN": ... }
         * 
         * As a whole, this would represent an invalid json document, but it is useful for streaming large numbers of objects in batch. Therefore, we simply
         * check here to see if the reader has more objects to read, and if so we keep going
         */
        if (reader.peek() == JsonToken.END_DOCUMENT) {
            return false;
        }
        setupIterator(reader);
    }
    
    if (jsonIterator.hasNext()) {
        JsonElement jsonElement = jsonIterator.next();
        
        parseCurrentValue(jsonElement.getAsJsonObject());
        pos = countingInputStream.getCount();
        
        // Save ref to the current json element, to be used when writing the raw data to the record in getEvent
        currentJsonObj = jsonElement;
        return true;
    }
    
    return false;
}
 
Example 15
Source File: IpcdJsonReader.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the type of the next token without consuming it.
 */
public JsonToken peek() throws IOException {
   int p = peeked;
   if (p == PEEKED_NONE) {
      p = doPeek();
   }

   switch (p) {
      case PEEKED_BEGIN_OBJECT:
         return JsonToken.BEGIN_OBJECT;
      case PEEKED_END_OBJECT:
         return JsonToken.END_OBJECT;
      case PEEKED_BEGIN_ARRAY:
         return JsonToken.BEGIN_ARRAY;
      case PEEKED_END_ARRAY:
         return JsonToken.END_ARRAY;
      case PEEKED_SINGLE_QUOTED_NAME:
      case PEEKED_DOUBLE_QUOTED_NAME:
      case PEEKED_UNQUOTED_NAME:
         return JsonToken.NAME;
      case PEEKED_TRUE:
      case PEEKED_FALSE:
         return JsonToken.BOOLEAN;
      case PEEKED_NULL:
         return JsonToken.NULL;
      case PEEKED_SINGLE_QUOTED:
      case PEEKED_DOUBLE_QUOTED:
      case PEEKED_UNQUOTED:
      case PEEKED_BUFFERED:
         return JsonToken.STRING;
      case PEEKED_LONG:
      case PEEKED_NUMBER:
         return JsonToken.NUMBER;
      case PEEKED_EOF:
         return JsonToken.END_DOCUMENT;
      default:
         throw new AssertionError();
   }
}
 
Example 16
Source File: SchemaParser.java    From qemu-java with GNU General Public License v2.0 4 votes vote down vote up
@Nonnull
public SchemaModel parse() throws IOException {
    SchemaModel model = new SchemaModel();

    Queue<URL> files = new LinkedList<URL>();
    files.add(this.file);

    while (!files.isEmpty()) {
        State state = new State(files.remove());

        for (;;) {
            // state.schemaReader.clearDocs();
            JsonToken token = state.jsonReader.peek();
            // LOG.info("Token is " + token);
            if (token == JsonToken.END_DOCUMENT)
                break;

            JsonObject jsonTree = parser.parse(state.jsonReader).getAsJsonObject();
            // LOG.info("Read generic " + jsonTree);

            // if (jsonTree.get("gen") != null) continue;

            Class<?> type;
            if (jsonTree.get("command") != null)
                type = QApiCommandDescriptor.class;
            else if (jsonTree.get("struct") != null)
                type = QApiStructDescriptor.class;
            else if (jsonTree.get("type") != null) {
                // 'struct' replaces 'type' after QEmu commit 895a2a8.
                jsonTree.add("struct", jsonTree.remove("type"));
                type = QApiTypeDescriptor.class;
            } else if (jsonTree.get("enum") != null)
                type = QApiEnumDescriptor.class;
            else if (jsonTree.get("union") != null)
                type = QApiUnionDescriptor.class;
            else if (jsonTree.get("alternate") != null)
                type = QApiAnonymousUnionDescriptor.class;
            else if (jsonTree.get("event") != null)
                type = QApiEventDescriptor.class;
            else if (jsonTree.get("include") != null)
                type = QApiIncludeDescriptor.class;
            else if (jsonTree.get("pragma") != null)
                continue; // ignore pragma sections
            else
                throw new IllegalArgumentException("Unknown JsonObject " + jsonTree);
            // LOG.info("Tree is " + jsonTree + "; docs are " + state.schemaReader.getDocs());

            TypeAdapter<?> adapter = gson.getAdapter(type);
            Object object;
            try {
                object = adapter.fromJsonTree(jsonTree);
            } catch (JsonSyntaxException e) {
                throw new JsonSyntaxException("Failed to parse " + jsonTree, e);
            }
            if (object instanceof QApiIncludeDescriptor) {
                files.add(new URL(state.file, ((QApiIncludeDescriptor) object).include));
                continue;
            }

            QApiElementDescriptor element = (QApiElementDescriptor) object;
            // element.docs = state.schemaReader.getDocs();
            element.setModel(model);
            model.elements.put(element.getName(), element);

            if ((element instanceof AbstractQApiTypeDescriptor) && (((AbstractQApiTypeDescriptor) element).base instanceof Map)) {
                QApiTypeDescriptor parentElement = QApiTypeDescriptor.fromMap(
                        (Map) ((AbstractQApiTypeDescriptor) element).base,
                        element.getName() + "Base");
                model.elements.put(parentElement.getName(), parentElement);
            }
            // LOG.info("Read specific " + element);
        }

        state.jsonReader.close();
    }

    return model;
}
 
Example 17
Source File: JsonTreeReader.java    From MiBandDecompiled with Apache License 2.0 4 votes vote down vote up
public JsonToken peek()
{
    if (c.isEmpty())
    {
        return JsonToken.END_DOCUMENT;
    }
    Object obj = a();
    if (obj instanceof Iterator)
    {
        boolean flag = c.get(-2 + c.size()) instanceof JsonObject;
        Iterator iterator = (Iterator)obj;
        if (iterator.hasNext())
        {
            if (flag)
            {
                return JsonToken.NAME;
            } else
            {
                c.add(iterator.next());
                return peek();
            }
        }
        if (flag)
        {
            return JsonToken.END_OBJECT;
        } else
        {
            return JsonToken.END_ARRAY;
        }
    }
    if (obj instanceof JsonObject)
    {
        return JsonToken.BEGIN_OBJECT;
    }
    if (obj instanceof JsonArray)
    {
        return JsonToken.BEGIN_ARRAY;
    }
    if (obj instanceof JsonPrimitive)
    {
        JsonPrimitive jsonprimitive = (JsonPrimitive)obj;
        if (jsonprimitive.isString())
        {
            return JsonToken.STRING;
        }
        if (jsonprimitive.isBoolean())
        {
            return JsonToken.BOOLEAN;
        }
        if (jsonprimitive.isNumber())
        {
            return JsonToken.NUMBER;
        } else
        {
            throw new AssertionError();
        }
    }
    if (obj instanceof JsonNull)
    {
        return JsonToken.NULL;
    }
    if (obj == b)
    {
        throw new IllegalStateException("JsonReader is closed");
    } else
    {
        throw new AssertionError();
    }
}
 
Example 18
Source File: JsonTreeReader.java    From letv with Apache License 2.0 4 votes vote down vote up
public JsonToken peek() throws IOException {
    if (this.stack.isEmpty()) {
        return JsonToken.END_DOCUMENT;
    }
    Iterator<?> o = peekStack();
    if (o instanceof Iterator) {
        boolean isObject = this.stack.get(this.stack.size() - 2) instanceof JsonObject;
        Iterator<?> iterator = o;
        if (!iterator.hasNext()) {
            return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
        } else {
            if (isObject) {
                return JsonToken.NAME;
            }
            this.stack.add(iterator.next());
            return peek();
        }
    } else if (o instanceof JsonObject) {
        return JsonToken.BEGIN_OBJECT;
    } else {
        if (o instanceof JsonArray) {
            return JsonToken.BEGIN_ARRAY;
        }
        if (o instanceof JsonPrimitive) {
            JsonPrimitive primitive = (JsonPrimitive) o;
            if (primitive.isString()) {
                return JsonToken.STRING;
            }
            if (primitive.isBoolean()) {
                return JsonToken.BOOLEAN;
            }
            if (primitive.isNumber()) {
                return JsonToken.NUMBER;
            }
            throw new AssertionError();
        } else if (o instanceof JsonNull) {
            return JsonToken.NULL;
        } else {
            if (o == SENTINEL_CLOSED) {
                throw new IllegalStateException("JsonReader is closed");
            }
            throw new AssertionError();
        }
    }
}
 
Example 19
Source File: JsonTreeReader.java    From gson with Apache License 2.0 4 votes vote down vote up
@Override public JsonToken peek() throws IOException {
  if (stackSize == 0) {
    return JsonToken.END_DOCUMENT;
  }

  Object o = peekStack();
  if (o instanceof Iterator) {
    boolean isObject = stack[stackSize - 2] instanceof JsonObject;
    Iterator<?> iterator = (Iterator<?>) o;
    if (iterator.hasNext()) {
      if (isObject) {
        return JsonToken.NAME;
      } else {
        push(iterator.next());
        return peek();
      }
    } else {
      return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
    }
  } else if (o instanceof JsonObject) {
    return JsonToken.BEGIN_OBJECT;
  } else if (o instanceof JsonArray) {
    return JsonToken.BEGIN_ARRAY;
  } else if (o instanceof JsonPrimitive) {
    JsonPrimitive primitive = (JsonPrimitive) o;
    if (primitive.isString()) {
      return JsonToken.STRING;
    } else if (primitive.isBoolean()) {
      return JsonToken.BOOLEAN;
    } else if (primitive.isNumber()) {
      return JsonToken.NUMBER;
    } else {
      throw new AssertionError();
    }
  } else if (o instanceof JsonNull) {
    return JsonToken.NULL;
  } else if (o == SENTINEL_CLOSED) {
    throw new IllegalStateException("JsonReader is closed");
  } else {
    throw new AssertionError();
  }
}
 
Example 20
Source File: JsonTreeReader.java    From framework with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override public JsonToken peek() throws IOException {
  if (stack.isEmpty()) {
    return JsonToken.END_DOCUMENT;
  }

  Object o = peekStack();
  if (o instanceof Iterator) {
    boolean isObject = stack.get(stack.size() - 2) instanceof JsonObject;
    Iterator<?> iterator = (Iterator<?>) o;
    if (iterator.hasNext()) {
      if (isObject) {
        return JsonToken.NAME;
      } else {
        stack.add(iterator.next());
        return peek();
      }
    } else {
      return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
    }
  } else if (o instanceof JsonObject) {
    return JsonToken.BEGIN_OBJECT;
  } else if (o instanceof JsonArray) {
    return JsonToken.BEGIN_ARRAY;
  } else if (o instanceof JsonPrimitive) {
    JsonPrimitive primitive = (JsonPrimitive) o;
    if (primitive.isString()) {
      return JsonToken.STRING;
    } else if (primitive.isBoolean()) {
      return JsonToken.BOOLEAN;
    } else if (primitive.isNumber()) {
      return JsonToken.NUMBER;
    } else {
      throw new AssertionError();
    }
  } else if (o instanceof JsonNull) {
    return JsonToken.NULL;
  } else if (o == SENTINEL_CLOSED) {
    throw new IllegalStateException("JsonReader is closed");
  } else {
    throw new AssertionError();
  }
}