When we define a class with following format
public class Field {
@SerializedName("name")
public String name;
@SerializedName("category")
public String category;
}
for the JsonObject
content
{
"name" : "string",
"category" : "string",
}
and using Gson
to parse the content
Field field = new GsonBuilder().create().fromJson(
content, Field.class);
So,my question is can we use Gson
to get the @Serialized
name. For in this instance I want to know what @Serialized
name is used for field.name
,which is name
and for field.category
which is category
.
As per suggested by @Sotirios Delimanolis, using Reflection
we can get the Serialized
name
java.lang.reflect.Field fields = Field.class.getDeclaredField("name");
SerializedName sName =fields.getAnnotation(SerializedName.class);
System.out.println(sName.value());
Use reflection to retrieve the Field
object you want. You can then use Field#getAnnotation(Class)
to get a SerializedName
instance on which you can call value()
to get the name.
Instead of parsing with Field.class
, can't you parse it into a JsonObject.class
instead? Then use JsonObject.get()
:
import com.google.gson.JsonObject;
Gson gson = new GsonBuilder().create();
JsonObject jsonObject = gson.fromJson(content, JsonObject.class);
String serializedName = jsonObject.get("name").getAsString();
Note that .getAsString()
will return it as a String without embedded double quotes, compare this to when you call toString()
.
One thing I was trying to do was serialize an enum field, which is not an object. In that case, you can serialize using JsonElement.class
, since it's just a primitive:
import com.google.gson.JsonElement;
Gson gson = new GsonBuilder().create();
JsonElement jsonElement = gson.fromJson("\"a\"", JsonElement.class);
String serializedName = jsonElement.getAsString();
Thats a way with alternate
for example for a more complex example
public enum SomeStatusCd {
@SerializedName(
value = "status_1",
alternate = {"an alternate 1", "an alternate 2"}
)
STATUS_1("status_1")
....
}
public static SomeStatusCd getFromAlternate(String alternateFieldName){
SomeStatusCd result = null;
Field[] statusDeclaredFields = SomeStatusCd.class.getDeclaredFields();
String foundEnumName = null;
for (Field statusDeclaredField : statusDeclaredFields) {
SerializedName annotation = statusDeclaredField.getAnnotation(SerializedName.class);
if (annotation != null){
String[] declaredFieldAlternates = annotation.alternate();
for (String declaredFieldAlternate : declaredFieldAlternates) {
if (declaredFieldAlternate.equals(alternateFieldName)){
foundEnumName = statusDeclaredField.getName();
}
}
}
}
if (foundEnumName != null){
for (SomeStatusCd enumConstant : SomeStatusCd.class.getEnumConstants()) {
if (enumConstant.name().equals(foundEnumName)){
result = enumConstant;
}
}
}
return result;
}
SomeStatusCd fromAlternate = getFromAlternate("an alternate 1");
assertSame(fromAlternate, SomeStatusCd.STATUS_1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.