@Override
 public Leaf deserialize(JsonParser jp, DeserializationContext ctxt)
     throws IOException, JsonProcessingException {
   JsonNode tree = (JsonNode) jp.readValueAsTree();
   Leaf leaf = new Leaf();
   leaf.value = tree.get("value").intValue();
   return leaf;
 }
 public void testEmbeddedObject() throws Exception {
   TokenBuffer buf = new TokenBuffer(MAPPER);
   buf.writeObject(new byte[3]);
   JsonNode node = MAPPER.readTree(buf.asParser());
   buf.close();
   assertTrue(node.isBinary());
   byte[] data = node.binaryValue();
   assertNotNull(data);
   assertEquals(3, data.length);
 }
 // [Issue#232]
 public void testBigDecimalAsPlainStringTreeConversion() throws Exception {
   ObjectMapper mapper = new ObjectMapper();
   mapper.enable(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN);
   Map<String, Object> map = new HashMap<String, Object>();
   String PI_STR = "3.00000000";
   map.put("pi", new BigDecimal(PI_STR));
   JsonNode tree = mapper.valueToTree(map);
   assertNotNull(tree);
   assertEquals(1, tree.size());
   assertTrue(tree.has("pi"));
 }
Example #4
0
 /**
  * Method for locating node specified by given JSON pointer instances. Method will never return
  * null; if no matching node exists, will return a node for which {@link #isMissingNode()} returns
  * true.
  *
  * @return Node that matches given JSON Pointer: if no match exists, will return a node for which
  *     {@link #isMissingNode()} returns true.
  * @since 2.3
  */
 @Override
 public final JsonNode at(JsonPointer ptr) {
   // Basically: value nodes only match if we have "empty" path left
   if (ptr.matches()) {
     return this;
   }
   JsonNode n = _at(ptr);
   if (n == null) {
     return MissingNode.getInstance();
   }
   return n.at(ptr.tail());
 }
 public void testEmbeddedObjectInArray() throws Exception {
   TokenBuffer buf = new TokenBuffer(MAPPER);
   buf.writeStartArray();
   buf.writeObject(MARKER);
   buf.writeEndArray();
   JsonNode node = MAPPER.readTree(buf.asParser());
   buf.close();
   assertTrue(node.isArray());
   assertEquals(1, node.size());
   JsonNode n = node.get(0);
   assertTrue(n.isPojo());
   assertSame(MARKER, ((POJONode) n).getPojo());
 }
Example #6
0
 /**
  * Method that is similar to {@link #has(int)}, but that will return <code>false</code> for
  * explicitly added nulls.
  *
  * <p>This method is equivalent to:
  *
  * <pre>
  *   node.get(index) != null &lt;&lt; !node.get(index).isNull()
  * </pre>
  *
  * @since 2.1
  */
 public boolean hasNonNull(int index) {
   JsonNode n = get(index);
   return (n != null) && !n.isNull();
 }
Example #7
0
 /**
  * Method that is similar to {@link #has(String)}, but that will return <code>false</code> for
  * explicitly added nulls.
  *
  * <p>This method is functionally equivalent to:
  *
  * <pre>
  *   node.get(fieldName) != null &lt;&lt; !node.get(fieldName).isNull()
  * </pre>
  *
  * @since 2.1
  */
 public boolean hasNonNull(String fieldName) {
   JsonNode n = get(fieldName);
   return (n != null) && !n.isNull();
 }
  private void func_27328_a(JsonNode p_27328_1_, Writer p_27328_2_) throws IOException {
    boolean flag = true;
    switch (CompactJsonFormatter_JsonNodeType.field_27341_a[p_27328_1_.func_27218_a().ordinal()]) {
      case 1: // '\001'
        p_27328_2_.append('[');
        JsonNode jsonnode;
        for (Iterator iterator = p_27328_1_.func_27215_d().iterator();
            iterator.hasNext();
            func_27328_a(jsonnode, p_27328_2_)) {
          jsonnode = (JsonNode) iterator.next();
          if (!flag) {
            p_27328_2_.append(',');
          }
          flag = false;
        }

        p_27328_2_.append(']');
        break;

      case 2: // '\002'
        p_27328_2_.append('{');
        JsonStringNode jsonstringnode;
        for (Iterator iterator1 = (new TreeSet(p_27328_1_.func_27214_c().keySet())).iterator();
            iterator1.hasNext();
            func_27328_a((JsonNode) p_27328_1_.func_27214_c().get(jsonstringnode), p_27328_2_)) {
          jsonstringnode = (JsonStringNode) iterator1.next();
          if (!flag) {
            p_27328_2_.append(',');
          }
          flag = false;
          func_27328_a(((JsonNode) (jsonstringnode)), p_27328_2_);
          p_27328_2_.append(':');
        }

        p_27328_2_.append('}');
        break;

      case 3: // '\003'
        p_27328_2_
            .append('"')
            .append((new JsonEscapedString(p_27328_1_.func_27216_b())).toString())
            .append('"');
        break;

      case 4: // '\004'
        p_27328_2_.append(p_27328_1_.func_27216_b());
        break;

      case 5: // '\005'
        p_27328_2_.append("false");
        break;

      case 6: // '\006'
        p_27328_2_.append("true");
        break;

      case 7: // '\007'
        p_27328_2_.append("null");
        break;

      default:
        throw new RuntimeException(
            (new StringBuilder())
                .append("Coding failure in Argo:  Attempt to format a JsonNode of unknown type [")
                .append(p_27328_1_.func_27218_a())
                .append("];")
                .toString());
    }
  }
  /** tests getting serializer/deserializer instances. */
  public void testSerializeDeserializeWithJaxbAnnotations() throws Exception {
    ObjectMapper mapper = getJaxbMapper();
    // test expects that wrapper name be used...
    mapper.enable(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME);

    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    JaxbExample ex = new JaxbExample();
    QName qname = new QName("urn:hi", "hello");
    ex.setQname(qname);
    QName qname1 = new QName("urn:hi", "hello1");
    ex.setQname1(qname1);
    ex.setAttributeProperty("attributeValue");
    ex.setElementProperty("elementValue");
    ex.setWrappedElementProperty(Arrays.asList("wrappedElementValue"));
    ex.setEnumProperty(EnumExample.VALUE1);
    ex.setPropertyToIgnore("ignored");
    String json = mapper.writeValueAsString(ex);

    // uncomment to see what the JSON looks like.
    // System.out.println(json);

    // make sure the json is written out correctly.
    JsonNode node = mapper.readValue(json, JsonNode.class);
    assertEquals(qname.toString(), node.get("qname").asText());
    JsonNode attr = node.get("myattribute");
    assertNotNull(attr);
    assertEquals("attributeValue", attr.asText());
    assertEquals("elementValue", node.get("myelement").asText());
    assertTrue(node.has("mywrapped"));
    assertEquals(1, node.get("mywrapped").size());
    assertEquals("wrappedElementValue", node.get("mywrapped").get(0).asText());
    assertEquals("Value One", node.get("enumProperty").asText());
    assertNull(node.get("propertyToIgnore"));

    // now make sure it gets deserialized correctly.
    JaxbExample readEx = mapper.readValue(json, JaxbExample.class);
    assertEquals(ex.qname, readEx.qname);
    assertEquals(ex.qname1, readEx.qname1);
    assertEquals(ex.attributeProperty, readEx.attributeProperty);
    assertEquals(ex.elementProperty, readEx.elementProperty);
    assertEquals(ex.wrappedElementProperty, readEx.wrappedElementProperty);
    assertEquals(ex.enumProperty, readEx.enumProperty);
    assertNull(readEx.propertyToIgnore);
  }