How to prevent Gson serialization / deserialization of the first character of a field (underscore)?

My class:

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

I want to look like this:

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

And don't like it

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

I initialize the parser as follows:

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

I could see the API in the documentation from "FieldNamePolicy" as well, but I'm surprised that it doesn’t allow me to skip "_", I also know that I can use the annotation ...

@ SerializedName (" custom_naming ")

... but I don't want to write this for alllllll of my fields ...

It is very useful for me to distinguish between local variables and class fields. :( Any idea?

EDIT: There would be many obvious solutions (inheritance, gson rewrite methods, regular expressions). My question is more focused on whether there is a native gson solution or a less intrusive fix?

, FieldNamePolicy?

+5
2

GsonBuilder setFieldNamingStrategy(), FieldNamingStrategy.

, setFieldNamingPolicy() - GsonBuilder, , (FieldNamingPolicy enum - 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"
}
+11

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"}
0

All Articles