Пример #1
0
  /**
   * Converts object to JSON.
   *
   * @param o object to convert.
   * @return {@link JsonNode}
   */
  public static JsonNode objectToJson(Object o) {

    ObjectMapper mapper = new ObjectMapper();
    try {
      return mapper.valueToTree(o);
    } catch (Exception e) {
      log.error(String.format("JsonUtils.objectToJson(): Unable to write json"));
    }
    return null;
  }
Пример #2
0
 /**
  * Convenience method for creating an error response
  *
  * @param jsonRpc the jsonrpc string
  * @param id the id
  * @param code the error code
  * @param message the error message
  * @param data the error data (if any)
  * @return the error response
  */
 private ObjectNode createErrorResponse(
     String jsonRpc, String id, int code, String message, Object data) {
   ObjectNode response = mapper.createObjectNode();
   ObjectNode error = mapper.createObjectNode();
   error.put("code", code);
   error.put("message", message);
   if (data != null) {
     error.put("data", mapper.valueToTree(data));
   }
   response.put("jsonrpc", jsonRpc);
   response.put("id", id);
   response.put("error", error);
   return response;
 }
Пример #3
0
  /**
   * Invokes the given method on the {@code handler} passing the given params (after converting them
   * to beans\objects) to it.
   *
   * @param m the method to invoke
   * @param params the params to pass to the method
   * @return the return value (or null if no return)
   * @throws JsonParseException
   * @throws JsonMappingException
   * @throws IOException
   * @throws IllegalArgumentException
   * @throws IllegalAccessException
   * @throws InvocationTargetException
   */
  private JsonNode invoke(Method m, List<JsonNode> params)
      throws JsonParseException, JsonMappingException, IOException, IllegalArgumentException,
          IllegalAccessException, InvocationTargetException {

    // convert the parameters
    Object[] convertedParams = new Object[params.size()];
    Type[] parameterTypes = m.getGenericParameterTypes();
    for (int i = 0; i < parameterTypes.length; i++) {
      convertedParams[i] =
          mapper.treeToValue(params.get(i), TypeFactory.type(parameterTypes[i]).getRawClass());
    }

    // invoke the method
    Object result = m.invoke(handler, convertedParams);
    return (m.getGenericReturnType() != null) ? mapper.valueToTree(result) : null;
  }
Пример #4
0
  @Override
  public void render(Map<String, ?> map, HttpServletRequest req, HttpServletResponse resp)
      throws Exception {
    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
    mapper.getDeserializationConfig().setAnnotationIntrospector(introspector);
    mapper.getSerializationConfig().setAnnotationIntrospector(introspector);

    // resp.setHeader("Content-Type", "text/html; charset=UTF-8"); // "application/json");

    JsonGenerator generator =
        mapper.getJsonFactory().createJsonGenerator(resp.getOutputStream(), JsonEncoding.UTF8);

    ObjectNode json = mapper.valueToTree(map);
    // json.put("success", true);
    mapper.writeTree(generator, json);
    // mapper.writeValue(generator, ret);
    generator.flush();
  }