8

我的课:

class ExampleBean {
   private String _firstField;
   private String _secondField;
   // respective getters and setters
}

我想出现如下:

{
     "FirstField":"value",
     "SecondField":"value"
}

而不是这样

{
     "_FirstField":"value",
     "_SecondField":"value"
}

我初始化解析器如下:

GsonBuilder builder = new GsonBuilder();
    builder.setDateFormat(DateFormat.LONG);
    builder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE);
    builder.setPrettyPrinting();
    set_defaultParser(builder.create());

我可以在“FieldNamePolicy”的文档中看到API和文档,但我很惊讶没有提供跳过“_”的选项我也知道我可以使用注释......

@ SerializedName (" custom_naming ")

...但不想为我的所有领域写这个...

区分局部变量和类的字段对我来说非常有用。:( 任何的想法?

编辑:会有很多明显的解决方案,(继承、gson 覆盖方法、正则表达式)。我的问题更侧重于是否有 gson 的本机解决方案或侵入性较小的修复?

也许我们可以提议作为新的 FieldNamePolicy?

4

2 回答 2

14

GsonBuilder提供了一种setFieldNamingStrategy()允许您传递自己的FieldNamingStrategy实现的方法。

请注意,这将替换调用setFieldNamingPolicy()- 如果您查看GsonBuilder这两种方法的源代码,它们是互斥的,因为它们设置了相同的内部字段(FieldNamingPolicy枚举是 a FieldNamingStrategy)。

public class App
{
    public static void main(String[] args)
    {
        Gson gson = new GsonBuilder()
                        .setFieldNamingStrategy(new MyFieldNamingStrategy())
                        .setPrettyPrinting()
                        .create();

        System.out.println(gson.toJson(new ExampleBean()));
    }
}

class ExampleBean
{

    private String _firstField = "first field value";
    private String _secondField = "second field value";
    // respective getters and setters
}

class MyFieldNamingStrategy implements FieldNamingStrategy
{
    public String translateName(Field field)
    {
        String fieldName = 
            FieldNamingPolicy.UPPER_CAMEL_CASE.translateName(field);
        if (fieldName.startsWith("_"))
        {
            fieldName = fieldName.substring(1);
        }
        return fieldName;
    }
}

输出:

{
  "FirstField": "first field value",
  "SecondField": "second field value"
}
于 2013-02-20T19:17:38.283 回答
1

你想要的是

import java.lang.reflect.Field;
import java.text.DateFormat;

import com.google.gson.FieldNamingStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GsonExample {

    public static void main(String... args) throws Exception {
        final GsonBuilder builder = new GsonBuilder();
        builder.setDateFormat(DateFormat.LONG);
        builder.setPrettyPrinting();
        builder.setFieldNamingStrategy(new FieldNamingStrategy() {
            @Override
            public String translateName(Field f) {
                String fieldName = f.getName();
                if(fieldName.startsWith("_") && fieldName.length() > 1) {
                    fieldName = fieldName.substring(1, 2).toUpperCase() + fieldName.substring(2);
                }
                return fieldName;
            }
        });
        final Gson gson = builder.create();
        System.out.println(gson.toJson(new ExampleBean("example", "bean")));
    }


    private static class ExampleBean {
        private final String _firstField;
        private final String _secondField;

        private ExampleBean(String _firstField, String _secondField) {
            this._firstField = _firstField;
            this._secondField = _secondField;
        }
    }
}

这会产生

{"FirstField":"example","SecondField":"bean"}
于 2014-10-16T18:32:44.417 回答