// [JACKSON-684] public void testAsIndex() throws Exception { // By default, serialize using name ObjectMapper m = new ObjectMapper(); assertFalse(m.isEnabled(SerializationFeature.WRITE_ENUMS_USING_INDEX)); assertEquals(quote("B"), m.writeValueAsString(TestEnum.B)); // but we can change (dynamically, too!) it to be number-based m.enable(SerializationFeature.WRITE_ENUMS_USING_INDEX); assertEquals("1", m.writeValueAsString(TestEnum.B)); }
// [databind#601] public void testEnumsWithJsonValueInMap() throws Exception { EnumMap<EnumWithJsonValue, String> input = new EnumMap<EnumWithJsonValue, String>(EnumWithJsonValue.class); input.put(EnumWithJsonValue.B, "x"); assertEquals( "{\"" + EnumWithJsonValue.B.toString() + "\":\"x\"}", mapper.writeValueAsString(input)); }
// [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)); }
// [Issue#227] public void testGenericEnumSerializer() throws Exception { // By default, serialize using name ObjectMapper m = new ObjectMapper(); SimpleModule module = new SimpleModule("foobar"); module.addSerializer(Enum.class, new LowerCasingEnumSerializer()); m.registerModule(module); assertEquals(quote("b"), m.writeValueAsString(TestEnum.B)); }
// [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)); }
/** * Simple test to verify that byte[] values can be handled properly when converting, as long as * there is metadata (from POJO definitions). */ public void testIssue709() throws Exception { byte[] inputData = new byte[] {1, 2, 3}; ObjectNode node = MAPPER.createObjectNode(); node.put("data", inputData); Issue709Bean result = MAPPER.treeToValue(node, Issue709Bean.class); String json = MAPPER.writeValueAsString(node); Issue709Bean resultFromString = MAPPER.readValue(json, Issue709Bean.class); Issue709Bean resultFromConvert = MAPPER.convertValue(node, Issue709Bean.class); // all methods should work equally well: Assert.assertArrayEquals(inputData, resultFromString.data); Assert.assertArrayEquals(inputData, resultFromConvert.data); Assert.assertArrayEquals(inputData, result.data); }
@Override public void serialize(OutputStream out, Record record) throws RecordSerializationException { ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addSerializer(StandardRecord.class, new EventSerializer()); mapper.registerModule(module); // map json to student try { mapper.enable(SerializationFeature.INDENT_OUTPUT); mapper.setPropertyNamingStrategy( PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); String jsonString = mapper.writeValueAsString(record); out.write(jsonString.getBytes()); out.flush(); } catch (IOException e) { e.printStackTrace(); } }
// [databind#661] public void testCustomEnumMapKeySerializer() throws Exception { String json = mapper.writeValueAsString(new MyBean661("abc")); assertEquals(aposToQuotes("{'X-FOO':'abc'}"), json); }
// [databind#594] public void testJsonValueForEnumMapKey() throws Exception { assertEquals( aposToQuotes("{'stuff':{'longValue':'foo'}}"), mapper.writeValueAsString(new MyStuff594("foo"))); }
public void testOverrideEnumAsNumber() throws Exception { assertEquals("{\"value\":1}", mapper.writeValueAsString(new PoOverrideAsNumber())); }
public void testEnumsWithJsonValue() throws Exception { assertEquals("\"bar\"", mapper.writeValueAsString(EnumWithJsonValue.B)); }
// As of 2.5, use of Shape.ARRAY is legal alias for "write as number" public void testEnumAsObjectBroken() throws Exception { assertEquals("0", mapper.writeValueAsString(PoAsArray.A)); }
public void testEnumAsIndexViaAnnotations() throws Exception { assertEquals("{\"text\":0}", mapper.writeValueAsString(new PoNUMContainer())); }
public void testEnumAsObjectValid() throws Exception { assertEquals("{\"value\":\"a1\"}", mapper.writeValueAsString(PoNUM.A)); }
// Test [JACKSON-214] public void testSubclassedEnums() throws Exception { assertEquals("\"B\"", mapper.writeValueAsString(EnumWithSubClass.B)); }
// [JACKSON-576] public void testMapWithEnumKeys() throws Exception { MapBean bean = new MapBean(); bean.add(TestEnum.B, 3); String json = mapper.writeValueAsString(bean); assertEquals("{\"map\":{\"b\":3}}", json); }
public void testEnumsWithJsonValueUsingMixin() throws Exception { // can't share, as new mix-ins are added ObjectMapper m = new ObjectMapper(); m.addMixIn(TestEnum.class, ToStringMixin.class); assertEquals("\"b\"", m.writeValueAsString(TestEnum.B)); }
/** * Test for ensuring that @JsonSerializable is used with Enum types as well as with any other * types. */ public void testSerializableEnum() throws Exception { assertEquals("\"foo\"", mapper.writeValueAsString(SerializableEnum.A)); }
// [JACKSON-757] public void testAnnotationsOnEnumCtor() throws Exception { assertEquals(quote("V1"), mapper.writeValueAsString(OK.V1)); assertEquals(quote("V1"), mapper.writeValueAsString(NOT_OK.V1)); assertEquals(quote("V2"), mapper.writeValueAsString(NOT_OK2.V2)); }
public void testSimple() throws Exception { assertEquals("\"B\"", mapper.writeValueAsString(TestEnum.B)); }