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

Deserialization of null to non-optional Record property #65

Closed
kevingentile opened this issue Jun 4, 2021 · 4 comments · Fixed by #64
Closed

Deserialization of null to non-optional Record property #65

kevingentile opened this issue Jun 4, 2021 · 4 comments · Fixed by #64

Comments

@kevingentile
Copy link
Contributor

kevingentile commented Jun 4, 2021

Refers to the test case in #64

I'm seeing a null value where I would expect an initialized/zeroed value for the property.

Possibly related to JamesNK/Newtonsoft.Json#2116

@bartelink
Copy link
Collaborator

bartelink commented Jun 8, 2021

Thanks for logging an issue, and taking the time to add a PR too. In this instance I'm not sure the best answer is to actually change the behavior of FsCodec; I'll try to explain why...

String can legitimately have null as a value. You have two choices:

  1. use string option to represent the null
  2. apply json.net rules to outlaw null for that field or in general
    • the Settings.Create has an overload for allowNulls which may or may not help
    • json.net has attributes to change the default for a type to outlaw nulls, which you can then provide exceptions to by tagging individual properties/fields

See https://github.com/jet/equinox/blob/master/src/Equinox.CosmosStore/CosmosStore.fs#L22 (I leave it in default mode but outlaw nulls that I am not handling intentionally)

Would appreciate if the PR could add a paragraph to the README to explain this technique / convention when using json.net (having a characterization test or two illustrating it too would be helpful too but from my perspective those tests should not be failing - i.e. this is by design, unless I'm really missing something)

@bartelink
Copy link
Collaborator

It should also be mentioned that FsCodec.SystemTextJson should be providing an analogous mechanism. I'm not sure how easy/possible that is, but in general FsCodec is seeking to provide a pragmatic middle way of using NSJ or STJ in F# without completely changing what one might expect to happen when using json.net in order to provide an F# only experience - i.e. the aim is to provide helpers to smooth the way for using reflection based serialization in a way that would not surprise people coming from a C# background and/or in mixed C#+F# codebases.

I hope this explains the intent a little better than the above

@bartelink
Copy link
Collaborator

@kevingentile Does this make sense to you? Can you please close the issue and PR if so and/or provide a PR that updates the README to better express the design in a manner that explains this in a way which would have saved you the confusion?

@kevingentile
Copy link
Contributor Author

Hey @bartelink, apologies for the delay. This behavior makes sense and I'll update the PR.

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

Successfully merging a pull request may close this issue.

2 participants