FlexJSON does not deserialize the date

I work with FlexJSON and I am having problems parsing a Date object from int . I am trying to use JSONDeserializer as follows:

 String json = decryptJson(new String(personalInformationData)); return new JSONDeserializer<PersonalInformation>().deserialize(json); 

And the json value is:

 {"address1":"123 Fake St","address2":"#4","address3":"","city":"Springfield","class":"PersonalInformation","confirmEmailAddress":" foo@bar.com ","coverageGroupName":"","coverageGroupNumber":"","coverageType":"I","dob":21600000,"emailAddress":" foo@bar.com ","firstName":"Zapp","formOfId":"D","group":false,"idNum":"K201132083220","idState":"AL","individual":true,"lastName":"Brannigan","middleInitial":"","nonUsAddress":false,"nonUsAddress1":null,"nonUsAddress2":null,"nonUsAddress3":null,"phone":"(555) 555-5555","ssn":"555555555","state":"OH","zip":"55555"} 

Everything is correctly analyzed, if only the date of birth ( dob key) is not between December 7, 1969 and January 25, 1970 (or from -2138400000 to 2095200000 in milliseconds), then FlexJSON gives this error:

 [JSONException: [ dob ]: Parsing date 21600000 was not recognized as a date format] 

I am not sure how this happens because new Date(21600000) evaluates Thu Jan 01 00:00:00 CST 1970 .

Has anyone come across this?



Update # 1


So, it seems that this error is occurring because the JSONDeserializer cannot handle dates stored as Unix TimeStamp, which are in the range from December 7, 1969 to January 25, 1970. Any other date outside this range is accepted and is also a Unix TimeStamp.

I don’t think I need to implement a custom ObjectFactory using .use() or create a custom Transformer, because other Unix TimeStamps work that are not in the failure date range.



Update # 2


I tried to implement transformer after serialization to change the date from Unix TimeStamp to a formatted date string using:

 String json = new JSONSerializer().transform(new DateTransformer("yyyy-caMM-dd"), "dob").serialize(personalInformation); 

This worked exactly as intended, but not during deserialization. I am still getting the same error:

 [JSONException: [ dob ]: Parsing date 1970-01-01 was not recognized as a date format] 
+4
source share
2 answers

Well, this is definitely a problem with Flexjson. We still could not understand the problem, but my colleague managed to come up with a workaround until it was fixed. Essentially, we create a new DateTransformer and denote the format used. Then we use this transformer to convert Date.class when serializing and again use the transformer with use() when deserializing.

DateTransformer :

 private static final DateTransformer DATE_TRANSFORMER = new DateTransformer("MM/dd/yyyy"); 

Serialization:

 String json = new JSONSerializer().transform(DATE_TRANSFORMER, Date.class).serialize(personalInformation); 

Deserialization:

 return new JSONDeserializer<PersonalInformation>().use(Date.class, DATE_TRANSFORMER).deserialize(json); 
+1
source

I have the same problem. Fixed by extending flexjson.factories.DateObjectFactory and overriding the instantiate () method.

 @Override public Object instantiate(ObjectBinder context, Object value, Type targetType, Class targetClass) { if (value instanceof Integer) { return super.instantiate(context, ((Integer) value).longValue(), targetType, targetClass); } return super.instantiate(context, value, targetType, targetClass); } 

After that just do a little trick

 JSONDeserializer<T> jsonDeserializer = new JSONDeserializer<T>().use(Date.class, new >YourExtendedDateObjectFactoryClass<) 

And then you can easily deserialize json strings.

+3
source

All Articles