Skip to content

Commit c899909

Browse files
committed
Fix Enum JSON/XML Serialization for Round-Trip Compatibility
1 parent 9ca377b commit c899909

File tree

6 files changed

+15
-126
lines changed

6 files changed

+15
-126
lines changed

grails-converters/src/main/groovy/grails/converters/JSON.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ else if (o instanceof CharSequence) {
185185
else if (o instanceof Class<?>) {
186186
writer.value(((Class<?>) o).getName());
187187
}
188+
else if (o instanceof Enum) {
189+
writer.value(((Enum<?>) o).name());
190+
}
188191
else if (o instanceof Number) {
189192
writer.value((Number) o);
190193
} else if (o instanceof Boolean) {

grails-converters/src/main/groovy/grails/converters/XML.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ public void convertAnother(Object o) throws ConverterException {
171171
else if (o instanceof Class<?>) {
172172
writer.characters(((Class<?>) o).getName());
173173
}
174+
else if (o instanceof Enum) {
175+
writer.characters(((Enum<?>) o).name());
176+
}
174177
else if ((o.getClass().isPrimitive() && !o.getClass().equals(byte[].class)) ||
175178
o instanceof Number || o instanceof Boolean) {
176179
writer.characters(String.valueOf(o));

grails-converters/src/main/groovy/org/grails/web/converters/configuration/ConvertersConfigurationInitializer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ private void initJSONConfiguration() {
9595
marshallers.add(new org.grails.web.converters.marshaller.json.ByteArrayMarshaller());
9696
marshallers.add(new org.grails.web.converters.marshaller.json.CollectionMarshaller());
9797
marshallers.add(new org.grails.web.converters.marshaller.json.MapMarshaller());
98-
marshallers.add(new org.grails.web.converters.marshaller.json.EnumMarshaller());
9998
marshallers.add(new org.grails.web.converters.marshaller.ProxyUnwrappingMarshaller<>());
10099

101100
Config grailsConfig = getGrailsConfig();
@@ -177,7 +176,6 @@ private void initXMLConfiguration() {
177176
marshallers.add(new org.grails.web.converters.marshaller.xml.ArrayMarshaller());
178177
marshallers.add(new org.grails.web.converters.marshaller.xml.CollectionMarshaller());
179178
marshallers.add(new org.grails.web.converters.marshaller.xml.MapMarshaller());
180-
marshallers.add(new org.grails.web.converters.marshaller.xml.EnumMarshaller());
181179
marshallers.add(new org.grails.web.converters.marshaller.xml.DateMarshaller());
182180
marshallers.add(new ProxyUnwrappingMarshaller<>());
183181
marshallers.add(new org.grails.web.converters.marshaller.xml.ToStringBeanMarshaller());

grails-converters/src/main/groovy/org/grails/web/converters/marshaller/json/EnumMarshaller.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

grails-converters/src/main/groovy/org/grails/web/converters/marshaller/xml/EnumMarshaller.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

grails-test-suite-web/src/test/groovy/org/grails/web/converters/JSONConverterTests.groovy

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,14 @@ class JSONConverterTests extends Specification implements ControllerUnitTest<JSO
9494
when:
9595
def enumInstance = Role.HEAD
9696
params.e = enumInstance
97-
controller.testEnum()
97+
controller.testEnumInMap()
98+
def jsonString = response.contentAsString
9899
def json = response.json
99100

100101
then:
101-
json.enumType == "org.grails.web.converters.Role"
102-
json.name == "HEAD"
103-
json.size() == 2
102+
json.size() == 1
103+
jsonString == '{"value":"HEAD"}'
104+
json.value == "HEAD"
104105
}
105106

106107
// GRAILS-11513
@@ -196,6 +197,10 @@ class JSONConverterController {
196197
render params.e as JSON
197198
}
198199

200+
def testEnumInMap = {
201+
render([value: params.e] as JSON)
202+
}
203+
199204
def testNullValues = {
200205
def descriptors = [:]
201206
descriptors.put(null,null)

0 commit comments

Comments
 (0)