jsonapi-serializer
jsonapi-serializer copied to clipboard
Deserializer Option `typeForAttribute` missing
I just wanted to try the following:
new JSONApiDeserializer(
typeForAttribute: function(attribute) {
if (typeof attribute === 'string') {
const match = attribute.match(/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\dZ$/);
return match ? new Date(attribute) : undefined;
}
return undefined;
}
).deserialize(data);
But the I found out that the deserializer doesn't take this configuration option (compared to the serializer who does). Is there a specific reason for that?
My current workaround looks like this:
function couldBeDate(dateString) {
return !!dateString.match(/^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\dZ$/);
}
function deserializeDatesInplace(obj) {
Reflect.ownKeys(obj).forEach(key => {
if (typeof obj[key] === 'object') {
deserializeDatesInplace(obj[key])
}
if (typeof obj[key] === 'string' && couldBeDate(obj[key])) {
obj[key] = new Date(obj[key])
}
});
}