C
C#3w ago
MaggyD

Idiomatic C# Data Objects with Immutable Collections?

Hello, I'm trying to understand the best way to define a class that holds collection of some data that is completely immutable, yet serializable and deserilazable by System.Text.Json. For instance, I want this class to just represent a JSON document from the front-end application, it can be seen as a form of DTO as some sort. However, after spending 30 minutes online and asking ChatGPT, I feel like I still don't have a clear guide on how to construct a class with collections. I have tried multiple things in my experience with C#. Backing fields. Just making them mutable and ignoring the warnings. Some combination of the previous two??? Please help. Thanks, Maguire
12 Replies
ero
ero3w ago
use some form of readonly collection interface IReadOnlyCollection, IReadOnlyList
MaggyD
MaggyDOP3w ago
Yup...
ero
ero3w ago
so? does that answer the question? or did i misunderstand something the collections are immutable
MaggyD
MaggyDOP3w ago
I guess I'm just struggling with a different thing, not really related to this question I just honestly don't understand why I keep running into issues with creating classes and records that don't deserialize or serialize in a predictable way I've been writing C# for 3 years... I still can't get this right
ero
ero3w ago
you could provide some examples, what's going wrong and what you expect
MaggyD
MaggyDOP3w ago
Ok, so I have a JSON object that I want to deserialize into a class. Previously, we serialized this class with a property say: Padding is a nullable immutable struct propety. Now, I want to change that! I want Padding to never be nullable, however, if can deserialize a null by providing a default value. HOW do I do this? I thought this would work... ~~
public Padding Padding { get; set; } = Padding.Default;
public Padding Padding { get; set; } = Padding.Default;
Apparently, it does not.... which is bullshit
ero
ero3w ago
i'm not sure what you mean? null can't be deserialized to a property that cannot be null well, for structs anyway
MaggyD
MaggyDOP3w ago
????
ero
ero3w ago
hm?
MaggyD
MaggyDOP3w ago
But it has a default value... I'm saying, use Padding.Default
ero
ero3w ago
that doesn't really matter. if your json explicitly says "this value should be null when deserialized", then you cannot expect the serializer to know "oh i guess i should just ignore this". i'm not sure whether there are any available ways to just "ignore" null values in a given json string. research suggests no and you need to use a converter. the only easy way to do this is to simply omit the value from the json string
SleepWellPupper
If you're on net9 and using STJ, you could utilize nullable annotations (that is, the absence of) to instruct the serializer: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/nullable-annotations
Respect nullable annotations - .NET
Learn how to configure serialization and deserialization to respect nullable annotations.
Want results from more Discord servers?
Add your server