0

I get this JSON data from front-end:

user    {"idUser":5,"fullname":"Bob Marley", "birthday":"1990-12-14", "login":"b.marley", "password":"none","dateCreate":"2014-10-09 15:01","grp":{"idGrp":"2","nameGrp":"client"}}

I want to do deserialization these data in java:

User.java

public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    private int idUser;
    private String fullname;
    private Date birthday;
    private String login;
    private String password;
    private Date dateCreate;
    private Grp grp;
    //getters and setters

UserDeserializer.java

public class UserDeserializer implements JsonDeserializer<User> {

    @Override
    public User deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        JsonObject jObject = json.getAsJsonObject();
        User user = new User();
        user.setIdUser(jObject.get("idUser").getAsInt());
        user.setFullname(jObject.get("fullname").getAsString());

        try {
            Date birthday = new SimpleDateFormat("yyyy-MM-dd").parse(jObject.get("birthday").getAsString());
            user.setBirthday(birthday);
        } catch (ParseException e) {
        }

        user.setLogin(jObject.get("login").getAsString());
        user.setPassword(jObject.get("password").getAsString());

        try {
            Date dateCreate = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(jObject.get("dateCreate").getAsString());
            user.setDateCreate(dateCreate);
        } catch (ParseException e) {
        }

        Grp grp = context.deserialize(jObject.get("grp"), Grp.class);
        user.setGrp(grp);    
        return user;
    }
}

UserEdit.java

public class UserEdit extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(User.class, new UserDeserializer());
        Gson gson = new GsonBuilder().create();
        User user = gson.fromJson(request.getParameter("user"), User.class);

And I get this error at the last row of the UserEdit.java:

04-Nov-2014 21:30:44.003 SEVERE [http-apr-8080-exec-1] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [Servlet UserEdit] in context with path [] threw exception com.google.gson.JsonSyntaxException: 1990-12-14 at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:81) at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:66) at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:41) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:95) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:183) at com.google.gson.Gson.fromJson(Gson.java:805) at com.google.gson.Gson.fromJson(Gson.java:770) at com.google.gson.Gson.fromJson(Gson.java:719) at com.google.gson.Gson.fromJson(Gson.java:691) at com.gmail.zigfridus.UserEdit.doGet(UserEdit.java:24) at javax.servlet.http.HttpServlet.service(HttpServlet.java:618) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1017) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652) at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2451) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2440) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:722) Caused by: java.text.ParseException: Unparseable date: "1990-12-14" at java.text.DateFormat.parse(DateFormat.java:357) at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:79) ... 32 more

It says that error is because of birthday date, but that date looks like normal.

Please, help me.

9
  • The user at the beginning of the JSON user {"idUser":5, makes this ill-formed JSON. Is there really a user at the beginning? Commented Nov 4, 2014 at 19:56
  • @Vinze: which one exactly are you referring to, that deals with "any problem"? Commented Nov 4, 2014 at 20:00
  • @zigfridus are you sure your deserialize method is called? Commented Nov 4, 2014 at 20:01
  • @JonathanM Yes, there is really user at the beginning: http://localhost:8080/UserEdit?user=%7B%22idUser%22%3A5%2C%22... Commented Nov 4, 2014 at 20:04
  • @njzk2 I wrongly read the question, I think... removed my comment. No entry of the stack seems in user code, so it may be a misuse of the API... Commented Nov 4, 2014 at 20:12

4 Answers 4

2

Here :

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(User.class, new UserDeserializer());
Gson gson = new GsonBuilder().create();

You are creating a new GsonBuilder, effectively dismissing the one you just configured. Use

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(User.class, new UserDeserializer());
Gson gson = gsonBuilder.create();

to use the gsonBuilder configured with the user deserializer.

Sign up to request clarification or add additional context in comments.

1 Comment

It was a really stupid mistake. How I didn't see it, I don't understand... Thank You very much for your help! )
1

Use yyyy-MM-dd HH:mm:ss as your simple date format. I know you don't have hours, minutes and seconds, but I don't believe that it accepts it without them.

2 Comments

I added dummy time to the birthday: "birthday":"1990-12-14 17:08:44". But it didn't help. I got the same error: com.google.gson.JsonSyntaxException: 1990-12-14 17:08:44
Try not adding the dummy time. It will come out zeroes, but It should do the correct format on its own.
1

You will need to add values for hh:mm:ss in order to instantiate an acceptable date format. You obviously aren't concerned with the time and in this case I would suggest simply using 00:00:00.

Comments

1

The error seems the use of DateTypeAdapter in the API internal bind... I'm not familiar with this library but maybe your UserDeserializer is not correctly configured in the API/framework and default one is used (and birthday is Date in the bean to deserialize)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.