/** Unit test to check for regression of [JACKSON-18]. */
  public void testSmallNumbers() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    ArrayNode root = mapper.createArrayNode();
    for (int i = -20; i <= 20; ++i) {
      JsonNode n = root.numberNode(i);
      root.add(n);
      // Hmmh. Not sure why toString() won't be triggered otherwise...
      assertEquals(String.valueOf(i), n.toString());
    }

    // Loop over 2 different serialization methods
    for (int type = 0; type < 2; ++type) {
      StringWriter sw = new StringWriter();
      if (type == 0) {
        JsonGenerator gen = new JsonFactory().createGenerator(sw);
        root.serialize(gen, null);
        gen.close();
      } else {
        mapper.writeValue(sw, root);
      }

      String doc = sw.toString();
      JsonParser p = new JsonFactory().createParser(new StringReader(doc));

      assertEquals(JsonToken.START_ARRAY, p.nextToken());
      for (int i = -20; i <= 20; ++i) {
        assertEquals(JsonToken.VALUE_NUMBER_INT, p.nextToken());
        assertEquals(i, p.getIntValue());
        assertEquals("" + i, p.getText());
      }
      assertEquals(JsonToken.END_ARRAY, p.nextToken());
      p.close();
    }
  }
示例#2
1
  public CallTwoArgBlockNode(
      ISourcePosition position, Node receiverNode, String name, ArrayNode args, IterNode iter) {
    super(position, receiverNode, name, args, iter);

    assert args.size() == 2 : "args.size() is 2";

    arg1 = args.get(0);
    arg2 = args.get(1);
  }
  /** {@inheritDoc} */
  @Override
  public V remove(K key) {
    Node node = find(key.hashCode());
    if (node == null) return null;
    if (node instanceof ArrayNode) return null;

    KeyValueNode<V> kvNode = (KeyValueNode<V>) node;
    V value = kvNode.value;
    if (node.parent == null) {
      // If parent is null, removing the root
      root = null;
    } else {
      ArrayNode parent = node.parent;
      // Remove child from parent
      int position = getPosition(parent.height, node.key);
      parent.removeChild(position);
      // Go back up the tree, pruning any array nodes which no longer have children.
      int numOfChildren = parent.getNumberOfChildren();
      while (numOfChildren == 0) {
        node = parent;
        parent = node.parent;
        if (parent == null) {
          // Reached root of the tree, need to stop
          root = null;
          break;
        }
        position = getPosition(parent.height, node.key);
        parent.removeChild(position);
        numOfChildren = parent.getNumberOfChildren();
      }
    }
    size--;
    return value;
  }
    private static <K, V> String getString(
        Node parent, Node node, int height, String prefix, boolean isTail) {
      StringBuilder builder = new StringBuilder();

      if (node instanceof KeyValueNode) {
        KeyValueNode<V> kvNode = (KeyValueNode<V>) node;
        int position = getPosition(height, kvNode.key);
        builder.append(
            prefix
                + (isTail ? "└── " : "├── ")
                + ((parent == null) ? null : toBinaryString(position))
                + "="
                + toBinaryString(kvNode.key)
                + "="
                + kvNode.value
                + "\n");
      } else {
        ArrayNode arrayNode = (ArrayNode) node;
        int position = (arrayNode.parent == null) ? -1 : getPosition(height, arrayNode.key);
        builder.append(
            prefix
                + (isTail ? "└── " : "├── ")
                + ((parent == null) ? null : toBinaryString(position))
                + " height="
                + ((height < 0) ? null : height)
                + " bitmap="
                + toBinaryString(arrayNode.bitmap)
                + "\n");
        List<Node> children = new LinkedList<Node>();
        for (int i = 0; i < MAX_BITS; i++) {
          Node child = arrayNode.getChild(i);
          if (child != null) children.add(child);
        }
        for (int i = 0; i < (children.size() - 1); i++) {
          builder.append(
              getString(
                  arrayNode,
                  children.get(i),
                  height + 1,
                  prefix + (isTail ? "    " : "│   "),
                  false));
        }
        if (children.size() >= 1) {
          builder.append(
              getString(
                  arrayNode,
                  children.get(children.size() - 1),
                  height + 1,
                  prefix + (isTail ? "    " : "│   "),
                  true));
        }
      }
      return builder.toString();
    }
 private void addToSet(java.util.Set<java.util.Map.Entry<K, V>> set, Node node) {
   if (node instanceof KeyValueNode) {
     KeyValueNode<V> kvNode = (KeyValueNode<V>) node;
     set.add(new JavaCompatibleMapEntry<K, V>((K) new Integer(kvNode.key), kvNode.value));
   } else if (node instanceof ArrayNode) {
     ArrayNode arrayNode = (ArrayNode) node;
     for (int i = 0; i < MAX_BITS; i++) {
       Node child = arrayNode.getChild(i);
       if (child != null) addToSet(set, child);
     }
   }
 }
示例#6
0
 public String toJson(ObjectMapper mapper) {
   ObjectNode rootNode = mapper.createObjectNode();
   ArrayNode keysNode = rootNode.putArray("keys");
   for (Object key : keys) {
     keysNode.addPOJO(key);
   }
   try {
     return mapper.writeValueAsString(rootNode);
   } catch (Exception e) {
     throw Exceptions.propagate(e);
   }
 }
 private Node find(Node node, int key) {
   if (node instanceof KeyValueNode) {
     KeyValueNode<V> kvNode = (KeyValueNode<V>) node;
     if (kvNode.key == key) return kvNode;
     return null;
   } else if (node instanceof ArrayNode) {
     ArrayNode arrayNode = (ArrayNode) node;
     int position = getPosition(arrayNode.height, key);
     Node possibleNode = arrayNode.getChild(position);
     if (possibleNode == null) return null;
     return find(possibleNode, key);
   }
   return null;
 }
  private static <K, V> boolean validate(ArrayNode parent, ArrayNode node) {
    if (parent != null) {
      if (parent.key != (node.parent.key)) return false;
      if (parent.height + 1 != node.height) return false;
    }

    int children = 0;
    for (int i = 0; i < node.children.length; i++) {
      Node child = node.children[i];
      if (child != null) {
        children++;
        if (child instanceof KeyValueNode) {
          KeyValueNode<V> kvChild = (KeyValueNode<V>) child;
          if (!validate(node, kvChild)) return false;
        } else if (child instanceof ArrayNode) {
          ArrayNode arrayNode = (ArrayNode) child;
          if (!validate(node, arrayNode)) return false;
        } else {
          return false;
        }
      }
    }
    boolean result = (children == node.getNumberOfChildren());
    if (!result) {
      return false;
    }
    return true;
  }
  public void testFromArray() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    ArrayNode root = mapper.createArrayNode();
    root.add(TEXT1);
    root.add(3);
    ObjectNode obj = root.addObject();
    obj.put(FIELD1, true);
    obj.putArray(FIELD2);
    root.add(false);

    /* Ok, ready... let's serialize using one of two alternate
     * methods: first preferred (using generator)
     * (there are 2 variants here too)
     */
    for (int i = 0; i < 2; ++i) {
      StringWriter sw = new StringWriter();
      if (i == 0) {
        JsonGenerator gen = new JsonFactory().createGenerator(sw);
        root.serialize(gen, null);
        gen.close();
      } else {
        mapper.writeValue(sw, root);
      }
      verifyFromArray(sw.toString());
    }

    // And then convenient but less efficient alternative:
    verifyFromArray(root.toString());
  }
  private V put(ArrayNode parent, Node node, byte height, int key, V value) {
    if (node instanceof KeyValueNode) {
      KeyValueNode<V> kvNode = (KeyValueNode<V>) node;
      if (key == kvNode.key) {
        // Key already exists as key-value pair, replace value
        kvNode.value = value;
        return value;
      }
      // Key already exists but doesn't match current key
      KeyValueNode<V> oldParent = kvNode;
      int newParentPosition = getPosition(height - 1, key);
      int oldParentPosition = getPosition(height, oldParent.key);
      int childPosition = getPosition(height, key);
      ArrayNode newParent = new ArrayNode(parent, key, height);
      newParent.parent = parent;
      if (parent == null) {
        // Only the root doesn't have a parent, so new root
        root = newParent;
      } else {
        // Add the child to the parent in it's parent's position
        parent.addChild(newParentPosition, newParent);
      }
      if (oldParentPosition != childPosition) {
        // The easy case, the two children have different positions in parent
        newParent.addChild(oldParentPosition, oldParent);
        oldParent.parent = newParent;
        newParent.addChild(childPosition, new KeyValueNode<V>(newParent, key, value));
        return null;
      }
      while (oldParentPosition == childPosition) {
        // Handle the case when the new children map to same position.
        height++;
        if (height > MAX_DEPTH) {
          // We have found two keys which match exactly.
          System.err.println("Yikes! Found two keys which match exactly.");
          return null;
        }
        newParentPosition = getPosition(height - 1, key);
        ArrayNode newParent2 = new ArrayNode(newParent, key, height);
        newParent.addChild(newParentPosition, newParent2);

        oldParentPosition = getPosition(height, oldParent.key);
        childPosition = getPosition(height, key);
        if (oldParentPosition != childPosition) {
          newParent2.addChild(oldParentPosition, oldParent);
          oldParent.parent = newParent2;
          newParent2.addChild(childPosition, new KeyValueNode<V>(newParent2, key, value));
        } else {
          newParent = newParent2;
        }
      }
      return null;
    } else if (node instanceof ArrayNode) {
      ArrayNode arrayRoot = (ArrayNode) node;
      int position = getPosition(arrayRoot.height, key);
      Node child = arrayRoot.getChild(position);
      if (child == null) {
        // Found an empty slot in parent
        arrayRoot.addChild(position, new KeyValueNode<V>(arrayRoot, key, value));
        return null;
      }
      return put(arrayRoot, child, (byte) (height + 1), key, value);
    }
    return null;
  }
示例#11
0
  public YieldTwoNode(ISourcePosition position, ArrayNode args) {
    super(position, args, true);

    argument1 = args.get(0);
    argument2 = args.get(1);
  }
示例#12
0
  private void handleProperties(
      Map<String, Object> mapToFill,
      ArrayList<Map.Entry<String, JsonNode>> itProperties,
      Object returnedValue)
      throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

    for (int k = 0; k < itProperties.size(); k++) {
      String titleBean = itProperties.get(k).getKey();

      FMDProperty objectProperty =
          fillObjectProperty(
              Lists.newArrayList(itProperties.get(k).getValue().fields()).listIterator());
      if (objectProperty.getTitleBean() == null) {
        objectProperty.setTitleBean(titleBean);
      }

      Object mdsdValue = getReturnedValueFromObject(objectProperty, returnedValue, titleBean);

      if (mdsdValue != null) {

        // if there is not a reference
        if (objectProperty.getReference() == null) {
          String orderObj = getOrderFromEntity(objectProperty.getOrder());
          if (isReadyToPut(objectProperty)) {
            Object valueToPut =
                ((((ValueNode) mdsdValue).getNodeType().toString()).equals("NUMBER"))
                    ? mdsdValue
                    : ((TextNode) mdsdValue).asText();
            mapToFill.put(
                orderObj,
                new FMDescriptor(
                    titleBean,
                    objectProperty.getTitleToVisualize(),
                    objectProperty.getDescription(),
                    valueToPut));
          } else if (objectProperty.getType() != null
              && objectProperty.getType().equals(ARRAY_TYPE)
              && objectProperty.getItems().get(TYPE_FIELD) != null
              && objectProperty.getItems().get(TYPE_FIELD).asText().equals(STRING_TYPE)) {
            ArrayNode values = ((ArrayNode) mdsdValue);
            for (int i = 0, size = values.size(); i < size; i++) {
              values.add(values.get(i).textValue());
            }
            mapToFill.put(
                orderObj,
                new FMDescriptor(
                    titleBean,
                    objectProperty.getTitleToVisualize(),
                    objectProperty.getDescription(),
                    values));
          } else {
            mapToFill.put(
                orderObj,
                new FMDescriptor(
                    titleBean,
                    objectProperty.getTitleToVisualize(),
                    objectProperty.getDescription(),
                    fillRecursive2(itProperties.get(k).getValue().fields(), mdsdValue)));
          }
        }
        // if it is a reference
        else {
          handleReferences(
              objectProperty.getReference(), objectProperty, mapToFill, mdsdValue, false);
        }
      }
    }
  }