コード例 #1
0
  // for [JACKSON-652]
  // @since 1.9
  public void testUntypedWithJsonArrays() throws Exception {
    // by default we get:
    Object ob = MAPPER.readValue("[1]", Object.class);
    assertTrue(ob instanceof List<?>);

    // but can change to produce Object[]:
    MAPPER.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
    ob = MAPPER.readValue("[1]", Object.class);
    assertEquals(Object[].class, ob.getClass());
  }
コード例 #2
0
  /** Test for [JACKSON-420] (add DeserializationConfig.FAIL_ON_NULL_FOR_PRIMITIVES) */
  public void testNullForPrimitives() throws IOException {
    // by default, ok to rely on defaults
    PrimitivesBean bean =
        MAPPER.readValue(
            "{\"intValue\":null, \"booleanValue\":null, \"doubleValue\":null}",
            PrimitivesBean.class);
    assertNotNull(bean);
    assertEquals(0, bean.intValue);
    assertEquals(false, bean.booleanValue);
    assertEquals(0.0, bean.doubleValue);

    bean =
        MAPPER.readValue(
            "{\"byteValue\":null, \"longValue\":null, \"floatValue\":null}", PrimitivesBean.class);
    assertNotNull(bean);
    assertEquals((byte) 0, bean.byteValue);
    assertEquals(0L, bean.longValue);
    assertEquals(0.0f, bean.floatValue);

    // but not when enabled
    final ObjectMapper mapper2 = new ObjectMapper();
    mapper2.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);

    // boolean
    try {
      mapper2.readValue("{\"booleanValue\":null}", PrimitivesBean.class);
      fail("Expected failure for boolean + null");
    } catch (JsonMappingException e) {
      verifyException(e, "Can not map JSON null into type boolean");
    }
    // byte/char/short/int/long
    try {
      mapper2.readValue("{\"byteValue\":null}", PrimitivesBean.class);
      fail("Expected failure for byte + null");
    } catch (JsonMappingException e) {
      verifyException(e, "Can not map JSON null into type byte");
    }
    try {
      mapper2.readValue("{\"charValue\":null}", PrimitivesBean.class);
      fail("Expected failure for char + null");
    } catch (JsonMappingException e) {
      verifyException(e, "Can not map JSON null into type char");
    }
    try {
      mapper2.readValue("{\"shortValue\":null}", PrimitivesBean.class);
      fail("Expected failure for short + null");
    } catch (JsonMappingException e) {
      verifyException(e, "Can not map JSON null into type short");
    }
    try {
      mapper2.readValue("{\"intValue\":null}", PrimitivesBean.class);
      fail("Expected failure for int + null");
    } catch (JsonMappingException e) {
      verifyException(e, "Can not map JSON null into type int");
    }
    try {
      mapper2.readValue("{\"longValue\":null}", PrimitivesBean.class);
      fail("Expected failure for long + null");
    } catch (JsonMappingException e) {
      verifyException(e, "Can not map JSON null into type long");
    }

    // float/double
    try {
      mapper2.readValue("{\"floatValue\":null}", PrimitivesBean.class);
      fail("Expected failure for float + null");
    } catch (JsonMappingException e) {
      verifyException(e, "Can not map JSON null into type float");
    }
    try {
      mapper2.readValue("{\"doubleValue\":null}", PrimitivesBean.class);
      fail("Expected failure for double + null");
    } catch (JsonMappingException e) {
      verifyException(e, "Can not map JSON null into type double");
    }
  }