Reputation: 463
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.
Upvotes: 0
Views: 980
Reputation: 39406
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.
Upvotes: 2
Reputation: 2539
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)
Upvotes: 1
Reputation: 11
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.
Upvotes: 1
Reputation: 914
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.
Upvotes: 1