问题描述
我正在寻找一种通过Grails JSON转换进行字符串格式化的方法,类似于自定义格式日期,我在 类似这样的内容:
import grails.converters.JSON;
class BootStrap {
def init = {servletContext - >
JSON.registerObjectMarshaller(String){
返回它?.trim()}
}
def destroy = {
}
}
我知道自定义格式可以在每个域类的基础上完成,但我正在寻找更全面的解决方案。 / p>
尝试创建使用特定格式的属性名称或类的自定义编码器。
class CustomDtoObjectMarshaller实现ObjectMarshaller< JSON> {
String [] excludedProperties = ['metaClass']
public boolean supports(Object object){
返回对象instanceof GroovyObject;
}
public void marshalObject(Object o,JSON json)throws ConverterException {
JSONWriter writer = json.getWriter();
尝试{
writer.object(); (PropertyDescriptor属性:BeanUtils.getPropertyDescriptors(o.getClass())){
String name = property.getName();
方法readMethod = property.getReadMethod();
if(readMethod!= null&&!(excludedProperties中的名称)){
Object value = readMethod.invoke(o,(Object [])null);
if(value!= null){
writer.key(name);
json.convertAnother(value);
$ b for(Field field:o.getClass()。getDeclaredFields()){
int modifiers = field.getModifiers();
if(Modifier.isPublic(modifiers)&&!(Modifier.isStatic(modifiers)|| Modifier.isTransient(modifiers))){
writer.key(field.getName());
json.convertAnother(field.get(o));
}
}
writer.endObject();
}
catch(ConverterException ce){
throw ce;
}
catch(Exception e){
throw new ConverterException(使用类转换Bean时出错+ o.getClass()。getName(),e);
}
}
}
注册在bootstrap:
CustomDtoObjectMarshaller customDtoObjectMarshaller = new CustomDtoObjectMarshaller()
customDtoObjectMarshaller.excludedProperties = [ 'metaClass','class']
JSON.registerObjectMarshaller(customDtoObjectMarshaller)
在我的示例我只是scip'metaClass'和'class'字段。我认为最常见的方式是
I am looking for a way to do some string formatting through Grails JSON conversion, similar to custom formatting dates, which I found in this post.
Something like this:
import grails.converters.JSON;
class BootStrap {
def init = { servletContext ->
JSON.registerObjectMarshaller(String) {
return it?.trim() }
}
def destroy = {
}
}
I know custom formatting can be done on a per domain class basis, but I am looking for a more global solution.
Try to create custom marshaller that use specific formatting for property names or class. Just look at the marshaller bellow and modify it:
class CustomDtoObjectMarshaller implements ObjectMarshaller<JSON>{
String[] excludedProperties=['metaClass']
public boolean supports(Object object) {
return object instanceof GroovyObject;
}
public void marshalObject(Object o, JSON json) throws ConverterException {
JSONWriter writer = json.getWriter();
try {
writer.object();
for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(o.getClass())) {
String name = property.getName();
Method readMethod = property.getReadMethod();
if (readMethod != null && !(name in excludedProperties)) {
Object value = readMethod.invoke(o, (Object[]) null);
if (value!=null) {
writer.key(name);
json.convertAnother(value);
}
}
}
for (Field field : o.getClass().getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isPublic(modifiers) && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) {
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);
}
}
}
The register in bootstrap:
CustomDtoObjectMarshaller customDtoObjectMarshaller=new CustomDtoObjectMarshaller()
customDtoObjectMarshaller.excludedProperties=['metaClass','class']
JSON.registerObjectMarshaller(customDtoObjectMarshaller)
In my example I just scip 'metaClass' and 'class' fields. I think the most common way
这篇关于在Grails JSON编组中自定义字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!