Java Code Examples for org.noggit.ObjectBuilder#getVal()

The following examples show how to use org.noggit.ObjectBuilder#getVal() . 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: 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: 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 4
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 5
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 6
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;
  }

}