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(); } }
/** * Directs the JSON Writer to the given Writer * * @param out the Writer * @throws org.grails.web.converters.exceptions.ConverterException * */ public void render(Writer out) throws ConverterException { prepareRender(out); value(target); finalizeRender(out); }
/** * Parses the given JSON String and returns either a JSONObject or a JSONArray * * @param reader JSON source * @return either a JSONObject or a JSONArray - depending on the given JSON * @throws ConverterException when the JSON content is not valid */ public static JSONElement parse(Reader reader) throws ConverterException { return parse(IOUtils.toString(reader)); }
public void marshalObject(Object en, JSON json) throws ConverterException { JSONWriter writer = json.getWriter(); try { writer.object(); Class<?> enumClass = en.getClass(); json.property("enumType", enumClass.getName()); Method nameMethod = BeanUtils.findDeclaredMethod(enumClass, "name", null); try { json.property("name",nameMethod.invoke(en)); } catch (Exception e) { json.property("name", ""); } writer.endObject(); } catch (ConverterException ce) { throw ce; } catch (Exception e) { throw new ConverterException("Error converting Enum with class " + en.getClass().getName(), e); } } }
@SuppressWarnings({ "unchecked", "rawtypes" }) public void marshalObject(Object value, JSON json) throws ConverterException { JSONWriter writer = json.getWriter(); value = proxyHandler.unwrapIfProxy(value); Class<?> clazz = value.getClass(); List<String> excludes = json.getExcludes(clazz); List<String> includes = json.getIncludes(clazz); IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>(); Object idValue = extractValue(value, id); if(idValue != null) { json.property(GrailsDomainClassProperty.IDENTITY, idValue); Object version = extractValue(value, versionProperty); if(version != null) { json.property(GrailsDomainClassProperty.VERSION, version); json.convertAnother(val); referenceObject = new ArrayList((Collection) referenceObject); json.convertAnother(referenceObject); json.value(null); json.convertAnother(referenceObject);
public void marshalObject(Object o, JSON json) throws ConverterException { JSONWriter writer = json.getWriter(); List<String> excludes = json.getExcludes(clazz); List<String> includes = json.getIncludes(clazz); IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>(); Object value = readMethod.invoke(o, (Object[]) null); writer.key(name); json.convertAnother(value); if(!shouldInclude(includeExcludeSupport,includes,excludes,o,name)) continue; writer.key(field.getName()); json.convertAnother(field.get(o));
public void marshalObject(Object object, JSON converter) throws ConverterException { byte[] bytes = (byte[]) object; converter.convertAnother(String.format("%d Bytes", bytes.length)); } }
public void marshalObject(Object object, JSON converter) throws ConverterException { try { converter.getWriter().value(formatter.format(object)); } catch (JSONException e) { throw new ConverterException(e); } } }
public static void use(String cfgName) throws ConverterException { if (cfgName == null || "default".equals(cfgName)) { ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(JSON.class, null); } else { ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(JSON.class, getNamedConfig(cfgName)); } }
/** * Default Constructor for a JSON Converter */ public JSON() { config = initConfig(); encoding = config != null ? config.getEncoding() : "UTF-8"; contentType = MimeType.JSON.getName(); circularReferenceBehaviour = config != null ? config.getCircularReferenceBehaviour() : CircularReferenceBehaviour.DEFAULT; prettyPrint = config != null && config.isPrettyPrint(); }
public void marshalObject(Object o, JSON json) throws ConverterException { JSONWriter writer = json.getWriter(); List<String> excludes = json.getExcludes(clazz); List<String> includes = json.getIncludes(clazz); IncludeExcludeSupport<String> includeExcludeSupport = new IncludeExcludeSupport<String>(); try { Object value = readMethod.invoke(o, (Object[]) null); writer.key(name); json.convertAnother(value); if(!shouldInclude(includeExcludeSupport,includes,excludes,o,name)) continue; writer.key(name); json.convertAnother(field.get(o));
public void marshalObject(Object object, JSON json) throws ConverterException { Errors errors = (Errors) object; JSONWriter writer = json.getWriter(); 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)); json.property("message", fe.getDefaultMessage()); 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)); json.property("message", fe.getDefaultMessage());
public void marshalObject(Object object, JSON converter) throws ConverterException { converter.convertAnother(object.toString()); } }
public void marshalObject(Object object, JSON converter) throws ConverterException { try { converter.getWriter().value(object); } catch (JSONException e) { throw new ConverterException(e); } } }
public static Object use(String configName, Closure<?> callable) throws ConverterException { ConverterConfiguration<JSON> old = ConvertersConfigurationHolder.getThreadLocalConverterConfiguration(JSON.class); ConverterConfiguration<JSON> cfg = getNamedConfig(configName); ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(JSON.class, cfg); try { return callable.call(); } finally { ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(JSON.class, old); } }
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(); } }
public void marshalObject(Object object, JSON converter) throws ConverterException { MetaMethod method = getToJSONMethod(object); try { Object result = method.invoke(object, new Object[]{ converter }); if (result != null && !(result instanceof JSON) && !(result instanceof JSONWriter)) { converter.convertAnother(result); } } catch(Throwable e) { throw e instanceof ConverterException ? (ConverterException)e : new ConverterException("Error invoking toJSON method of object with class " + object.getClass().getName(),e); } }
/** * Parses the given JSON and returns either a JSONObject or a JSONArray * * @param is An InputStream which delivers some JSON * @param encoding the Character Encoding to use * @return either a JSONObject or a JSONArray - depending on the given JSON * @throws ConverterException when the JSON content is not valid */ public static JSONElement parse(InputStream is, String encoding) throws ConverterException { return parse(IOUtils.toString(is, encoding)); }
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(); }