Java Code Examples for org.noggit.JSONParser#STRING

The following examples show how to use org.noggit.JSONParser#STRING . 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: JSONTupleStream.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void handleError() throws IOException {
  for (;;) {
    int event = parser.nextEvent();
    if(event == JSONParser.STRING) {
      String val = parser.getString();
      if("msg".equals(val)) {
        event = parser.nextEvent();
        if(event == JSONParser.STRING) {
          String msg = parser.getString();
          if(msg != null) {
            throw new SolrStream.HandledException(msg);
          }
        }
      }
    } else if (event == JSONParser.OBJECT_END) {
      throw new IOException("");
    }
  }
}
 
Example 2
Source File: JsonLoader.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
String getString(int ev) throws IOException {
  switch (ev) {
    case JSONParser.STRING:
      return parser.getString();
    case JSONParser.BIGNUMBER:
    case JSONParser.NUMBER:
    case JSONParser.LONG:
      return parser.getNumberChars().toString();
    case JSONParser.BOOLEAN:
      return Boolean.toString(parser.getBoolean());
    case JSONParser.NULL:
      return null;
    default:
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
          "Expected primitive JSON value but got: " + JSONParser.getEventString(ev)
              + " at [" + parser.getPosition() + "]");
  }
}
 
Example 3
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 4
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 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: 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 7
Source File: JsonLoader.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
AddUpdateCommand parseAdd() throws IOException {
  AddUpdateCommand cmd = new AddUpdateCommand(req);
  cmd.commitWithin = commitWithin;
  cmd.overwrite = overwrite;

  while (true) {
    int ev = parser.nextEvent();
    if (ev == JSONParser.STRING) {
      if (parser.wasKey()) {
        String key = parser.getString();
        if ("doc".equals(key)) {
          if (cmd.solrDoc != null) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Multiple documents in same"
                + " add command at [" + parser.getPosition() + "]");
          }
          ev = assertNextEvent(JSONParser.OBJECT_START);
          cmd.solrDoc = parseDoc(ev);
        } else if (UpdateRequestHandler.OVERWRITE.equals(key)) {
          cmd.overwrite = parser.getBoolean(); // reads next boolean
        } else if (UpdateRequestHandler.COMMIT_WITHIN.equals(key)) {
          cmd.commitWithin = (int) parser.getLong();
        } else if ("boost".equals(key)) {
          String boost = parser.getNumberChars().toString();
          String message = "Ignoring document boost: " + boost + " as index-time boosts are not supported anymore";
          if (WARNED_ABOUT_INDEX_TIME_BOOSTS.compareAndSet(false, true)) {
            log.warn(message);
          } else {
            log.debug(message);
          }
        } else {
          throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown key '" + key + "' at [" + parser.getPosition() + "]");
        }
      } else {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
            "Should be a key "
                + " at [" + parser.getPosition() + "]");
      }
    } else if (ev == JSONParser.OBJECT_END) {
      if (cmd.solrDoc == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Missing solr document at [" + parser.getPosition() + "]");
      }
      return cmd;
    } else {
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
          "Got: " + JSONParser.getEventString(ev)
              + " at [" + parser.getPosition() + "]");
    }
  }
}
 
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);
}