android - Swagger: java.text.ParseException: Unparseable date error -


i'm using swagger define api specifications android project.

this how gsonbuilder initialized

public static gsonbuilder gsonbuilder;    static {     gsonbuilder = new gsonbuilder();     gsonbuilder.serializenulls();     gsonbuilder.setdateformat("yyyy-mm-dd't'hh:mm:ss.sssz");   } 

my json data returns 1 of parameter 'creationdate'. it's value 2017-08-14to2:42:59.528z when passes through apiinvoker method in swagger module, gave me below error.

java.text.parseexception:unparseable date "2017-08-14to2:42:59.528z"  

the 'creationdate' parameter not getting deserialized.

enter image description here

when debugged code further, found failing in defaultdatetypeadapter.java class. unable parse through of 3 formats in deserializetodate method.

private date deserializetodate(jsonelement json) {     synchronized (localformat) {       try {         return localformat.parse(json.getasstring());       } catch (parseexception ignored) {       }       try {         return enusformat.parse(json.getasstring());       } catch (parseexception ignored) {       }       try {         return iso8601format.parse(json.getasstring());       } catch (parseexception e) {         throw new jsonsyntaxexception(json.getasstring(), e);       }     }   } 

enter image description here

does know how solve error?

note: images posted giving more clarity errors. shows same information wrote.

this question's answer has been resolved. apparently, didn't mention version of gson in gradle application file. so, default assuming 2.3.1 version. but, gson data in 2.7 version. that's why failing.

if using gson 2.3.1 version, defaultdatetypeadapter.java has following methods

    final class defaultdatetypeadapter implements jsonserializer<date>, jsondeserializer<date> {    // todo: migrate streaming adapter    private final dateformat enusformat;   private final dateformat localformat;   private final dateformat iso8601format;    defaultdatetypeadapter() {     this(dateformat.getdatetimeinstance(dateformat.default, dateformat.default, locale.us),         dateformat.getdatetimeinstance(dateformat.default, dateformat.default));   }    defaultdatetypeadapter(string datepattern) {     this(new simpledateformat(datepattern, locale.us), new simpledateformat(datepattern));   }    defaultdatetypeadapter(int style) {     this(dateformat.getdateinstance(style, locale.us), dateformat.getdateinstance(style));   }    public defaultdatetypeadapter(int datestyle, int timestyle) {     this(dateformat.getdatetimeinstance(datestyle, timestyle, locale.us),         dateformat.getdatetimeinstance(datestyle, timestyle));   }    defaultdatetypeadapter(dateformat enusformat, dateformat localformat) {     this.enusformat = enusformat;     this.localformat = localformat;     this.iso8601format = new simpledateformat("yyyy-mm-dd't'hh:mm:ss'z'", locale.us);     this.iso8601format.settimezone(timezone.gettimezone("utc"));   }    // these methods need synchronized since jdk dateformat classes not thread-safe   // see issue 162   public jsonelement serialize(date src, type typeofsrc, jsonserializationcontext context) {     synchronized (localformat) {       string dateformatasstring = enusformat.format(src);       return new jsonprimitive(dateformatasstring);     }   }    public date deserialize(jsonelement json, type typeoft, jsondeserializationcontext context)       throws jsonparseexception {     if (!(json instanceof jsonprimitive)) {       throw new jsonparseexception("the date should string value");     }     date date = deserializetodate(json);     if (typeoft == date.class) {       return date;     } else if (typeoft == timestamp.class) {       return new timestamp(date.gettime());     } else if (typeoft == java.sql.date.class) {       return new java.sql.date(date.gettime());     } else {       throw new illegalargumentexception(getclass() + " cannot deserialize " + typeoft);     }   }    private date deserializetodate(jsonelement json) {     synchronized (localformat) {       try {         return localformat.parse(json.getasstring());       } catch (parseexception ignored) {       }       try {         return enusformat.parse(json.getasstring());       } catch (parseexception ignored) {       }       try {         return iso8601format.parse(json.getasstring());       } catch (parseexception e) {         throw new jsonsyntaxexception(json.getasstring(), e);       }     }   }    @override   public string tostring() {     stringbuilder sb = new stringbuilder();     sb.append(defaultdatetypeadapter.class.getsimplename());     sb.append('(').append(localformat.getclass().getsimplename()).append(')');     return sb.tostring();   } } 

if notice deserializetodate method, generates 3 formats,

1) local format

2) enusformat

3) iso8601format

where

enusformat = new simpledateformat("yyyy-mm-dd't'hh:mm:ss.sss'z'") and

localformat = new simpledateformat("yyyy-mm-dd't'hh:mm:ss.sss'z'", locale.us) 

both localformat , iso8601format same. value of parameter creationdate 2017-08-14to2:42:59.528z. reason, not getting parsed through either of 3 formats.

later, realized using wrong gson version, included line in build.gradle file , again ran application.

compile 'com.google.code.gson:gson:2.7' 

i ran in debug code , went through each , every step. time noticed defaultdatetypeadapter.java file got changed.

final class defaultdatetypeadapter implements jsonserializer<date>, jsondeserializer<date> {    // todo: migrate streaming adapter    private final dateformat enusformat;   private final dateformat localformat;    defaultdatetypeadapter() {     this(dateformat.getdatetimeinstance(dateformat.default, dateformat.default, locale.us),         dateformat.getdatetimeinstance(dateformat.default, dateformat.default));   }    defaultdatetypeadapter(string datepattern) {     this(new simpledateformat(datepattern, locale.us), new simpledateformat(datepattern));   }    defaultdatetypeadapter(int style) {     this(dateformat.getdateinstance(style, locale.us), dateformat.getdateinstance(style));   }    public defaultdatetypeadapter(int datestyle, int timestyle) {     this(dateformat.getdatetimeinstance(datestyle, timestyle, locale.us),         dateformat.getdatetimeinstance(datestyle, timestyle));   }    defaultdatetypeadapter(dateformat enusformat, dateformat localformat) {     this.enusformat = enusformat;     this.localformat = localformat;   }    // these methods need synchronized since jdk dateformat classes not thread-safe   // see issue 162   @override   public jsonelement serialize(date src, type typeofsrc, jsonserializationcontext context) {     synchronized (localformat) {       string dateformatasstring = enusformat.format(src);       return new jsonprimitive(dateformatasstring);     }   }    @override   public date deserialize(jsonelement json, type typeoft, jsondeserializationcontext context)       throws jsonparseexception {     if (!(json instanceof jsonprimitive)) {       throw new jsonparseexception("the date should string value");     }     date date = deserializetodate(json);     if (typeoft == date.class) {       return date;     } else if (typeoft == timestamp.class) {       return new timestamp(date.gettime());     } else if (typeoft == java.sql.date.class) {       return new java.sql.date(date.gettime());     } else {       throw new illegalargumentexception(getclass() + " cannot deserialize " + typeoft);     }   }    private date deserializetodate(jsonelement json) {     synchronized (localformat) {       try {         return localformat.parse(json.getasstring());       } catch (parseexception ignored) {}       try {         return enusformat.parse(json.getasstring());       } catch (parseexception ignored) {}       try {         return iso8601utils.parse(json.getasstring(), new parseposition(0));       } catch (parseexception e) {         throw new jsonsyntaxexception(json.getasstring(), e);       }     }   }    @override   public string tostring() {     stringbuilder sb = new stringbuilder();     sb.append(defaultdatetypeadapter.class.getsimplename());     sb.append('(').append(localformat.getclass().getsimplename()).append(')');     return sb.tostring();   } } 

if notice deserializetodate method, generated 1 different format , gson version showing 2.7.

1) localformat

2) enusformat

3) iso8601utils

this time, creationdate value got parsed through iso8601utils method.

i struggling find solution last 1 week. @ last, 1 inclusion of line in build.gradle file made difference in end.

hope explanation helps facing same issue in future.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -