I recently came accross this issue with Datetime
deserialization.
When using JSON.deserialize()
with a random numeric value, the Datetime
instance is created in a strange format, however, for the same numeric value input, Datetime.parse()
throws an exception:
Code:
Datetime dt = (Datetime) JSON.deserialize('96598', Datetime.class);
System.debug(dt);
System.debug(Datetime.parse('96598'));
Output:
14:49:35.28 (31544333)|USER_DEBUG|[2]|DEBUG|96598-01-01 00:00:00
Error on line 3, column 1: System.TypeException: Invalid date/time: 96598
AnonymousBlock: line 3, column 1
Also, when trying to insert an SObject with the "deserialized" 96598-01-01 00:00:00
value, I get and DmlException FIELD_INTEGRITY_EXCEPTION
.
Any ideas on how to properly validate this instead of having to manually check the input String when deserializing to a Datetime
field?