【发布时间】:2011-07-30 04:17:22
【问题描述】:
在我的代码中,我有一个带有 Enum 类型的域对象。该枚举类型可以很好地从数据库中保存和检索。我正在将域对象列表转换为 json 单元格。除枚举之外的所有字段都是字符串或长整数。当 json 在“另一边”被拾取时,它会显示列的 [Object object],而不是枚举名称或枚举值。在域或 json 方面有什么可以帮助解决这个问题吗?下面的代码/示例
领域类
class MyDomain {
long id
long otherValue
MyEnum enu //Nullable per constraints
//Mapping and constraints are not special.
}
枚举
enum MyEnum {
ENUM1("Value1"),ENUM2("Value2")
//constructor ommitted
String myValue
String toString() { myValue }
Json 单元创建
def jsonCells = domainList.collect
{
[cell: [
it.id,
it.otherValue,
it.enu?.value
],
id: it.id]
}
it.enu?.value 有效。但是,有没有更好的方法来做到这一点,我不必每次都调用该值并且可以依赖该对象?我会假设重写 toString 方法会处理它,但显然我错了。我知道这似乎是一个小问题,但更容易忘记“.value”,尤其是在许多域对象中使用相同的枚举时。想法?
【问题讨论】: