Java Code Examples for org.noggit.JSONParser#getString()

The following examples show how to use org.noggit.JSONParser#getString() . 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: JsonRecordReader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static Object parseSingleFieldValue(int ev, JSONParser parser, MethodFrameWrapper runnable) throws IOException {
  switch (ev) {
    case STRING:
      return parser.getString();
    case LONG:
      return parser.getLong();
    case NUMBER:
      return parser.getDouble();
    case BIGNUMBER:
      return parser.getNumberChars().toString();
    case BOOLEAN:
      return parser.getBoolean();
    case NULL:
      parser.getNull();
      return null;
    case ARRAY_START:
      return parseArrayFieldValue(ev, parser, runnable);
    case OBJECT_START:
      if (runnable != null) {
        runnable.walk(OBJECT_START);
        return null;
      }
      consumeTillMatchingEnd(parser, 1, 0);
      return null;
    default:
      throw new RuntimeException("Error parsing JSON field value. Unexpected " + JSONParser.getEventString(ev));
  }
}
 
Example 2
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 3
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);
}