SeyZ / jsonapi-serializer

A Node.js framework agnostic library for (de)serializing your data to JSON API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Deserializer Option `typeForAttribute` missing

schmijos opened this issue · comments

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])
    }
  });
}