Dev
Dev

Reputation: 15

GSON to JSON conversion - long format and simple date format "yyyy-MM-dd'T'HH:mm'Z'"

I'm using GSON to convert JSONArray to Java objects. I need to convert both long date and simple date format "yyyy-MM-dd'T'HH:mm'Z'" to Java objects. I'm able to convert alone long or simple date but couldn't able to convert both together.

used below code snippet for long conversion :

GsonBuilder builder = new GsonBuilder();

    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
            return new Date(json.getAsJsonPrimitive()
                    .getAsLong());
        }
    });

    Gson gson = builder.create();

    Type userListType = new TypeToken<ArrayList<CurrentLocation>>() {
    }.getType();

    gson.fromJson(data, userListType);

Sample data I need to convert :

[
  {
    "instance": {
      "location": {
        "lat": 31.522291,
        "lon": -96.532816,
        "timestamp": 1587693720000
      },
      "timeAtLocation": "2020-04-23T04:59Z"
    }
  },
  {
    "instance": {
      "location": {
        "lat": 31.522291,
        "lon": -96.532816,
        "timestamp": 1737693720000
      },
      "timeAtLocation": "2020-0-23T04:59Z"
    }
  }
]

Upvotes: 0

Views: 1153

Answers (1)

silentsudo
silentsudo

Reputation: 6973

Please try following code:

public class Main {

    static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm'Z'";
    static final Calendar calendar = Calendar.getInstance();

    static class LongUtil {
        static boolean isLong(String longValue) {
            try {
                Long.parseLong(longValue);
                return true;
            } catch (RuntimeException e) {
                return false;
            }

        }
    }

    static class DateDeserializer implements JsonDeserializer<Date> {

        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);

        @Override
        public Date deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
            System.out.println(type.getTypeName());
            System.out.println(jsonElement.getAsJsonPrimitive());
            if (jsonElement.getAsJsonPrimitive() != null) {
                final String expectedDateValue = jsonElement.getAsJsonPrimitive().getAsString();
                if (LongUtil.isLong(expectedDateValue)) {
                    System.out.println("It is long value hence parsing it to long");

                    calendar.setTimeInMillis(Long.parseLong(expectedDateValue));
                    return calendar.getTime();
                } else {
                    System.out.println("It is dateformat value hence parsing it to dateformat");
                    try {
                        return simpleDateFormat.parse(expectedDateValue);
                    } catch (ParseException e) {
                        throw new JsonParseException("Invalud dateFormat while parsing");
                    }
                }
            } else {
                throw new JsonParseException("JSOn Premitive Exception null");
            }
        }
    }

    public static void main(String[] args) throws IOException {
        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(Date.class, new DateDeserializer());
        Gson gson = builder.create();
        Type userListType = new TypeToken<ArrayList<Wrapper>>() {
        }.getType();
        File file = new File("response.json");
        System.out.println("file exists : " + file.exists());
        JsonReader jsonReader = new JsonReader(new FileReader(file));

        List<Wrapper> wrapper = gson.fromJson(jsonReader, userListType);

        System.out.println(wrapper);
    }

    class Wrapper {
        User instance;

        @Override
        public String toString() {
            return "Wrapper{" +
                    "instance=" + instance +
                    '}';
        }
    }

    class User {
        Location location;
        Date timeAtLocation;


        @Override
        public String toString() {
            return "User{" +
                    "location=" + location +
                    ", timeAtLocation='" + timeAtLocation + '\'' +
                    '}';
        }
    }


    class Location {
        String lat;
        String lon;
        Date timestamp;

        @Override
        public String toString() {
            return "Location{" +
                    "lat='" + lat + '\'' +
                    ", lon='" + lon + '\'' +
                    ", timestamp='" + timestamp + '\'' +
                    '}';
        }
    }
}

Explaination:

While deserializing i am checking if the input string is either in dateformat or in long and based on that I create the calendar instance, then i set long and finally get date object date otherwise just use the string and format it based on date formatter.

Output:

file exists : true
It is long value hence parsing it to long
It is dateformat value hence parsing it to dateformat
It is long value hence parsing it to long
It is dateformat value hence parsing it to dateformat
[Wrapper{instance=User{location=Location{lat='31.522291', lon='-96.532816', timestamp='Fri May 01 13:49:08 IST 2020'}, timeAtLocation='Thu Apr 23 04:59:00 IST 2020'}}, Wrapper{instance=User{location=Location{lat='31.522291', lon='-96.532816', timestamp='Fri Jan 24 10:12:00 IST 2025'}, timeAtLocation='Mon Dec 23 04:59:00 IST 2019'}}]

Upvotes: 1

Related Questions