Search in sources :

Example 1 with JSON

use of grails.converters.JSON in project grails-core by grails.

the class ConvertersConfigurationInitializer method initJSONConfiguration.

private void initJSONConfiguration() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Initializing default JSON Converters Configuration...");
    }
    List<ObjectMarshaller<JSON>> marshallers = new ArrayList<ObjectMarshaller<JSON>>();
    marshallers.addAll(getPreviouslyConfiguredMarshallers(JSON.class));
    marshallers.add(new org.grails.web.converters.marshaller.json.ArrayMarshaller());
    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<JSON>());
    Config grailsConfig = getGrailsConfig();
    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.");
        }
        marshallers.add(new org.grails.web.converters.marshaller.json.JavascriptDateMarshaller());
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Using default JSON Date Marshaller");
        }
        marshallers.add(new org.grails.web.converters.marshaller.json.DateMarshaller());
    }
    marshallers.add(new org.grails.web.converters.marshaller.json.ToStringBeanMarshaller());
    boolean includeDomainVersion = includeDomainVersionProperty(grailsConfig, "json");
    boolean includeDomainClassName = includeDomainClassProperty(grailsConfig, "json");
    ProxyHandler proxyHandler = getProxyHandler();
    if (grailsConfig.getProperty(SETTING_CONVERTERS_JSON_DEFAULT_DEEP, Boolean.class, false)) {
        LOG.debug("Using DeepDomainClassMarshaller as default.");
        marshallers.add(new org.grails.web.converters.marshaller.json.DeepDomainClassMarshaller(includeDomainVersion, includeDomainClassName, proxyHandler, grailsApplication));
    } else {
        marshallers.add(new org.grails.web.converters.marshaller.json.DomainClassMarshaller(includeDomainVersion, includeDomainClassName, proxyHandler, grailsApplication));
    }
    marshallers.add(new org.grails.web.converters.marshaller.json.GroovyBeanMarshaller());
    marshallers.add(new org.grails.web.converters.marshaller.json.GenericJavaBeanMarshaller());
    DefaultConverterConfiguration<JSON> cfg = new DefaultConverterConfiguration<JSON>(marshallers, proxyHandler);
    cfg.setEncoding(grailsConfig.getProperty(SETTING_CONVERTERS_ENCODING, "UTF-8"));
    String defaultCirRefBehaviour = grailsConfig.getProperty(SETTING_CONVERTERS_CIRCULAR_REFERENCE_BEHAVIOUR, "DEFAULT");
    cfg.setCircularReferenceBehaviour(Converter.CircularReferenceBehaviour.valueOf(grailsConfig.getProperty(SETTING_CONVERTERS_JSON_CIRCULAR_REFERENCE_BEHAVIOUR, String.class, defaultCirRefBehaviour, Converter.CircularReferenceBehaviour.allowedValues())));
    Boolean defaultPrettyPrint = grailsConfig.getProperty(SETTING_CONVERTERS_PRETTY_PRINT, Boolean.class, false);
    Boolean prettyPrint = grailsConfig.getProperty(SETTING_CONVERTERS_JSON_PRETTY_PRINT, Boolean.class, defaultPrettyPrint);
    cfg.setPrettyPrint(prettyPrint);
    cfg.setCacheObjectMarshallerByClass(grailsConfig.getProperty(SETTING_CONVERTERS_JSON_CACHE_OBJECTS, Boolean.class, true));
    registerObjectMarshallersFromApplicationContext(cfg, JSON.class);
    ConvertersConfigurationHolder.setDefaultConfiguration(JSON.class, new ChainedConverterConfiguration<JSON>(cfg, proxyHandler));
}
Also used : Config(grails.config.Config) PropertySourcesConfig(org.grails.config.PropertySourcesConfig) ArrayList(java.util.ArrayList) JSON(grails.converters.JSON) DefaultProxyHandler(grails.core.support.proxy.DefaultProxyHandler) ProxyHandler(grails.core.support.proxy.ProxyHandler) ObjectMarshaller(org.grails.web.converters.marshaller.ObjectMarshaller)

Aggregations

Config (grails.config.Config)1 JSON (grails.converters.JSON)1 DefaultProxyHandler (grails.core.support.proxy.DefaultProxyHandler)1 ProxyHandler (grails.core.support.proxy.ProxyHandler)1 ArrayList (java.util.ArrayList)1 PropertySourcesConfig (org.grails.config.PropertySourcesConfig)1 ObjectMarshaller (org.grails.web.converters.marshaller.ObjectMarshaller)1