C
C#6mo ago
redmoss

How to serialize part of an object with a guid but then deserialize later?

So at runtime, I'll populate a list of objects that have a guid id and a string name and a bool isFlagged. When I serialize, I don't care about the name because it gets created again when the object is instantiated so there's no need to store that data. But when deserializing, I instantiate the list of objects again, then want to go through each one and, using the guids I saved, set the isFlagged that I had serialized. The problem is, the new instances have new guids so there's no way to map the deserialized data back to their originals. How should I approach this problem?
6 Replies
redmoss
redmossOP6mo ago
Perhaps it's because I'm storing the objects in a list, and I need to store them in a dict indexed by guid instead
Pobiega
Pobiega6mo ago
that sounds like a good idea at a glance, yes
canton7
canton76mo ago
How come the deserialized objects have new guids? If they're being deserialized, and the guid is being set to the deserialized guid, then they shouldn't have new guids...
redmoss
redmossOP6mo ago
So when I serialize, I only serialize the guid and isFlagged, because name never changes (it gets set when the objec tis constructed). But upon deserializing, I don't know how to recreate the object because I'm missing the name info, so I can't fully reconstruct the object. If I construct the objects then try to change isFlagged on each one, I don't know which ones in my dictioanry correspond to the ones in my deserialized package
canton7
canton76mo ago
I think we're missing important information. Why don't you know which objects in your dictionary (what dictionary?) correspond to the ones in your deserialized package?
Joschi
Joschi6mo ago
What as a whole are you even trying to accomplish? Why not serialise the complete object including the name. Is storage space or bandwidth a concern? If you really need to do custom work while serialising or deserialising you should take a look at JsonConverter<T> https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/converters-how-to?pivots=dotnet-8-0
How to write custom converters for JSON serialization - .NET
Learn how to create custom converters for the JSON serialization classes that are provided in the System.Text.Json namespace.
Want results from more Discord servers?
Add your server