org.noggit.ObjectBuilder Java Examples

The following examples show how to use org.noggit.ObjectBuilder. 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: SolrTestCaseHS.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static void compare(SolrQueryRequest req, String path, Object model,
                           @SuppressWarnings({"rawtypes"})Map<Comparable, Doc> fullModel) throws Exception {
  String strResponse = h.query(req);

  Object realResponse = ObjectBuilder.fromJSON(strResponse);
  String err = JSONTestUtil.matchObj(path, realResponse, model);
  if (err != null) {
    log.error("RESPONSE MISMATCH: {}\n\trequest={}\n\tresult={}" +
        "\n\texpected={}\n\tmodel={}"
        , err, req, strResponse, JSONUtil.toJSON(model), fullModel);

    // re-execute the request... good for putting a breakpoint here for debugging
    String rsp = h.query(req);

    fail(err);
  }

}
 
Example #3
Source File: TestJsonFacetsStatsParsing.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked"})
public void testVerboseSyntaxWithLocalParams() throws IOException {
  // some parsers may choose to use "global" req params as defaults/shadows for
  // local params, but DebugAgg does not -- so use these to test that the
  // JSON Parsing doesn't pollute the local params the ValueSourceParser gets...
  try (SolrQueryRequest req = req("foo", "zzzz", "yaz", "zzzzz")) { 
    final FacetRequest fr = FacetRequest.parse
      (req, (Map<String,Object>) ObjectBuilder.fromJSON
       ("{ x:{type:func, func:'debug()', foo:['abc','xyz'], bar:4.2 } }"));

    final Map<String, AggValueSource> stats = fr.getFacetStats();
    assertEquals(1, stats.size());
    AggValueSource agg = stats.get("x");
    assertNotNull(agg);
    assertThat(agg, instanceOf(DebugAgg.class));
    
    DebugAgg x = (DebugAgg)agg;
    assertEquals(new String[] {"abc", "xyz"}, x.localParams.getParams("foo"));
    assertEquals((Float)4.2F, x.localParams.getFloat("bar"));
    assertNull(x.localParams.get("yaz"));
  }
}
 
Example #4
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 #5
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 #6
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();
    }
  };
}
 
Example #7
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 #8
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 #9
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 #10
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 #11
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static Long deleteByQueryAndGetVersion(String q, SolrParams params) throws Exception {
  if (params==null || params.get("versions") == null) {
    ModifiableSolrParams mparams = new ModifiableSolrParams(params);
    mparams.set("versions","true");
    params = mparams;
  }
  String response = updateJ(jsonDelQ(q), params);
  @SuppressWarnings({"rawtypes"})
  Map rsp = (Map)ObjectBuilder.fromJSON(response);
  @SuppressWarnings({"rawtypes"})
  List lst = (List)rsp.get("deleteByQuery");
  if (lst == null || lst.size() == 0) return null;
  return (Long) lst.get(1);
}
 
Example #12
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static Long deleteAndGetVersion(String id, SolrParams params) throws Exception {
  if (params==null || params.get("versions") == null) {
    ModifiableSolrParams mparams = new ModifiableSolrParams(params);
    mparams.set("versions","true");
    params = mparams;
  }
  String response = updateJ(jsonDelId(id), params);
  @SuppressWarnings({"rawtypes"})
  Map rsp = (Map)ObjectBuilder.fromJSON(response);
  @SuppressWarnings({"rawtypes"})
  List lst = (List)rsp.get("deletes");
  if (lst == null || lst.size() == 0) return null;
  return (Long) lst.get(1);
}
 
Example #13
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static Long addAndGetVersion(SolrInputDocument sdoc, SolrParams params) throws Exception {
  if (params==null || params.get("versions") == null) {
    ModifiableSolrParams mparams = new ModifiableSolrParams(params);
    mparams.set("versions","true");
    params = mparams;
  }
  String response = updateJ(jsonAdd(sdoc), params);
  @SuppressWarnings({"rawtypes"})
  Map rsp = (Map)ObjectBuilder.fromJSON(response);
  @SuppressWarnings({"rawtypes"})
  List lst = (List)rsp.get("adds");
  if (lst == null || lst.size() == 0) return null;
  return (Long) lst.get(1);
}
 
Example #14
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 #15
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 #16
Source File: DefaultWrapperModel.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
protected Map<String, Object> parseInputStream(InputStream in) throws IOException {
  try (Reader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
    return (Map<String, Object>) new ObjectBuilder(new JSONParser(reader)).getValStrict();
  }
}
 
Example #17
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 #18
Source File: JSONTestUtil.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * @param path JSON path expression
 * @param input JSON Structure to parse and test against
 * @param expected expected value of path
 * @param delta tollerance allowed in comparing float/double values
 */
public static String match(String path, String input, String expected, double delta) throws Exception {
  Object inputObj = failRepeatedKeys ? new NoDupsObjectBuilder(new JSONParser(input)).getVal() : ObjectBuilder.fromJSON(input);
  Object expectObj = failRepeatedKeys ? new NoDupsObjectBuilder(new JSONParser(expected)).getVal() : ObjectBuilder.fromJSON(expected);
  return matchObj(path, inputObj, expectObj, delta);
}