Skip to content

Commit

Permalink
Add unit tests for #749; failure to dynamically use WRITE_ENUMS_USING…
Browse files Browse the repository at this point in the history
…_TO_STRING
  • Loading branch information
cowtowncoder committed Apr 8, 2015
1 parent 119ddc9 commit cbda007
Showing 1 changed file with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected static interface ToStringMixin {
@JsonValue public String toString();
}

protected enum SerializableEnum implements JsonSerializable
protected static enum SerializableEnum implements JsonSerializable
{
A, B, C;

Expand All @@ -86,7 +86,7 @@ public void serialize(JsonGenerator jgen, SerializerProvider provider) throws IO
}
}

protected enum LowerCaseEnum {
protected static enum LowerCaseEnum {
A, B, C;
private LowerCaseEnum() { }
@Override
Expand Down Expand Up @@ -282,6 +282,11 @@ 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));
}

// [JACKSON-212]
Expand Down Expand Up @@ -371,6 +376,31 @@ public void testCustomEnumMapKeySerializer() throws Exception {
String json = mapper.writeValueAsString(new MyBean661("abc"));
assertEquals(aposToQuotes("{'X-FOO':'abc'}"), json);
}

// [databind#749]

public void testEnumMapSerDefault() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
EnumMap<LowerCaseEnum, String> m = new EnumMap<LowerCaseEnum, String>(LowerCaseEnum.class);
m.put(LowerCaseEnum.A, "value");
assertEquals("{\"A\":\"value\"}", mapper.writeValueAsString(m));
}

public void testEnumMapSerDisableToString() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
ObjectWriter w = mapper.writer().without(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
EnumMap<LowerCaseEnum, String> m = new EnumMap<LowerCaseEnum, String>(LowerCaseEnum.class);
m.put(LowerCaseEnum.A, "value");
assertEquals("{\"A\":\"value\"}", w.writeValueAsString(m));
}

public void testEnumMapSerEnableToString() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
ObjectWriter w = mapper.writer().with(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
EnumMap<LowerCaseEnum, String> m = new EnumMap<LowerCaseEnum, String>(LowerCaseEnum.class);
m.put(LowerCaseEnum.A, "value");
assertEquals("{\"a\":\"value\"}", w.writeValueAsString(m));
}
}

// [JACKSON-757], non-inner enum
Expand Down

0 comments on commit cbda007

Please sign in to comment.