Question

I'm working with FlexJSON and am having trouble parsing a Date object from an int. I'm trying to use the JSONDeserializer like so:

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

And the value of json 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 parsed correctly unless the date of birth (dob key) value is between December 7th, 1969 and January 25th, 1970 (or -2138400000 to 2095200000 in milliseconds), then FlexJSON throws this error:

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

I'm not sure how this is happening because new Date(21600000) evaluates to Thu Jan 01 00:00:00 CST 1970.

Has anyone encountered this?



Update #1


So it appears that this error is happening because JSONDeserializer can't handle dates saved as a Unix TimeStamp that are in the range of Dec. 7th, 1969 through Jan. 25th, 1970. Any other date outside of that range is accepted and is also a Unix TimeStamp.

I don't think that I need to implement a custom ObjectFactory with .use() or create a custom Transformer because other Unix TimeStamps work that aren't in the failing date range.



Update #2


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

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

That worked exactly how it was supposed to but not upon deserialization. I'm still getting the same error:

[JSONException: [ dob ]: Parsing date 1970-01-01 was not recognized as a date format]
Was it helpful?

Solution 2

Well it's definitely a problem with Flexjson. We still weren't able to figure out the problem but my coworker managed to come up with a work-around until it's fixed. Essentially we create a new DateTransformer and designate a format to use. We then use that transformer to transform the Date.class upon serialization and make use of the transformer again with use() upon deserialization.

The 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);

OTHER TIPS

I got the same problem. Fixed by extending flexjson.factories.DateObjectFactory and overriding instantiate() method just like this.

@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 small trick

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

And then you can easily deserialized json strings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top