Search in sources :

Example 1 with JSONWriter

use of org.grails.web.json.JSONWriter in project grails-core by grails.

the class DomainClassMarshaller method asShortObject.

protected void asShortObject(Object refObj, JSON json, GrailsDomainClassProperty idProperty, GrailsDomainClass referencedDomainClass) throws ConverterException {
    Object idValue;
    if (proxyHandler instanceof EntityProxyHandler) {
        idValue = ((EntityProxyHandler) proxyHandler).getProxyIdentifier(refObj);
        if (idValue == null) {
            idValue = extractValue(refObj, idProperty);
        }
    } else {
        idValue = extractValue(refObj, idProperty);
    }
    JSONWriter writer = json.getWriter();
    writer.object();
    if (isIncludeClass()) {
        writer.key("class").value(referencedDomainClass.getFullName());
    }
    if (idValue != null) {
        writer.key("id").value(idValue);
    }
    writer.endObject();
}
Also used : JSONWriter(org.grails.web.json.JSONWriter) EntityProxyHandler(grails.core.support.proxy.EntityProxyHandler) GroovyObject(groovy.lang.GroovyObject)

Example 2 with JSONWriter

use of org.grails.web.json.JSONWriter in project grails-core by grails.

the class GenericJavaBeanMarshaller method marshalObject.

public void marshalObject(Object o, JSON json) throws ConverterException {
    JSONWriter writer = json.getWriter();
    Class<? extends Object> clazz = o.getClass();
    List<String> excludes = json.getExcludes(clazz);
    List<String> includes = json.getIncludes(clazz);
    IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>();
    try {
        writer.object();
        for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(o.getClass())) {
            String name = property.getName();
            Method readMethod = property.getReadMethod();
            if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name))
                continue;
            if (readMethod != null && !(name.equals("metaClass")) && !(name.equals("class"))) {
                if (readMethod.getAnnotation(PersistenceMethod.class) != null)
                    continue;
                if (readMethod.getAnnotation(ControllerMethod.class) != null)
                    continue;
                Object value = readMethod.invoke(o, (Object[]) null);
                writer.key(name);
                json.convertAnother(value);
            }
        }
        for (Field field : o.getClass().getDeclaredFields()) {
            int modifiers = field.getModifiers();
            if (field.isAccessible() && Modifier.isPublic(modifiers) && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) {
                String name = field.getName();
                if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name))
                    continue;
                writer.key(field.getName());
                json.convertAnother(field.get(o));
            }
        }
        writer.endObject();
    } catch (ConverterException ce) {
        throw ce;
    } catch (Exception e) {
        throw new ConverterException("Error converting Bean with class " + o.getClass().getName(), e);
    }
}
Also used : JSONWriter(org.grails.web.json.JSONWriter) ConverterException(org.grails.web.converters.exceptions.ConverterException) PropertyDescriptor(java.beans.PropertyDescriptor) ControllerMethod(grails.web.controllers.ControllerMethod) PersistenceMethod(grails.persistence.PersistenceMethod) Method(java.lang.reflect.Method) IncludeExcludeSupport(org.grails.core.util.IncludeExcludeSupport) ConverterException(org.grails.web.converters.exceptions.ConverterException) Field(java.lang.reflect.Field) PersistenceMethod(grails.persistence.PersistenceMethod) ControllerMethod(grails.web.controllers.ControllerMethod)

Example 3 with JSONWriter

use of org.grails.web.json.JSONWriter in project grails-core by grails.

the class MapMarshaller method marshalObject.

public void marshalObject(Object o, JSON converter) throws ConverterException {
    JSONWriter writer = converter.getWriter();
    writer.object();
    Map<Object, Object> map = (Map<Object, Object>) o;
    for (Map.Entry<Object, Object> entry : map.entrySet()) {
        Object key = entry.getKey();
        if (key != null) {
            writer.key(key.toString());
            converter.convertAnother(entry.getValue());
        }
    }
    writer.endObject();
}
Also used : JSONWriter(org.grails.web.json.JSONWriter) Map(java.util.Map)

Example 4 with JSONWriter

use of org.grails.web.json.JSONWriter in project grails-core by grails.

the class ArrayMarshaller method marshalObject.

public void marshalObject(Object o, JSON converter) throws ConverterException {
    JSONWriter writer = converter.getWriter();
    int len = Array.getLength(o);
    writer.array();
    for (int i = 0; i < len; i++) {
        converter.convertAnother(Array.get(o, i));
    }
    writer.endArray();
}
Also used : JSONWriter(org.grails.web.json.JSONWriter)

Example 5 with JSONWriter

use of org.grails.web.json.JSONWriter in project grails-core by grails.

the class CollectionMarshaller method marshalObject.

public void marshalObject(Object o, JSON converter) throws ConverterException {
    JSONWriter writer = converter.getWriter();
    writer.array();
    for (Object o1 : ((Collection) o)) {
        converter.convertAnother(o1);
    }
    writer.endArray();
}
Also used : JSONWriter(org.grails.web.json.JSONWriter)

Aggregations

JSONWriter (org.grails.web.json.JSONWriter)9 ConverterException (org.grails.web.converters.exceptions.ConverterException)4 GroovyObject (groovy.lang.GroovyObject)3 Method (java.lang.reflect.Method)3 IncludeExcludeSupport (org.grails.core.util.IncludeExcludeSupport)3 PersistenceMethod (grails.persistence.PersistenceMethod)2 ControllerMethod (grails.web.controllers.ControllerMethod)2 PropertyDescriptor (java.beans.PropertyDescriptor)2 Field (java.lang.reflect.Field)2 GrailsDomainClass (grails.core.GrailsDomainClass)1 GrailsDomainClassProperty (grails.core.GrailsDomainClassProperty)1 EntityProxyHandler (grails.core.support.proxy.EntityProxyHandler)1 Locale (java.util.Locale)1 Map (java.util.Map)1 BeanWrapper (org.springframework.beans.BeanWrapper)1 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)1 Errors (org.springframework.validation.Errors)1 FieldError (org.springframework.validation.FieldError)1 ObjectError (org.springframework.validation.ObjectError)1