1

I have a class like this

public class Person()
{
    @SerializedName("c")
    public String name;
    @SerializedName("i")
    public int id;
}

Can i serialize it in manner of retain the field name, not the serialize name without removing the annotation?

I can't change class declaration cause these annotation are used for serializing to database, and i want to make a readable string of these objects for debugging.

eg: {"name":"Mark","id":0} instead of {"c":"Mark","i":0}

tiboo
  • 8,213
  • 6
  • 33
  • 43

1 Answers1

4

Just remove @SerializedName("c"). By default it should be 'name' after serialization.

See explanation and example here: http://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/google/gson/annotations/SerializedName.html

That seems to be exactly your case.

If the data file should not be edited, as the author has suggested in the question update, an alternative solution can be changing or deleting @SerializedName("c") annotation through Java reflection, which is described in details here: http://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/google/gson/annotations/SerializedName.html

Oleg Gryb
  • 5,122
  • 1
  • 28
  • 40
  • I edited my question, i can't change class declaration cause it's used for serializing to database, and i want to make a readable string of these objects for debugging . – tiboo May 29 '14 at 06:59
  • 1
    Then your might want to change or remove annotation through reflection http://stackoverflow.com/questions/14268981/modify-a-class-definitions-annotation-string-parameter-at-runtime/14276270#14276270 – Oleg Gryb May 29 '14 at 13:55