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();
}
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);
}
}
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();
}
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();
}
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();
}
Aggregations