DEV Community

loading...

Discussion on: [C #] Some scenarios for deserializing a JSON to a type with read-only properties by "System.Text.Json"

Collapse
jeikabu profile image
jeikabu

This is why I've been wary of some of the recent C# changes. I understand the problems they're solving, but there's starting to be too many ways to do things. I feel the same about C++; there's a bunch of things you can do, but then you need a resource like "C++ FAQ" to detail what you should do.

Surprising or unexpected results are rarely desirable.

Collapse
abhishektripathi profile image
Abhishek Tripathi

I feel it too but a language must evolve with time. While evolving, it has two choices. Either it can introduce a breaking change which will lead to chaos for large systems trying to use the latest and the greatest, and the second approach is that it retains the old behavior unless it is a technical obstacle in moving forward. I do understand with this growth, now there is plenty more at the language level one must learn and must be aware which situation demands for which approach.

Collapse
j_sakamoto profile image
jsakamoto Author

I want to be to respect other's opinions, and also I partially agree with what you said, but you are too much afraid of it, I feel.

Pretty much cases, "constructor initialized read-only properties" immutable object pattern - and this is a very standard and classical pattern in C#, I think - works fine with "System.Text.Json" without additional coding.

And, the "no constructor and init-only properties" immutable object pattern - this is a modern pattern - will also work fine with "System.Text.Json" too. Of course, it doesn't require additional coding.

In this article, I just explained rare cases for someone who runs into the JSON deserialization problem, such as the class has multiple constructors.

I agree that there are too many ways to do something in recent C# programming, but recent C# programmers will use modern patterns, so the use cases of the classical ways will decrease.

For example, the "anonymous delegate" feature is still alive in the newest C#, but we usually use "lambda expression" instead of it today, so recent C# programmers may not know "anonymous delegate".

So, in my opinion, I'm optimistic about this C# programming topic. 😊

Collapse
sharpninja profile image
The Sharp Ninja

This is not a useful way of thinking. Everything evolves and sometimes introduction of new language features, such as init properties, shines light on the inadequacies of old techniques. In this case, private setters or no setter has been part of C# frim the beginning. Having a constructor to initialize read-inly properties has been common since day one. Auto-properties and object initializers caused a big shift in thinking and it became normal to have no constructors at all. Init setters fixes the obvious issue with that. So now you have code bases with three techniques to create a read-only class and serializers need to handle all of the. The fact that adding two attribures allows all use cases to be covered speaks volumes about the foresight of the team building C# in the 1990s.

Forem Open with the Forem app