// [JACKSON-212]
 public void testToStringEnumWithEnumMap() throws Exception {
   ObjectMapper m = new ObjectMapper();
   m.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
   EnumMap<LowerCaseEnum, String> enums = new EnumMap<LowerCaseEnum, String>(LowerCaseEnum.class);
   enums.put(LowerCaseEnum.C, "value");
   assertEquals("{\"c\":\"value\"}", m.writeValueAsString(enums));
 }
 public void testGlobalVisibilityForGetters() {
   ObjectMapper m = new ObjectMapper();
   m.configure(MapperFeature.AUTO_DETECT_GETTERS, false);
   POJOPropertiesCollector coll = collector(m, SimpleGetterVisibility.class, true);
   // should be 1, expect that we disabled getter auto-detection, so
   Map<String, POJOPropertyBuilder> props = coll.getPropertyMap();
   assertEquals(0, props.size());
 }
  // 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());
  }
示例#4
0
	/**
	 * Initialises the database.
	 */
	static {
		try {
			ObjectMapper om = new ObjectMapper();
			om.configure( JsonParser.Feature.ALLOW_COMMENTS, true );

			String jsonText = Resources.loadResourceAsString( DATABASE_NAME ); // workaround for bug #779, see http://jira.codehaus.org/browse/JACKSON-779
			database = om.readTree( jsonText );
		} catch ( Exception e ) {
			throw new ExceptionInInitializerError( e );
		}
	}
  // [JACKSON-212]
  public void testToStringEnum() throws Exception {
    ObjectMapper m = new ObjectMapper();
    m.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
    assertEquals("\"b\"", m.writeValueAsString(LowerCaseEnum.B));

    // [databind#749] but should also be able to dynamically disable
    assertEquals(
        "\"B\"",
        m.writer()
            .without(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
            .writeValueAsString(LowerCaseEnum.B));
  }
  @SuppressWarnings("hiding")
  public void testJackson703() throws Exception {
    // note: need a separate mapper, need to reconfigure
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(MapperFeature.USE_ANNOTATIONS, false);
    BasicBeanDescription beanDesc =
        mapper.getSerializationConfig().introspect(mapper.constructType(Jackson703.class));
    assertNotNull(beanDesc);

    Jackson703 bean = new Jackson703();
    String json = mapper.writeValueAsString(bean);
    assertNotNull(json);
  }
 // [JACKSON-620]: allow "" to mean 'null' for Maps
 public void testFromEmptyString() throws Exception {
   ObjectMapper m = new ObjectMapper();
   m.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
   Map<?, ?> result = m.readValue(quote(""), Map.class);
   assertNull(result);
 }
  /** 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");
    }
  }