My shop json is:
"shop":{
"name" : {
"en" : "My Shop",
"es" : "Mi Tienda"
},
"order" : 0
}
And shop model is:
class Shop{
public String name;
public int order
}
So how can I deserialize that kind of Json to this model? Can I write a custom serializer for only "name" field?
You can use the @JsonAdapter annotation on the first for which you want to deserialize using custom logic. In your example, you would write a TypeAdapter or TypeAdapterFactory which handled selecting the correct language from the object and return the string value.
Thank you,
public class MultilingualStringAdapter extends TypeAdapter<String> {
@Override
public void write(JsonWriter out, String value) throws IOException {
Log.d("multilingual test", value);
}
@Override
public String read(JsonReader in) throws IOException {
String value = null;
in.beginObject();
while(in.hasNext()){
String name = in.nextName();
if(name.equals(Global.getAppLanguage().code)){
value = in.nextString();
}
else {
in.skipValue();
}
}
in.endObject();
return value;
}
}
solved my issue.
Most helpful comment
You can use the
@JsonAdapterannotation on the first for which you want to deserialize using custom logic. In your example, you would write aTypeAdapterorTypeAdapterFactorywhich handled selecting the correct language from the object and return the string value.