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

The following examples show how to use org.noggit.JSONParser#nextEvent() . 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 6 votes vote down vote up
private void parse(JSONParser parser,
                   Handler handler,
                   Map<String, Object> values) throws IOException {

  int event = -1;
  boolean recordStarted = false;
  for (; ; ) {
    event = parser.nextEvent();
    if (event == EOF) break;
    if (event == OBJECT_START) {
      handleObjectStart(parser, handler, new LinkedHashMap<>(), new Stack<>(), recordStarted, null);
    } else if (event == ARRAY_START) {
      for (; ; ) {
        event = parser.nextEvent();
        if (event == ARRAY_END) break;
        if (event == OBJECT_START) {
          handleObjectStart(parser, handler, new LinkedHashMap<>(), new Stack<>(), recordStarted, null);
        }
      }
    }
  }

}
 
Example 2
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 3
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 4
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 5
Source File: MtasSolrComponentCollection.java    From mtas with Apache License 2.0 5 votes vote down vote up
/**
 * String to string values.
 *
 * @param stringValue the string value
 * @return the hash set
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static HashSet<String> stringToStringValues(String stringValue)
    throws IOException {
  // should be improved to support escaped characters
  HashSet<String> stringValues = new HashSet<>();
  JSONParser jsonParser = new JSONParser(stringValue);
  int event = jsonParser.nextEvent();
  if (event == JSONParser.ARRAY_START) {
    while ((event = jsonParser.nextEvent()) != JSONParser.ARRAY_END) {
      if (jsonParser.getLevel() == 1) {
        switch (event) {
        case JSONParser.STRING:
          stringValues.add(jsonParser.getString());
          break;
        case JSONParser.BIGNUMBER:
        case JSONParser.NUMBER:
        case JSONParser.LONG:
          stringValues.add(jsonParser.getNumberChars().toString());
          break;
        case JSONParser.BOOLEAN:
          stringValues.add(Boolean.toString(jsonParser.getBoolean()));
          break;
        default:
          // do nothing
          break;
        }
      }
    }
  } else {
    throw new IOException("unsupported json structure");
  }
  return stringValues;
}
 
Example 6
Source File: JSONUtil.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public static void expect(JSONParser parser, int parserEventType) throws IOException {
  int event = parser.nextEvent();
  if (event != parserEventType) {
    throw new IOException("JSON Parser: expected " + JSONParser.getEventString(parserEventType) + " but got " + JSONParser.getEventString(event) );
  }
}
 
Example 7
Source File: RequestUtil.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static void getParamsFromJSON(Map<String, String[]> params, String json) {
  if (json.indexOf("params") < 0) {
    return;
  }

  JSONParser parser = new JSONParser(json);
  try {
    JSONUtil.expect(parser, JSONParser.OBJECT_START);
    boolean found = JSONUtil.advanceToMapKey(parser, "params", false);
    if (!found) {
      return;
    }

    parser.nextEvent();  // advance to the value

    Object o = ObjectBuilder.getVal(parser);
    if (!(o instanceof Map)) return;
    @SuppressWarnings("unchecked")
    Map<String,Object> map = (Map<String,Object>)o;
    // To make consistent with json.param handling, we should make query params come after json params (i.e. query params should
    // appear to overwrite json params.

    // Solr params are based on String though, so we need to convert
    for (Map.Entry<String, Object> entry : map.entrySet()) {
      String key = entry.getKey();
      Object val = entry.getValue();
      if (params.get(key) != null) {
        continue;
      }

      if (val == null) {
        params.remove(key);
      } else if (val instanceof List) {
        List<?> lst = (List<?>) val;
        String[] vals = new String[lst.size()];
        for (int i = 0; i < vals.length; i++) {
          vals[i] = lst.get(i).toString();
        }
        params.put(key, vals);
      } else {
        params.put(key, new String[]{val.toString()});
      }
    }

  } catch (Exception e) {
    // ignore parse exceptions at this stage, they may be caused by incomplete macro expansions
    return;
  }

}
 
Example 8
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);
}