Skip to content

Commit ef17f26

Browse files
committed
Add failing test for #1543
1 parent 13836d7 commit ef17f26

File tree

2 files changed

+113
-65
lines changed

2 files changed

+113
-65
lines changed

src/test/java/com/fasterxml/jackson/databind/ser/TestEnumSerialization.java

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.util.*;
55

66
import com.fasterxml.jackson.annotation.*;
7-
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
87

98
import com.fasterxml.jackson.core.*;
109
import com.fasterxml.jackson.databind.*;
@@ -114,32 +113,6 @@ static enum OK {
114113
protected String key;
115114
OK(String key) { this.key = key; }
116115
}
117-
118-
// Types for [https://github.com/FasterXML/jackson-databind/issues/24]
119-
// (Enums as JSON Objects)
120-
121-
@JsonFormat(shape=JsonFormat.Shape.OBJECT)
122-
static enum PoNUM {
123-
A("a1"), B("b2");
124-
125-
@JsonProperty
126-
protected final String value;
127-
128-
private PoNUM(String v) { value = v; }
129-
130-
public String getValue() { return value; }
131-
}
132-
133-
static class PoNUMContainer {
134-
@JsonFormat(shape=Shape.NUMBER)
135-
public OK text = OK.V1;
136-
}
137-
138-
@JsonFormat(shape=JsonFormat.Shape.ARRAY) // alias for 'number', as of 2.5
139-
static enum PoAsArray
140-
{
141-
A, B;
142-
}
143116

144117
@SuppressWarnings({ "rawtypes", "serial" })
145118
static class LowerCasingEnumSerializer extends StdSerializer<Enum>
@@ -152,19 +125,6 @@ public void serialize(Enum value, JsonGenerator jgen,
152125
}
153126
}
154127

155-
// for [databind#572]
156-
static class PoOverrideAsString
157-
{
158-
@JsonFormat(shape=Shape.STRING)
159-
public PoNUM value = PoNUM.B;
160-
}
161-
162-
static class PoOverrideAsNumber
163-
{
164-
@JsonFormat(shape=Shape.NUMBER)
165-
public PoNUM value = PoNUM.B;
166-
}
167-
168128
static enum MyEnum594 {
169129
VALUE_WITH_A_REALLY_LONG_NAME_HERE("longValue");
170130

@@ -348,22 +308,6 @@ public void testAnnotationsOnEnumCtor() throws Exception
348308
assertEquals(quote("V2"), MAPPER.writeValueAsString(NOT_OK2.V2));
349309
}
350310

351-
// Tests for [issue#24]
352-
353-
public void testEnumAsObjectValid() throws Exception {
354-
assertEquals("{\"value\":\"a1\"}", MAPPER.writeValueAsString(PoNUM.A));
355-
}
356-
357-
public void testEnumAsIndexViaAnnotations() throws Exception {
358-
assertEquals("{\"text\":0}", MAPPER.writeValueAsString(new PoNUMContainer()));
359-
}
360-
361-
// As of 2.5, use of Shape.ARRAY is legal alias for "write as number"
362-
public void testEnumAsObjectBroken() throws Exception
363-
{
364-
assertEquals("0", MAPPER.writeValueAsString(PoAsArray.A));
365-
}
366-
367311
// [Issue#227]
368312
public void testGenericEnumSerializer() throws Exception
369313
{
@@ -375,15 +319,6 @@ public void testGenericEnumSerializer() throws Exception
375319
assertEquals(quote("b"), m.writeValueAsString(TestEnum.B));
376320
}
377321

378-
// [databind#572]
379-
public void testOverrideEnumAsString() throws Exception {
380-
assertEquals("{\"value\":\"B\"}", MAPPER.writeValueAsString(new PoOverrideAsString()));
381-
}
382-
383-
public void testOverrideEnumAsNumber() throws Exception {
384-
assertEquals("{\"value\":1}", MAPPER.writeValueAsString(new PoOverrideAsNumber()));
385-
}
386-
387322
// [databind#594]
388323
public void testJsonValueForEnumMapKey() throws Exception {
389324
assertEquals(aposToQuotes("{'stuff':{'longValue':'foo'}}"),
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.fasterxml.jackson.databind.struct;
2+
3+
import com.fasterxml.jackson.annotation.*;
4+
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
5+
6+
import com.fasterxml.jackson.databind.*;
7+
8+
/**
9+
* Unit tests for verifying serialization of simple basic non-structured
10+
* types; primitives (and/or their wrappers), Strings.
11+
*/
12+
public class EnumFormatShapeTest
13+
extends BaseMapTest
14+
{
15+
@JsonFormat(shape=JsonFormat.Shape.OBJECT)
16+
static enum PoNUM {
17+
A("a1"), B("b2");
18+
19+
@JsonProperty
20+
protected final String value;
21+
22+
private PoNUM(String v) { value = v; }
23+
24+
public String getValue() { return value; }
25+
}
26+
27+
static enum OK {
28+
V1("v1");
29+
protected String key;
30+
OK(String key) { this.key = key; }
31+
}
32+
33+
static class PoNUMContainer {
34+
@JsonFormat(shape=Shape.NUMBER)
35+
public OK text = OK.V1;
36+
}
37+
38+
@JsonFormat(shape=JsonFormat.Shape.ARRAY) // alias for 'number', as of 2.5
39+
static enum PoAsArray
40+
{
41+
A, B;
42+
}
43+
44+
// for [databind#572]
45+
static class PoOverrideAsString
46+
{
47+
@JsonFormat(shape=Shape.STRING)
48+
public PoNUM value = PoNUM.B;
49+
}
50+
51+
static class PoOverrideAsNumber
52+
{
53+
@JsonFormat(shape=Shape.NUMBER)
54+
public PoNUM value = PoNUM.B;
55+
}
56+
57+
// for [databind#1543]
58+
@JsonFormat(shape = JsonFormat.Shape.NUMBER_INT)
59+
enum Color {
60+
RED,
61+
YELLOW,
62+
GREEN
63+
}
64+
65+
static class ColorWrapper {
66+
public final Color color;
67+
68+
ColorWrapper(Color color) {
69+
this.color = color;
70+
}
71+
}
72+
73+
/*
74+
/**********************************************************
75+
/* Tests
76+
/**********************************************************
77+
*/
78+
79+
private final ObjectMapper MAPPER = new ObjectMapper();
80+
81+
// Tests for JsonFormat.shape
82+
83+
public void testEnumAsObjectValid() throws Exception {
84+
assertEquals("{\"value\":\"a1\"}", MAPPER.writeValueAsString(PoNUM.A));
85+
}
86+
87+
public void testEnumAsIndexViaAnnotations() throws Exception {
88+
assertEquals("{\"text\":0}", MAPPER.writeValueAsString(new PoNUMContainer()));
89+
}
90+
91+
// As of 2.5, use of Shape.ARRAY is legal alias for "write as number"
92+
public void testEnumAsObjectBroken() throws Exception
93+
{
94+
assertEquals("0", MAPPER.writeValueAsString(PoAsArray.A));
95+
}
96+
97+
// [databind#572]
98+
public void testOverrideEnumAsString() throws Exception {
99+
assertEquals("{\"value\":\"B\"}", MAPPER.writeValueAsString(new PoOverrideAsString()));
100+
}
101+
102+
public void testOverrideEnumAsNumber() throws Exception {
103+
assertEquals("{\"value\":1}", MAPPER.writeValueAsString(new PoOverrideAsNumber()));
104+
}
105+
106+
// for [databind#1543]
107+
public void testEnumAsNumber() throws Exception {
108+
assertEquals(String.valueOf(Color.GREEN.ordinal()),
109+
MAPPER.writeValueAsString(Color.GREEN));
110+
assertEquals(String.format(aposToQuotes("{'color':'%s'}"), Color.GREEN.ordinal()),
111+
MAPPER.writeValueAsString(new ColorWrapper(Color.GREEN)));
112+
}
113+
}

0 commit comments

Comments
 (0)