Can I GSON deserialize a variable name which includes a hyphen in Java?

Just thought of sharing, if you are using Jackson, which I believe many people are, you can use:

import com.fasterxml.jackson.annotation.JsonProperty;

class Person {
    boolean success;
    @JsonProperty("person-name")
    String personName;
}

Choose a valid Java identifier and use the @SerializedName annotation to tell GSON the name of the corresponding JSON property:

import com.google.gson.annotations.SerializedName;

class Person {
    boolean success;
    @SerializedName("person-name")
    String personName;
}

Tags:

Java