Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,19 @@ private void initJSONConfiguration() {
marshallers.add(new org.grails.web.converters.marshaller.json.ByteArrayMarshaller());
marshallers.add(new org.grails.web.converters.marshaller.json.CollectionMarshaller());
marshallers.add(new org.grails.web.converters.marshaller.json.MapMarshaller());
marshallers.add(new org.grails.web.converters.marshaller.json.EnumMarshaller());
marshallers.add(new org.grails.web.converters.marshaller.ProxyUnwrappingMarshaller<>());

Config grailsConfig = getGrailsConfig();

// Register enum marshaller - defaults to legacy for backward compatibility (will change in Grails 8.0)
String jsonEnumFormat = grailsConfig.getProperty("grails.converters.json.enum.format", String.class, "default");
if ("simple".equals(jsonEnumFormat)) {
marshallers.add(new org.grails.web.converters.marshaller.json.SimpleEnumMarshaller());
} else {
marshallers.add(new org.grails.web.converters.marshaller.json.EnumMarshaller());
}

marshallers.add(new org.grails.web.converters.marshaller.ProxyUnwrappingMarshaller<>());

if ("javascript".equals(grailsConfig.getProperty(SETTING_CONVERTERS_JSON_DATE, String.class, "default", Arrays.asList("javascript", "default")))) {
if (LOG.isDebugEnabled()) {
LOG.debug("Using Javascript JSON Date Marshaller.");
Expand Down Expand Up @@ -177,14 +185,22 @@ private void initXMLConfiguration() {
marshallers.add(new org.grails.web.converters.marshaller.xml.ArrayMarshaller());
marshallers.add(new org.grails.web.converters.marshaller.xml.CollectionMarshaller());
marshallers.add(new org.grails.web.converters.marshaller.xml.MapMarshaller());
marshallers.add(new org.grails.web.converters.marshaller.xml.EnumMarshaller());

Config grailsConfig = getGrailsConfig();

// Register enum marshaller - defaults to legacy for backward compatibility (will change in Grails 8.0)
String xmlEnumFormat = grailsConfig.getProperty("grails.converters.xml.enum.format", String.class, "default");
if ("simple".equals(xmlEnumFormat)) {
marshallers.add(new org.grails.web.converters.marshaller.xml.SimpleEnumMarshaller());
} else {
marshallers.add(new org.grails.web.converters.marshaller.xml.EnumMarshaller());
}

marshallers.add(new org.grails.web.converters.marshaller.xml.DateMarshaller());
marshallers.add(new ProxyUnwrappingMarshaller<>());
marshallers.add(new org.grails.web.converters.marshaller.xml.ToStringBeanMarshaller());
ProxyHandler proxyHandler = getProxyHandler();

Config grailsConfig = getGrailsConfig();

boolean includeDomainVersion = includeDomainVersionProperty(grailsConfig, "xml");
if (grailsConfig.getProperty(SETTING_CONVERTERS_XML_DEEP, Boolean.class, false)) {
marshallers.add(new org.grails.web.converters.marshaller.xml.DeepDomainClassMarshaller(includeDomainVersion, proxyHandler, grailsApplication));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
/**
* @author Siegfried Puchbauer
* @since 1.1
* @deprecated As of 7.0.2, replaced by {@link SimpleEnumMarshaller} for round-trip compatibility.
* This marshaller will no longer be registered by default in Grails 8.0.
* To opt-in to the new behavior now, set {@code grails.converters.json.enum.format=simple} in application.yml.
*/
@Deprecated(forRemoval = true, since = "7.0.2")
public class EnumMarshaller implements ObjectMarshaller<JSON> {

public boolean supports(Object object) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.grails.web.converters.marshaller.json;

import java.lang.reflect.Method;

import org.springframework.beans.BeanUtils;

import grails.converters.JSON;
import org.grails.web.converters.exceptions.ConverterException;
import org.grails.web.converters.marshaller.ObjectMarshaller;

/**
* Marshals enums as simple string values (just the enum name) for symmetric serialization/deserialization.
* This provides round-trip compatibility where POSTing JSON returns the same format when GETting.
*
* @since 7.0.2
*/
public class SimpleEnumMarshaller implements ObjectMarshaller<JSON> {

public boolean supports(Object object) {
return object.getClass().isEnum();
}

public void marshalObject(Object en, JSON json) throws ConverterException {
try {
Method nameMethod = BeanUtils.findDeclaredMethod(en.getClass(), "name");
try {
json.convertAnother(nameMethod.invoke(en));
}
catch (Exception e) {
json.convertAnother("");
}
}
catch (ConverterException ce) {
throw ce;
}
catch (Exception e) {
throw new ConverterException("Error converting Enum with class " + en.getClass().getName(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
/**
* @author Siegfried Puchbauer
* @since 1.1
* @deprecated As of 7.0.2, replaced by {@link SimpleEnumMarshaller} for round-trip compatibility.
* This marshaller will no longer be registered by default in Grails 8.0.
* To opt-in to the new behavior now, set {@code grails.converters.xml.enum.format=simple} in application.yml.
*/
@Deprecated(forRemoval = true, since = "7.0.2")
public class EnumMarshaller implements ObjectMarshaller<XML> {

public boolean supports(Object object) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.grails.web.converters.marshaller.xml;

import java.lang.reflect.Method;

import org.springframework.beans.BeanUtils;

import grails.converters.XML;
import org.grails.web.converters.exceptions.ConverterException;
import org.grails.web.converters.marshaller.ObjectMarshaller;

/**
* Marshals enums as simple string values (just the enum name) for symmetric serialization/deserialization.
* This provides round-trip compatibility where POSTing XML returns the same format when GETting.
*
* @since 7.0.2
*/
public class SimpleEnumMarshaller implements ObjectMarshaller<XML> {

public boolean supports(Object object) {
return object.getClass().isEnum();
}

public void marshalObject(Object en, XML xml) throws ConverterException {
try {
Method nameMethod = BeanUtils.findDeclaredMethod(en.getClass(), "name");
try {
xml.chars(nameMethod.invoke(en).toString());
}
catch (Exception e) {
// ignored
}
}
catch (ConverterException ce) {
throw ce;
}
catch (Exception e) {
throw new ConverterException("Error converting Enum with class " + en.getClass().getName(), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ class JSONConverterTests extends Specification implements ControllerUnitTest<JSO
json.size() == 2
}

void testJSONEnumConvertingWithSimpleMarshaller() {
given:
JSON.createNamedConfig('simple') {
it.registerObjectMarshaller(new org.grails.web.converters.marshaller.json.SimpleEnumMarshaller())
}
JSON.use('simple')

when:
def enumInstance = Role.HEAD
params.e = enumInstance
controller.testEnumInMap()
def jsonString = response.contentAsString
def json = response.json

then:
json.size() == 1
jsonString == '{"value":"HEAD"}'
json.value == "HEAD"
}

// GRAILS-11513
void testStringsWithQuotes() {
when:
Expand Down Expand Up @@ -196,6 +216,10 @@ class JSONConverterController {
render params.e as JSON
}

def testEnumInMap = {
render([value: params.e] as JSON)
}

def testNullValues = {
def descriptors = [:]
descriptors.put(null,null)
Expand Down
Loading