2

I'm working to deserialize World Weather Online API results with localizations.

the localization data returned as a lang-{locale} block depending by the given language (WWO supports ~40 languages:

 "hourly": [

           {

              "chanceoffog": "0",

              "chanceoffrost": "0",

              "chanceofhightemp": "0",

              "chanceofovercast": "63",

              "chanceofrain": "3",

              "chanceofremdry": "0",

              "chanceofsnow": "0",

              "chanceofsunshine": "0",

              "chanceofthunder": "0",

              "chanceofwindy": "0",

              "cloudcover": "88",

              "DewPointC": "1",

              "DewPointF": "34",

              "FeelsLikeC": "4",

              "FeelsLikeF": "39",

              "HeatIndexC": "7",

              "HeatIndexF": "44",

              "humidity": "67",

              "lang_ru": [

                 {

                    "value": "Пасмурно"

                 }

              ],

              "precipMM": "0.0",

              "pressure": "1033",

              "tempC": "7",

              "tempF": "44",

              "time": "24",

              "visibility": "10",

              "weatherCode": "122",

              "weatherDesc": [

                 {

                    "value": "Overcast"

                 }

              ],

              "weatherIconUrl": [

                 {

                    "value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png"

                 }

              ],

              "WindChillC": "4",

              "WindChillF": "39",

              "winddir16Point": "WNW",

              "winddirDegree": "294",

              "WindGustKmph": "17",

              "WindGustMiles": "11",

              "windspeedKmph": "14",

              "windspeedMiles": "9"

           }

        ]

It also can be lang_es, lang_fr etc..

I need to deserialize it to one field with the string inside regardless of the language given.

One ugly solution I thought about is adding 40 fields according to the languages and checking if the relevant is not null.

The other quite ugly solution is writing a full custom deserializer for hourly, but hourly has loads of (over 30) simple string fields so writing a custom deserializer for it seems inefficient and against the simplicity of GSON.

Is there any better neat and simple solution to write a custom deserializer only for the lang objects and allow GSON to automatically deserialize the rest of the data?

arseny
  • 396
  • 2
  • 13

1 Answers1

0

I had a similar problem where a user object can have id, userId or user_id, this is how I managed to map all three of them to id.

public class UserDeserializer implements JsonDeserializer<User> {


@Override
public User deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

    try {
        User user = new GsonBuilder().create().fromJson(json, User.class);
        if (user.id == null) {
            JsonObject jsonObject = json.getAsJsonObject();
            JsonElement userId = jsonObject.get("userid");

            if (userId != null) {
                user.id = userId.getAsString();
                return user;
            }
            JsonElement user_id = jsonObject.get("user_id");
            if (user_id != null) {
                user.id = user_id.getAsString();
                return user;
            }
        }
        return user;
    } catch (JsonParseException ex) {
        ex.printStackTrace();
        throw ex;
    }
}

}

atabouraya
  • 3,233
  • 1
  • 26
  • 31