Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

com.google.gson.JsonSyntaxException when trying to parse Date/Time in json

I'm using RestTemplete to get json data from a rest api and I'm using Gson to parse data from json format to Object

Gson gson = new Gson();

restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new GsonHttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

List<Appel> resultList = null;

resultList = Arrays.asList(restTemplate.getForObject(urlService, Appel[].class));

but I get this problem with Date, what should I do ..

Could not read JSON: 1382828400000; nested exception is com.google.gson.JsonSyntaxException: 1382828400000

my Pojo that contains other pojos in it's body

public class Appel implements Serializable {

    private Integer numOrdre;
    private String reference;
    private String objet;
    private String organisme;
    private Double budget;
    private Double caution;
    private Date dateParution;
    private Date heureParution;
    private Date dateLimite;
    private Date heureLimite;
    private List<Support> supportList;
    private Ville villeid;
    private Categorie categorieid;

    public Appel() {
    }

    public Appel(Integer numOrdre, String reference, String objet, String organisme, Date dateParution, Date heureParution, Date dateLimite) {
        this.numOrdre = numOrdre;
        this.reference = reference;
        this.objet = objet;
        this.organisme = organisme;
        this.dateParution = dateParution;
        this.heureParution = heureParution;
        this.dateLimite = dateLimite;
    }

this is ths json returned by my API

[
   {
       "numOrdre": 918272,
       "reference": "some text",
       "objet": "some text",
       "organisme": "some text",
       "budget": 3000000,
       "caution": 3000000,
       "dateParution": 1382828400000,
       "heureParution": 59400000,
       "dateLimite": 1389657600000,
       "heureLimite": 34200000,
       "supportList":
       [
           {
               "id": 1,
               "nom": "some text",
               "dateSupport": 1384732800000,
               "pgCol": "013/01"
           },
           {
               "id": 2,
               "nom": "some text",
               "dateSupport": 1380236400000,
               "pgCol": "011/01"
           }
       ],
       "villeid":
       {
           "id": 2,
           "nom": "Ville",
           "paysid":
           {
               "id": 1,
               "nom": "Pays"
           }
       },
       "categorieid":
       {
           "id": 1,
           "description": "some text"
       }
   },
  .....
]
like image 473
Noureddine Avatar asked Apr 03 '14 14:04

Noureddine


People also ask

How do I change date format in GSON?

Gson rawGson = new Gson(); SimpleDateFormat fmt = new SimpleDateFormat("MMM d, yyyy HH:mm:ss") private class DateDeserializer implements JsonDeserializer<Date> { @Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { return new rawGson.

Is GSON toJson thread safe?

Gson is typically used by first constructing a Gson instance and then invoking toJson(Object) or fromJson(String, Class) methods on it. Gson instances are Thread-safe so you can reuse them freely across multiple threads.

Is GSON deprecated?

Gson is not deprecated.


2 Answers

Custom Serializers are no longer necessary - simply use GsonBuilder, and specify the date format, as such:

Timestamp t = new Timestamp(System.currentTimeMillis());

String json = new GsonBuilder()
               .setDateFormat("yyyy-MM-dd hh:mm:ss.S")
               .create()
               .toJson(t);

System.out.println(json);
like image 96
DiscDev Avatar answered Oct 01 '22 17:10

DiscDev


What I've done finally is going to my API project and create a CustomSerializer

public class CustomDateSerializer extends JsonSerializer<Date> {  

    @Override
    public void serialize(Date t, JsonGenerator jg, SerializerProvider sp) throws IOException, JsonProcessingException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = formatter.format(t);

        jg.writeString(formattedDate);
    }
}

that return the format yyyy-MM-dd and I annotated date fields with

@JsonSerialize(using = CustomDateSerializer.class)

in my Android application I created Gson object as

            Reader reader = new InputStreamReader(content);

            GsonBuilder gsonBuilder = new GsonBuilder();
            gsonBuilder.setDateFormat("yyyy-MM-dd");
            Gson gson = gsonBuilder.create();
            appels = Arrays.asList(gson.fromJson(reader, Appel[].class));
            content.close();

and it works for now .. thanks for your help I appreciate it

like image 25
Noureddine Avatar answered Oct 01 '22 15:10

Noureddine