Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserializer Option typeForAttribute missing #206

Open
schmijos opened this issue Jul 4, 2019 · 0 comments
Open

Deserializer Option typeForAttribute missing #206

schmijos opened this issue Jul 4, 2019 · 0 comments

Comments

@schmijos
Copy link

schmijos commented Jul 4, 2019

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])
    }
  });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant