© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
6 replies
redmoss

Nice ways to partially serialize and deserialize objects?

Say I've got a class like this:

class Monster 
{
  public int Id;
  public string Name;
  public int AttackRating;
  public bool Seen;
}
class Monster 
{
  public int Id;
  public string Name;
  public int AttackRating;
  public bool Seen;
}


In my program, I might have various instances of these in a List that I refer to. When I serialize this list, it saves
Id
Id
Name
Name
and
AttackRating
AttackRating
but these are instanced in the code so if I change the code, the serialization is now out of date for users who try and load the save when the code has moved on. So really, I just want to serialize the
Seen
Seen
property, because the rest is defined in code by the
MonsterFactory
MonsterFactory
.

However, I'm not sure how I can deserialise a
List<Monster>
List<Monster>
which was just serialised with the
Seen
Seen
properties serialised only, if that makes sense. The other values are instantiated by the appropriate MonsterFactory in code. Now, I can use
[JsonIgnore]
[JsonIgnore]
to serialize just
Seen
Seen
. However the problem is recreating the list of objects without overwriting the other properties using the
MonsterFactory
MonsterFactory
for the rest.

Basically I want to be able to create my
Monster
Monster
class again from the factory method, but then deserialize a
List<Monster>
List<Monster>
from file that only stored
Seen
Seen
because it's the only property without
[JsonIgnore]
[JsonIgnore]
. But when I do this, it's going to set all my other properties to
null
null
.

Any thoughts?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Serialize and Deserialize JSON using built in components
C#CC# / help
2y ago
e different names during Serialize and Deserialize [Answered]
C#CC# / help
4y ago
JsonSerializer serialize 2 objects
C#CC# / help
4y ago
✅ Ways to deserialize Json with dynamic model
C#CC# / help
3y ago