Java Code Examples for org.noggit.JSONParser#ARRAY_START

The following examples show how to use org.noggit.JSONParser#ARRAY_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: 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 5
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 6
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 7
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;
}