Examples of JsonNode


Examples of org.codehaus.jackson.JsonNode

    // parse request
    String jsonRpc    = node.get("jsonrpc").getValueAsText();
    String methodName  = node.get("method").getValueAsText();
    String id      = node.get("id").getValueAsText();
    JsonNode params    = node.get("params");
    int paramCount    = (params!=null) ? params.size() : 0;

    // find methods
    Set<Method> methods = new HashSet<Method>();
    methods.addAll(ReflectionUtil.findMethods(getHandlerClass(), methodName));

    // iterate through the methods and remove
    // the one's who's parameter count's don't
    // match the request
    Iterator<Method> itr = methods.iterator();
    while (itr.hasNext()) {
      Method method = itr.next();
      if (method.getParameterTypes().length!=paramCount) {
        itr.remove();
      }
    }

    // method not found
    if (methods.isEmpty()) {
      mapper.writeValue(ops, createErrorResponse(
        jsonRpc, id, -32601, "Method not found", null));
      return;
    }

    // choose a method
    Method method = null;
    List<JsonNode> paramNodes = new ArrayList<JsonNode>();

    // handle param arrays, no params
    if (paramCount==0 || params.isArray()) {
      method = methods.iterator().next();
      for (int i=0; i<paramCount; i++) {
        paramNodes.add(params.get(i));
      }

    // handle named params
    } else if (params.isObject()) {

      // loop through each method
      for (Method m : methods) {

        // get method annotations
        Annotation[][] annotations = m.getParameterAnnotations();
        boolean found = true;
        List<JsonNode> namedParams = new ArrayList<JsonNode>();
        for (int i=0; i<annotations.length; i++) {

          // look for param name annotations
          String paramName = null;
          for (int j=0; j<annotations[i].length; j++) {
            if (!JsonRpcParamName.class.isInstance(annotations[i][j])) {
              continue;
            } else {
              paramName = JsonRpcParamName.class.cast(annotations[i][j]).value();
              continue;
            }
          }

          // bail if param name wasn't found
          if (paramName==null) {
            found = false;
            break;

          // found it by name
          } else if (params.has(paramName)) {
            namedParams.add(params.get(paramName));
          }
        }

        // did we find it?
        if (found) {
          method = m;
          paramNodes.addAll(namedParams);
          break;
        }
      }
    }

    // invalid parameters
    if (method==null) {
      mapper.writeValue(ops, createErrorResponse(
        jsonRpc, id, -32602, "Invalid method parameters.", null));
      return;
    }

    // get @JsonRpcErrors and nested @JsonRpcError annotations on method
    Annotation[] methodAnnotations = method.getAnnotations();
    JsonRpcError[] errorMappings = {};
    for (Annotation a : methodAnnotations) {
      if (!JsonRpcErrors.class.isInstance(a)) {
        continue;
      } else {
        errorMappings = JsonRpcErrors.class.cast(a).value();
      }
    }

    // invoke the method
    JsonNode result = null;
    ObjectNode error = null;
    Throwable thrown = null;
    try {
      result = invoke(method, paramNodes);
    } catch (Throwable e) {
View Full Code Here

Examples of org.codehaus.jackson.JsonNode

  public void exceptionWithoutAnnotatedServiceInterface() throws Exception {
    JsonRpcServer jsonRpcServer = new JsonRpcServer(mapper, new Service(), ServiceInterfaceWithoutAnnotation.class);
    jsonRpcServer.handle(new ClassPathResource(JSON_FILE).getInputStream(), baos);

    String response = baos.toString(JSON_ENCODING);       
    JsonNode json = mapper.readTree(response);
    JsonNode error = json.get("error");

    assertNotNull(error);       
    assertEquals(0, error.get("code").getIntValue());
    assertEquals(testException.getMessage(), error.get("message").getTextValue());
    assertEquals(TestException.class.getName(), error.get("data").getTextValue());       
  }
View Full Code Here

Examples of org.codehaus.jackson.JsonNode

  public void exceptionWithAnnotatedServiceInterface() throws Exception {
    JsonRpcServer jsonRpcServer = new JsonRpcServer(mapper, new Service(), ServiceInterfaceWithAnnotation.class);
    jsonRpcServer.handle(new ClassPathResource(JSON_FILE).getInputStream(), baos);

    String response = baos.toString(JSON_ENCODING);       
    JsonNode json = mapper.readTree(response);
    JsonNode error = json.get("error");

    assertNotNull(error);       
    assertEquals(1234, error.get("code").getIntValue());
    assertEquals("", error.get("message").getTextValue());
    assertNull(error.get("data"));       
  }
View Full Code Here

Examples of org.codehaus.jackson.JsonNode

  public void exceptionWithAnnotatedServiceInterfaceMessageAndData() throws Exception {
    JsonRpcServer jsonRpcServer = new JsonRpcServer(mapper, new Service(), ServiceInterfaceWithAnnotationMessageAndData.class);
    jsonRpcServer.handle(new ClassPathResource(JSON_FILE).getInputStream(), baos);

    String response = baos.toString(JSON_ENCODING);
    JsonNode json = mapper.readTree(response);
    JsonNode error = json.get("error");

    assertNotNull(error);       
    assertEquals(-5678, error.get("code").getIntValue());
    assertEquals("The message", error.get("message").getTextValue());
    assertEquals("The data", error.get("data").getTextValue());
  }
View Full Code Here

Examples of org.codehaus.jackson.JsonNode

  public void exceptionWithMsgInException() throws Exception {
    JsonRpcServer jsonRpcServer = new JsonRpcServer(mapper, new ServiceWithExceptionMsg(), ServiceInterfaceWithAnnotation.class);
    jsonRpcServer.handle(new ClassPathResource(JSON_FILE).getInputStream(), baos);

    String response = baos.toString(JSON_ENCODING);
    JsonNode json = mapper.readTree(response);
    JsonNode error = json.get("error");

    assertNotNull(error);       
    assertEquals(1234, error.get("code").getIntValue());
    assertEquals("", error.get("message").getTextValue());
    assertEquals(testExceptionWithMessage.getMessage(), error.get("data").getTextValue());
  }
View Full Code Here

Examples of org.codehaus.jackson.JsonNode

    if (jsonTree == null || jsonTree.isMissingNode()) {
      throw new JRException("No JSON data to operate on!");
    }

    currentJsonNode = null;
    JsonNode result = getJsonData(jsonTree, selectExpression);
    if (result != null && result.isObject()) {
//      System.out.println("result is object");
      final List<JsonNode> list = new ArrayList<JsonNode>();
      list.add(result);
      jsonNodesIterator = new Iterator<JsonNode>() {
        private int count = -1;
        public void remove() {
          list.remove(count);
        }
       
        public JsonNode next() {
          count ++;
          return list.get(count);
        }
       
        public boolean hasNext() {
          return count < list.size()-1;
        }
      };
    } else if (result != null && result.isArray()) {
//      System.out.println("result is array");
      jsonNodesIterator = result.getElements();
    }
  }
View Full Code Here

Examples of org.codehaus.jackson.JsonNode

      return null;
    }
    Object value = null;
   
    Class valueClass = jrField.getValueClass();
    JsonNode selectedObject = getJsonData(currentJsonNode, expression);
   
    if(Object.class != valueClass)
    {
      if (selectedObject != null)
      {
        try {
          if (valueClass.equals(String.class)) {
            value = selectedObject.getValueAsText();
           
          } else if (valueClass.equals(Boolean.class)) {
            value = selectedObject.getBooleanValue();
           
          } else if (Number.class.isAssignableFrom(valueClass)) {
              value = convertStringValue(selectedObject.getValueAsText(), valueClass);
             
          }
          else if (Date.class.isAssignableFrom(valueClass)) {
              value = convertStringValue(selectedObject.getValueAsText(), valueClass);
             
          } else {
            throw new JRException("Field '" + jrField.getName() + "' is of class '" + valueClass.getName() + "' and cannot be converted");
          }
        } catch (Exception e) {
View Full Code Here

Examples of org.codehaus.jackson.JsonNode

   */
  protected JsonNode getJsonData(JsonNode rootNode, String jsonExpression) throws JRException {
    if (jsonExpression == null || jsonExpression.length() == 0) {
      return rootNode;
    }
    JsonNode tempNode = rootNode;
    StringTokenizer tokenizer = new StringTokenizer(jsonExpression, PROPERTY_SEPARATOR);
   
    while(tokenizer.hasMoreTokens()) {
      String currentToken = tokenizer.nextToken();
      int currentTokenLength = currentToken.length();
      int indexOfLeftSquareBracket = currentToken.indexOf(ARRAY_LEFT);

      // got Left Square Bracket - LSB
      if (indexOfLeftSquareBracket != -1) {
        // a Right Square Bracket must be the last character in the current token
        if(currentToken.lastIndexOf(ARRAY_RIGHT) != (currentTokenLength-1)) {
          throw new JRException("Invalid expression: " + jsonExpression + "; current token " + currentToken + " not ended properly");
        }
       
        // LSB not first character
        if (indexOfLeftSquareBracket > 0) {
          // extract nodes at property
          String property = currentToken.substring(0, indexOfLeftSquareBracket);
          tempNode = goDownPathWithAttribute(tempNode, property);
         
          String arrayOperators = currentToken.substring(indexOfLeftSquareBracket);
          StringTokenizer arrayOpsTokenizer = new StringTokenizer(arrayOperators,ARRAY_RIGHT);
          while(arrayOpsTokenizer.hasMoreTokens()) {
            if (!tempNode.isMissingNode() && tempNode.isArray()) {
              String currentArrayOperator = arrayOpsTokenizer.nextToken();
              tempNode = tempNode.path(Integer.parseInt(currentArrayOperator.substring(1)));
            }
          }
        } else { // LSB first character
          String arrayOperators = currentToken.substring(indexOfLeftSquareBracket);
          StringTokenizer arrayOpsTokenizer = new StringTokenizer(arrayOperators,ARRAY_RIGHT);
          while(arrayOpsTokenizer.hasMoreTokens()) {
            if (!tempNode.isMissingNode() && tempNode.isArray()) {
              String currentArrayOperator = arrayOpsTokenizer.nextToken();
              tempNode = tempNode.path(Integer.parseInt(currentArrayOperator.substring(1)));
            }
          }
        }
      } else {
        tempNode = goDownPathWithAttribute(tempNode, currentToken);
View Full Code Here

Examples of org.codehaus.jackson.JsonNode

        String path = pathWithAttributeExpression.substring(0, indexOfLeftRoundBracket);
       
        // an expression in a form like: attribute==value
        String attributeExpression = pathWithAttributeExpression.substring(indexOfLeftRoundBracket + 1, pathWithAttributeExpression.length() - 1);
       
        JsonNode result = null;
        if (rootNode.isObject()) {
          // select only those nodes for which the attribute expression applies
          if (!rootNode.path(path).isMissingNode()) {
            if (rootNode.path(path).isObject()) {
              if (isValidExpression(rootNode.path(path), attributeExpression)) {
                result = rootNode.path(path);
              }
            } else if (rootNode.path(path).isArray()) {
              result = mapper.createArrayNode();
              for (JsonNode node: rootNode.path(path)) {
                if (isValidExpression(node, attributeExpression)) {
                  ((ArrayNode)result).add(node);
                }
              }
            }
          }
        } else if (rootNode.isArray()) {
          result = mapper.createArrayNode();
          for (JsonNode node: rootNode) {
            JsonNode deeperNode = node.path(path);
            if (!deeperNode.isMissingNode() && isValidExpression(deeperNode, attributeExpression)) {
              ((ArrayNode)result).add(deeperNode);
            }
          }
        }
        return result;
View Full Code Here

Examples of org.codehaus.jackson.JsonNode

   * @param simplePath - a simple field name, with no selection by attribute
   * @return
   */
  protected JsonNode goDownPath(JsonNode rootNode, String simplePath) {
    if(rootNode != null && !rootNode.isMissingNode()) {
      JsonNode result = null;
      if (rootNode.isObject()) {
        result = rootNode.path(simplePath);
      } else if (rootNode.isArray()) {
        result = mapper.createArrayNode();
        for (JsonNode node: rootNode) {
          JsonNode deeperNode = node.path(simplePath);
          if (!deeperNode.isMissingNode()) {
            ((ArrayNode)result).add(deeperNode);
          }
        }
      }
      return result;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.