Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Null value handling without using ExpandoObject #2

Open
Joelius300 opened this issue Jul 5, 2019 · 4 comments
Open

Null value handling without using ExpandoObject #2

Joelius300 opened this issue Jul 5, 2019 · 4 comments
Assignees

Comments

@Joelius300
Copy link
Owner

Joelius300 commented Jul 5, 2019

  • Here is the reason we need this in the first place described (chartJs doesn't ignore null values and it can't handle them).

  • See this question for detailed insight on how it's currently done. In the discussion with t3chb0t is some additional infos and links.

  • Here are the SerializerOptions you would use for the System.Text.Json serializer instead of json.net.

  • See this for how you can adapt to the change from json.net to System.Text.Json.

What's needed now?

  • Find out if there are CustomSerializers for System.Text.Json just like there are for json.net. If not, we cannot switch from json.net to System.Text.Json (because of the string and object enums) and have to explicitly instruct the app to use json.net in the startup (ConfigureServices). -> if there ever are (it looks like this jsonserializer stuff from .net is new in core 3 so they'll probably upgrade that) consider switching off json.net.
  • Find out if you can specify global serializer options (maybe this so question helps with that. This is however prior preview6 so we need to explicitly instruct the app to use json.net.)
  • Or just add the Attribute to the classes (you'd need that for almost every class which seems like pollution that's why there's the next point). See this so answer.
  • Determine which of the two above options is better. Maybe ask a SO question about this.
    • The first might have unexpected sideeffects when you'd have a class that gets deserialized with a property that has a default value. Say public string Name { get; set; } = "Default-Name". If you were to have an instance of that class where Name = null, you would get a json string where Name is not present. If you then deserialize that back, you would get Name = "Default-Name" because it wasn't set to null. It wasn't set at all so the default value is used which is different from the value null which was expected. Workaround for that would be that every property with a nullable datatype must not have a default value. If this is incorrect, please correct me :)
    • The second would require this attribute to be set for every single model class in the tree of config (which is a lot). I this worth it? Is this too polluted?
@Joelius300
Copy link
Owner Author

So there has been an update. I tried getting the IJSRuntime to use Json.Net. Since this didn't work I have asked on StackOverflow and done further research. I have asked the asp.net team about it and they say they won't allow users to change the serializer that IJSRuntime uses.
This means that as long as there are no custom serializers for System.Text.Json, it is impossible to implement the custom enums that currently require json.net to be serialized correctly.
When custom serializers for System.Text.Json arrive, I will update the library to not only remove all json.net dependencies but also to remove all the ugly ExpandoObject code.

@Joelius300
Copy link
Owner Author

UPDATE:

Since it (very unfortunately) won't be supported in IJSRuntime I reached out to the corefx team to see what's going on in the development for System.Text.Json (potential support for a way to work around this issue. E.g. similar functionality like JsonObject from Newtonsoft.Json).

These are the (currently) relevant links:

@Joelius300
Copy link
Owner Author

Update:

I have created a repos for the test-project I use to see how certain values are serialized. It's very helpful for solving this issue. You can find the repository here.

@SeppPenner
Copy link
Contributor

SeppPenner commented Sep 30, 2019

Is migrated to mariusmuntean/ChartJs.Blazor#39 now. Do you want to provide the details in there as well @Joelius300?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants