org.noggit.JSONParser Java Examples

The following examples show how to use org.noggit.JSONParser. 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
/** returns the next Tuple or null */
@Override
@SuppressWarnings({"unchecked"})
public Map<String,Object> next() throws IOException {
  if (!atDocs) {
    boolean found = advanceToDocs();
    atDocs = true;
    if (!found) return null;
  }
  // advance past ARRAY_START (in the case that we just advanced to docs, or OBJECT_END left over from the last call.
  int event = parser.nextEvent();
  if (event == JSONParser.ARRAY_END) return null;

  Object o = ObjectBuilder.getVal(parser);
  // right now, getVal will leave the last event read as OBJECT_END

  return (Map<String,Object>)o;
}
 
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: Utils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static Object fromJSON(byte[] utf8, int offset, int length) {
  // convert directly from bytes to chars
  // and parse directly from that instead of going through
  // intermediate strings or readers
  CharArr chars = new CharArr();
  ByteUtils.UTF8toUTF16(utf8, offset, length, chars);
  JSONParser parser = new JSONParser(chars.getArray(), chars.getStart(), chars.length());
  parser.setFlags(parser.getFlags() |
      JSONParser.ALLOW_MISSING_COLON_COMMA_BEFORE_OBJECT |
      JSONParser.OPTIONAL_OUTER_BRACES);
  try {
    return STANDARDOBJBUILDER.apply(parser).getValStrict();
  } catch (IOException e) {
    throw new RuntimeException(e); // should never happen w/o using real IO
  }
}
 
Example #4
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 #5
Source File: SolrCLI.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public Map<String,Object> handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
  HttpEntity entity = response.getEntity();
  if (entity != null) {

    String respBody = EntityUtils.toString(entity);
    Object resp = null;
    try {
      resp = fromJSONString(respBody);
    } catch (JSONParser.ParseException pe) {
      throw new ClientProtocolException("Expected JSON response from server but received: "+respBody+
          "\nTypically, this indicates a problem with the Solr server; check the Solr server logs for more information.");
    }
    if (resp != null && resp instanceof Map) {
      return (Map<String,Object>)resp;
    } else {
      throw new ClientProtocolException("Expected JSON object in response but received "+ resp);
    }
  } else {
    StatusLine statusLine = response.getStatusLine();
    throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
  }
}
 
Example #6
Source File: JsonRecordReader.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked"})
public static List<Object> parseArrayFieldValue(int ev, JSONParser parser, MethodFrameWrapper runnable) throws IOException {
  assert ev == ARRAY_START;

  @SuppressWarnings({"rawtypes"})
  ArrayList lst = new ArrayList(2);
  for (; ; ) {
    ev = parser.nextEvent();
    if (ev == ARRAY_END) {
      if (lst.isEmpty()) return null;
      return lst;
    }
    Object val = parseSingleFieldValue(ev, parser, runnable);
    if (val != null) lst.add(val);
  }
}
 
Example #7
Source File: SmileWriterTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings({"unchecked"})
public void testJSON() throws IOException {
  SolrQueryRequest req = req("wt","json","json.nl","arrarr");
  SolrQueryResponse rsp = new SolrQueryResponse();
  SmileResponseWriter w = new SmileResponseWriter();

  ByteArrayOutputStream buf = new ByteArrayOutputStream();
  @SuppressWarnings({"rawtypes"})
  NamedList nl = new NamedList();
  nl.add("data1", "he\u2028llo\u2029!");       // make sure that 2028 and 2029 are both escaped (they are illegal in javascript)
  nl.add(null, 42);
  rsp.add("nl", nl);

  rsp.add("byte", Byte.valueOf((byte)-3));
  rsp.add("short", Short.valueOf((short)-4));
  String expected = "{\"nl\":[[\"data1\",\"he\\u2028llo\\u2029!\"],[null,42]],byte:-3,short:-4}";
  w.write(buf, req, rsp);
  @SuppressWarnings({"rawtypes"})
  Map m = (Map) decodeSmile(new ByteArrayInputStream(buf.toByteArray()));
  @SuppressWarnings({"rawtypes"})
  Map o2 = (Map) new ObjectBuilder(new JSONParser(new StringReader(expected))).getObject();
  assertEquals(Utils.toJSONString(m), Utils.toJSONString(o2));
  req.close();
}
 
Example #8
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 #9
Source File: TestJsonFacetRefinement.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Use SimpleOrderedMap rather than Map to match responses from shards
 */
public static Object fromJSON(String json) throws IOException {
  JSONParser parser = new JSONParser(json);
  ObjectBuilder ob = new ObjectBuilder(parser) {
    @Override
    @SuppressWarnings({"rawtypes"})
    public Object newObject() throws IOException {
      return new SimpleOrderedMap();
    }

    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public void addKeyVal(Object map, Object key, Object val) throws IOException {
      ((SimpleOrderedMap) map).add(key.toString(), val);
    }
  };

  return ob.getObject();
}
 
Example #10
Source File: TestSolrConfigHandlerConcurrent.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes"})
public static LinkedHashMapWriter getAsMap(String uri, CloudSolrClient cloudClient) throws Exception {
  HttpGet get = new HttpGet(uri) ;
  HttpEntity entity = null;
  try {
    entity = cloudClient.getLbClient().getHttpClient().execute(get).getEntity();
    String response = EntityUtils.toString(entity, StandardCharsets.UTF_8);
    try {
      return (LinkedHashMapWriter) Utils.MAPWRITEROBJBUILDER.apply(new JSONParser(new StringReader(response))).getVal();
    } catch (JSONParser.ParseException e) {
      log.error(response,e);
      throw e;
    }
  } finally {
    EntityUtils.consumeQuietly(entity);
    get.releaseConnection();
  }
}
 
Example #11
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 #12
Source File: TestSolrConfigHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes"})
public static LinkedHashMapWriter getRespMap(String path, RestTestHarness restHarness) throws Exception {
  String response = restHarness.query(path);
  try {
    return (LinkedHashMapWriter) Utils.MAPWRITEROBJBUILDER.apply(Utils.getJSONParser(new StringReader(response))).getVal();
  } catch (JSONParser.ParseException e) {
    log.error(response);
    return new LinkedHashMapWriter();
  }
}
 
Example #13
Source File: CodecComponent.java    From mtas with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the params from JSON.
 *
 * @param params
 *          the params
 * @param json
 *          the json
 * @return the params from JSON
 */
private static void getParamsFromJSON(Map<String, Object> params,
    String json) {
  JSONParser parser = new JSONParser(json);
  try {
    Object o = ObjectBuilder.getVal(parser);
    if (!(o instanceof Map))
      return;
    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 {
        params.put(key, val);
      }
    }

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

}
 
Example #14
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 #15
Source File: JsonLoader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void assertEvent(int ev, int expected) {
  if (ev != expected) {
    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
        "Expected: " + JSONParser.getEventString(expected)
            + " but got " + JSONParser.getEventString(ev)
            + " at [" + parser.getPosition() + "]");
  }
}
 
Example #16
Source File: JsonLoader.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void handleAdds() throws IOException {
  while (true) {
    AddUpdateCommand cmd = new AddUpdateCommand(req);
    cmd.commitWithin = commitWithin;
    cmd.overwrite = overwrite;

    int ev = parser.nextEvent();
    if (ev == JSONParser.ARRAY_END) break;

    assertEvent(ev, JSONParser.OBJECT_START);
    cmd.solrDoc = parseDoc(ev);
    processor.processAdd(cmd);
  }
}
 
Example #17
Source File: MtasRequestHandler.java    From mtas with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the params from JSON.
 *
 * @param params
 *          the params
 * @param json
 *          the json
 * @return the params from JSON
 */
@SuppressWarnings("unchecked")
private static void getParamsFromJSON(Map<String, String> params, String json) {
  JSONParser parser = new JSONParser(json);
  try {
    Object o = ObjectBuilder.getVal(parser);
    if (!(o instanceof Map))
      return;
    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 String) {
        params.put(key, (String) val);
      }
    }
  } catch (Exception e) {
    log.debug("ignore parse exceptions at this stage, they may be caused by incomplete macro expansions", e);
    return;
  }
}
 
Example #18
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 #19
Source File: SolrClient.java    From yarn-proto with Apache License 2.0 5 votes vote down vote up
public Map<String,Object> handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
  HttpEntity entity = response.getEntity();
  if (entity != null) {
    Object resp = ObjectBuilder.getVal(new JSONParser(EntityUtils.toString(entity)));
    if (resp != null && resp instanceof Map) {
      return (Map<String,Object>)resp;
    } else {
      throw new ClientProtocolException("Expected JSON object in response but received "+ resp);
    }
  } else {
    StatusLine statusLine = response.getStatusLine();
    throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
  }
}
 
Example #20
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 #21
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 #22
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 #23
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 #24
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 #25
Source File: JSONTestUtil.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * @param input Object structure to parse and test against
 * @param pathAndExpected JSON path expression + '==' + expected value
 * @param delta tollerance allowed in comparing float/double values
 */
public static String matchObj(Object input, String pathAndExpected, double delta) throws Exception {
  int pos = pathAndExpected.indexOf("==");
  String path = pos>=0 ? pathAndExpected.substring(0,pos) : null;
  String expected = pos>=0 ? pathAndExpected.substring(pos+2) : pathAndExpected;
  Object expectObj = failRepeatedKeys ? new NoDupsObjectBuilder(new JSONParser(expected)).getVal() : ObjectBuilder.fromJSON(expected);
  return matchObj(path, input, expectObj, delta);
}
 
Example #26
Source File: Utils.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static JSONParser getJSONParser(Reader reader) {
  JSONParser parser = new JSONParser(reader);
  parser.setFlags(parser.getFlags() |
      JSONParser.ALLOW_MISSING_COLON_COMMA_BEFORE_OBJECT |
      JSONParser.OPTIONAL_OUTER_BRACES);
  return parser;
}
 
Example #27
Source File: Utils.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static Object fromJSON(InputStream is, Function<JSONParser, ObjectBuilder> objBuilderProvider) {
  try {
    return objBuilderProvider.apply(getJSONParser((new InputStreamReader(is, StandardCharsets.UTF_8)))).getValStrict();
  } catch (IOException e) {
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Parse error", e);
  }
}
 
Example #28
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 #29
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 #30
Source File: ValidatingJsonMap.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static ObjectBuilder getObjectBuilder(final JSONParser jp) throws IOException {
  return new ObjectBuilder(jp) {
    @Override
    public Object newObject() throws IOException {
      return new ValidatingJsonMap();
    }
  };
}