use of org.grails.web.converters.exceptions.ConverterException in project grails-core by grails.
the class Base64ByteArrayMarshaller method marshalObject.
public void marshalObject(Object object, XML xml) throws ConverterException {
xml.attribute("encoding", "BASE-64");
xml.chars("");
Writable w;
if (object instanceof byte[]) {
w = EncodingGroovyMethods.encodeBase64((byte[]) object);
} else {
w = EncodingGroovyMethods.encodeBase64((Byte[]) object);
}
try {
w.writeTo(xml.getStream());
} catch (IOException e) {
throw new ConverterException(e);
}
}
use of org.grails.web.converters.exceptions.ConverterException in project grails-core by grails.
the class EnumMarshaller method marshalObject.
public void marshalObject(Object en, XML xml) throws ConverterException {
try {
Class<?> enumClass = en.getClass();
xml.attribute("enumType", enumClass.getName());
Method nameMethod = BeanUtils.findDeclaredMethod(enumClass, "name", null);
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);
}
}
use of org.grails.web.converters.exceptions.ConverterException in project grails-core by grails.
the class GenericJavaBeanMarshaller method marshalObject.
public void marshalObject(Object o, XML xml) throws ConverterException {
try {
for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(o.getClass())) {
String name = property.getName();
Method readMethod = property.getReadMethod();
if (readMethod != null) {
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 (field.isAccessible() && Modifier.isPublic(modifiers) && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) {
xml.startNode(field.getName());
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 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.converters.exceptions.ConverterException in project grails-core by grails.
the class JSON method parse.
/**
* Parses the given request's InputStream and returns either a JSONObject or a JSONArray
*
* @param request the JSON Request
* @return either a JSONObject or a JSONArray - depending on the given JSON
* @throws ConverterException when the JSON content is not valid
*/
public static Object parse(HttpServletRequest request) throws ConverterException {
Object json = request.getAttribute(CACHED_JSON);
if (json != null) {
return json;
}
String encoding = request.getCharacterEncoding();
if (encoding == null) {
encoding = Converter.DEFAULT_REQUEST_ENCODING;
}
try {
PushbackInputStream pushbackInputStream = null;
int firstByte = -1;
try {
pushbackInputStream = new PushbackInputStream(request.getInputStream());
firstByte = pushbackInputStream.read();
} catch (IOException ioe) {
}
// code has only been changed from here down
if (firstByte == -1) {
json = new JSONObject();
} else {
pushbackInputStream.unread(firstByte);
json = parse(pushbackInputStream, encoding);
}
request.setAttribute(CACHED_JSON, json);
return json;
} catch (IOException e) {
throw new ConverterException("Error parsing JSON", e);
}
}
Aggregations