在Grails中选择深层或浅层的JSON序列化 [英] Choose deep or shallow JSON serialization in Grails

查看:298
本文介绍了在Grails中选择深层或浅层的JSON序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以轻松地指定是以深度还是浅度方式将对象转换为JSON?我知道你可以在Config.groovy文件中配置grails.converters.JSON实用程序,如下所示:

  grails .converters.json.default.deep = true 

但是当我转换某些对象时,我没有想深度转换。



我也看到有人推荐使用JSON.use(deep),但我得到以下错误:

 错误2012-03-04 00:39:13,673 [http-bio-8080-exec-1] ERROR errors.GrailsExceptionResolver  - 处理请求时发生IllegalAccessException:[GET] / Quaffic / home / json 
Class org.codehaus.groovy.grails.web.converters.marshaller.json.GenericJavaBeanMarshaller无法访问类org.joda.time.tz.DateTimeZoneBuilder的成员$ PrecalculatedZone with modifierspublic 。 Stacktrace如下所示:
Message:Class org.codehaus.groovy.grails.web.converters.marshaller.json.GenericJavaBeanMarshaller无法访问类org.joda.time.tz.DateTimeZoneBuilder的成员$ PrecalculatedZone带有修饰符public
Line |方法
- >> 198 |值在grails.converters.JSON

好像可能是joda.time错误,但是这个当我使用普通的Config.groovy技术时不会发生。有种令人困惑的...



任何帮助都会很棒!

解决方案

<我的解决方案是不依赖于深层/浅层json世代。我创建了一张地图,插入了元素,并对其进行了序列化。可能不是最佳做法,但它可以完成工作。

Is there a way to easily specify whether to convert an object as JSON in a deep or shallow manner? I know you can configure the grails.converters.JSON utility in the Config.groovy file by specifying something like the following:

grails.converters.json.default.deep = true

but when I convert certain objects, I don't want to deep convert.

I also saw that somebody recommended using JSON.use("deep"), but I get the following error:

Error 2012-03-04 00:39:13,673 ["http-bio-8080"-exec-1] ERROR errors.GrailsExceptionResolver  - IllegalAccessException occurred when processing request: [GET] /Quaffic/home/json
Class org.codehaus.groovy.grails.web.converters.marshaller.json.GenericJavaBeanMarshaller can not access a member of class org.joda.time.tz.DateTimeZoneBuilder$PrecalculatedZone with modifiers "public". Stacktrace follows:
Message: Class org.codehaus.groovy.grails.web.converters.marshaller.json.GenericJavaBeanMarshaller can not access a member of class org.joda.time.tz.DateTimeZoneBuilder$PrecalculatedZone with modifiers "public"
   Line | Method
->> 198 | value          in grails.converters.JSON

It seems like it could be a joda.time error, but this doesn't happen when I just use the plain Config.groovy technique. Kind of confusing...

Any help would be great!

解决方案

My solution was to not rely on deep/shallow json generation. I created a map, inserted elements, and serialized that. Probably not the best practice, but it gets the job done.

这篇关于在Grails中选择深层或浅层的JSON序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆