Java Code Examples for org.noggit.JSONParser#OBJECT_START

The following examples show how to use org.noggit.JSONParser#OBJECT_START . 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: JsonLoader.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private Object parseFieldValue(int ev, String fieldName) throws IOException {
  switch (ev) {
    case JSONParser.STRING:
      return parser.getString();
    case JSONParser.LONG:
      return parser.getLong();
    case JSONParser.NUMBER:
      return parser.getDouble();
    case JSONParser.BIGNUMBER:
      return parser.getNumberChars().toString();
    case JSONParser.BOOLEAN:
      return parser.getBoolean();
    case JSONParser.NULL:
      parser.getNull();
      return null;
    case JSONParser.ARRAY_START:
      return parseArrayFieldValue(ev, fieldName);
    case JSONParser.OBJECT_START:
      return parseObjectFieldValue(ev, fieldName);
    default:
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing JSON field value. "
          + "Unexpected " + JSONParser.getEventString(ev) + " at [" + parser.getPosition() + "], field=" + fieldName);
  }
}
 
Example 2
Source File: JSONTupleStream.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private boolean advanceToMapKey(String key, boolean deepSearch) throws IOException {
  for (;;) {
    int event = parser.nextEvent();
    switch (event) {
      case JSONParser.STRING:
        if (key != null) {
          String val = parser.getString();
          if (key.equals(val)) {
            return true;
          } else if("error".equals(val)) {
            handleError();
          }
        }
        break;
      case JSONParser.OBJECT_END:
        return false;
      case JSONParser.OBJECT_START:
        if (deepSearch) {
          boolean found = advanceToMapKey(key, true);
          if (found) {
            return true;
          }
        } else {
          advanceToMapKey(null, false);
        }
        break;
      case JSONParser.ARRAY_START:
        skipArray(key, deepSearch);
        break;
    }
  }
}
 
Example 3
Source File: JSONTupleStream.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void skipArray(String key, boolean deepSearch) throws IOException {
  for (;;) {
    int event = parser.nextEvent();
    switch (event) {
      case JSONParser.OBJECT_START:
        advanceToMapKey(key, deepSearch);
        break;
      case JSONParser.ARRAY_START:
        skipArray(key, deepSearch);
        break;
      case JSONParser.ARRAY_END:
        return;
    }
  }
}
 
Example 4
Source File: CommandOperation.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Parse the command operations into command objects from a json payload
 *
 * @param rdr               The payload
 * @param singletonCommands commands that cannot be repeated
 * @return parsed list of commands
 */
public static List<CommandOperation> parse(Reader rdr, Set<String> singletonCommands) throws IOException {
  JSONParser parser = new JSONParser(rdr);
  parser.setFlags(parser.getFlags() |
      JSONParser.ALLOW_MISSING_COLON_COMMA_BEFORE_OBJECT |
      JSONParser.OPTIONAL_OUTER_BRACES
  );

  ObjectBuilder ob = new ObjectBuilder(parser);

  if (parser.lastEvent() != JSONParser.OBJECT_START) {
    throw new RuntimeException("The JSON must be an Object of the form {\"command\": {...},...");
  }
  List<CommandOperation> operations = new ArrayList<>();
  for (; ; ) {
    int ev = parser.nextEvent();
    if (ev == JSONParser.OBJECT_END) {
      ObjectBuilder.checkEOF(parser);
      return operations;
    }
    Object key = ob.getKey();
    ev = parser.nextEvent();
    Object val = ob.getVal();
    if (val instanceof List && !singletonCommands.contains(key)) {
      @SuppressWarnings({"rawtypes"})
      List list = (List) val;
      for (Object o : list) {
        if (!(o instanceof Map)) {
          operations.add(new CommandOperation(String.valueOf(key), list));
          break;
        } else {
          operations.add(new CommandOperation(String.valueOf(key), o));
        }
      }
    } else {
      operations.add(new CommandOperation(String.valueOf(key), val));
    }
  }

}
 
Example 5
Source File: JSONUtil.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static boolean advanceToMapKey(JSONParser parser, String key, boolean deepSearch) throws IOException {
  for (;;) {
    int event = parser.nextEvent();
    switch (event) {
      case JSONParser.STRING:
        if (key != null && parser.wasKey()) {
          String val = parser.getString();
          if (key.equals(val)) {
            return true;
          }
        }
        break;
      case JSONParser.OBJECT_END:
        return false;
      case JSONParser.OBJECT_START:
        if (deepSearch) {
          boolean found = advanceToMapKey(parser, key, true);
          if (found) {
            return true;
          }
        } else {
          advanceToMapKey(parser, null, false);
        }
        break;
      case JSONParser.ARRAY_START:
        skipArray(parser, key, deepSearch);
        break;
    }
  }
}
 
Example 6
Source File: JSONUtil.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static void skipArray(JSONParser parser, String key, boolean deepSearch) throws IOException {
  for (;;) {
    int event = parser.nextEvent();
    switch (event) {
      case JSONParser.OBJECT_START:
        advanceToMapKey(parser, key, deepSearch);
        break;
      case JSONParser.ARRAY_START:
        skipArray(parser, key, deepSearch);
        break;
      case JSONParser.ARRAY_END:
        return;
    }
  }
}
 
Example 7
Source File: JsonLoader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void handleDeleteCommand() throws IOException {
  int ev = parser.nextEvent();
  switch (ev) {
    case JSONParser.ARRAY_START:
      handleDeleteArray(ev);
      break;
    case JSONParser.OBJECT_START:
      handleDeleteMap(ev);
      break;
    default:
      handleSingleDelete(ev);
  }
}
 
Example 8
Source File: JsonLoader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void handleSingleDelete(int ev) throws IOException {
  if (ev == JSONParser.OBJECT_START) {
    handleDeleteMap(ev);
  } else {
    DeleteUpdateCommand cmd = new DeleteUpdateCommand(req);
    cmd.commitWithin = commitWithin;
    String id = getString(ev);
    cmd.setId(id);
    processor.processDelete(cmd);
  }
}
 
Example 9
Source File: OpenExchangeRatesOrgProvider.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public OpenExchangeRates(InputStream ratesStream) throws IOException {
  parser = new JSONParser(new InputStreamReader(ratesStream, StandardCharsets.UTF_8));
  rates = new HashMap<>();
  
  int ev;
  do {
    ev = parser.nextEvent();
    switch( ev ) {
      case JSONParser.STRING:
        if( parser.wasKey() ) {
          String key = parser.getString();
          if(key.equals("disclaimer")) {
            parser.nextEvent();
            disclaimer = parser.getString();
          } else if(key.equals("license")) {
            parser.nextEvent();
            license = parser.getString();
          } else if(key.equals("timestamp")) {
            parser.nextEvent();
            timestamp = parser.getLong();
          } else if(key.equals("base")) {
            parser.nextEvent();
            baseCurrency = parser.getString();
          } else if(key.equals("rates")) {
            ev = parser.nextEvent();
            assert(ev == JSONParser.OBJECT_START);
            ev = parser.nextEvent();
            while (ev != JSONParser.OBJECT_END) {
              String curr = parser.getString();
              ev = parser.nextEvent();
              Double rate = parser.getDouble();
              rates.put(curr, rate);
              ev = parser.nextEvent();                  
            }
          } else {
            log.warn("Unknown key {}", key);
          }
          break;
        } else {
          log.warn("Expected key, got {}", JSONParser.getEventString(ev));
          break;
        }
         
      case JSONParser.OBJECT_END:
      case JSONParser.OBJECT_START:
      case JSONParser.EOF:
        break;

      default:
        if (log.isInfoEnabled()) {
          log.info("Noggit UNKNOWN_EVENT_ID: {}", JSONParser.getEventString(ev));
        }
        break;
    }
  } while( ev != JSONParser.EOF);
}