use of org.grails.web.converters.exceptions.ConverterException in project grails-core by grails.
the class GroovyBeanMarshaller 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(clazz)) {
Method readMethod = property.getReadMethod();
String name = property.getName();
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 : clazz.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isPublic(modifiers) && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) {
String name = field.getName();
if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name))
continue;
writer.key(name);
json.convertAnother(field.get(o));
}
}
writer.endObject();
} catch (ConverterException ce) {
throw ce;
} catch (Exception e) {
throw new ConverterException("Error converting Bean with class " + clazz.getName(), e);
}
}
use of org.grails.web.converters.exceptions.ConverterException in project grails-core by grails.
the class ValidationErrorsMarshaller method marshalObject.
public void marshalObject(Object object, JSON json) throws ConverterException {
Errors errors = (Errors) object;
JSONWriter writer = json.getWriter();
try {
writer.object();
writer.key("errors");
writer.array();
for (Object o : errors.getAllErrors()) {
if (o instanceof FieldError) {
FieldError fe = (FieldError) o;
writer.object();
json.property("object", fe.getObjectName());
json.property("field", fe.getField());
json.property("rejected-value", fe.getRejectedValue());
Locale locale = LocaleContextHolder.getLocale();
if (applicationContext != null) {
json.property("message", applicationContext.getMessage(fe, locale));
} else {
json.property("message", fe.getDefaultMessage());
}
writer.endObject();
} else if (o instanceof ObjectError) {
ObjectError fe = (ObjectError) o;
writer.object();
json.property("object", fe.getObjectName());
Locale locale = LocaleContextHolder.getLocale();
if (applicationContext != null) {
json.property("message", applicationContext.getMessage(fe, locale));
} else {
json.property("message", fe.getDefaultMessage());
}
writer.endObject();
}
}
writer.endArray();
writer.endObject();
} catch (ConverterException ce) {
throw ce;
} catch (Exception e) {
throw new ConverterException("Error converting Bean with class " + object.getClass().getName(), e);
}
}
use of org.grails.web.converters.exceptions.ConverterException in project grails-core by grails.
the class GroovyBeanMarshaller method marshalObject.
public void marshalObject(Object o, XML xml) throws ConverterException {
try {
Class<? extends Object> clazz = o.getClass();
List<String> excludes = xml.getExcludes(clazz);
List<String> includes = xml.getIncludes(clazz);
IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>();
boolean isEntity = o.getClass().getAnnotation(Entity.class) != null;
for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(o.getClass())) {
String name = property.getName();
if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name))
continue;
if (isEntity && (name.equals(GrailsDomainClassProperty.ATTACHED) || name.equals(GrailsDomainClassProperty.ERRORS)))
continue;
Method readMethod = property.getReadMethod();
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);
xml.startNode(name);
xml.convertAnother(value);
xml.end();
}
}
for (Field field : o.getClass().getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isPublic(modifiers) && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) {
String name = field.getName();
if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name))
continue;
if (isEntity && (name.equals(GrailsDomainClassProperty.ATTACHED) || name.equals(GrailsDomainClassProperty.ERRORS)))
continue;
xml.startNode(name);
xml.convertAnother(field.get(o));
xml.end();
}
}
} 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.converters.exceptions.ConverterException in project grails-core by grails.
the class ValidationErrorsMarshaller method marshalObject.
public void marshalObject(Object object, XML xml) throws ConverterException {
Errors errors = (Errors) object;
try {
for (Object o : errors.getAllErrors()) {
if (o instanceof FieldError) {
FieldError fe = (FieldError) o;
xml.startNode("error");
xml.attribute("object", fe.getObjectName());
xml.attribute("field", fe.getField());
xml.startNode("rejected-value").convertAnother(fe.getRejectedValue());
xml.end();
Locale locale = LocaleContextHolder.getLocale();
if (applicationContext != null) {
xml.startNode("message").chars(applicationContext.getMessage(fe, locale)).end();
} else {
String defaultMessage = fe.getDefaultMessage();
if (defaultMessage != null)
xml.startNode("message").chars(defaultMessage).end();
}
xml.end();
}
}
} catch (ConverterException ce) {
throw ce;
} catch (Exception e) {
throw new ConverterException("Error converting Bean with class " + object.getClass().getName(), e);
}
}
Aggregations