Skip to content

Nulls serialization #37

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

Open
breslavsky opened this issue Jul 14, 2020 · 3 comments
Open

Nulls serialization #37

breslavsky opened this issue Jul 14, 2020 · 3 comments

Comments

@breslavsky
Copy link

Primitives with null values are not presented in serialized object

@Model()
class User {
  @Field()
  firstName: string;

  @Field()
  lastName: string;
}
let admin = new User();
admin.firstName = "Anton";
admin.lastName = null;

console.log(serialize(admin));
// {firstName: "Anton"}
// but expected
// {firstName: "Anton", lastName: null}

Question is here https://github.com/AndreyZelenskiy/serialize/blob/master/src/serializers/field.utils.ts

export function isPresent(obj: Object): boolean {
  return obj !== undefined && obj !== null;
}

Perhaps remove obj !== null ?

@AndriiZelenskyi
Copy link
Owner

Hello! Glad to see you again 😄

Agreed that optional values will be nice to have. What do you think about the typing of such fields?

@breslavsky
Copy link
Author

I think, by default, we must serialize and deserialize nulls in any way. We have a case when we use serialization for graphql mutations and nulls have matter here. Please check master...breslavsky:master

I have created some fixes. It works, currently, we are using the library from https://www.npmjs.com/package/@junte/serialize-ts

Hope you we can find the solution, thanks for your job!

@AndriiZelenskyi
Copy link
Owner

Nice work! Glad to see forks.
Will do something about it in a couple of days. Thank you once again

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

2 participants