C
C#•3y ago
Arch Leaders

Custom Class Serialization

Is it possible to use a custom JSON serializer on a class? For example, I have a class with a few properties (Member), and another class with the class "Member" as a child property. I want the JSON serializer (Microsoft's version, System.Texts.Json) to serialize the entire Member class as just a string. Anyone know how this is done/if it can be done? Thanks.
class Report
{
string Name { get; set; }
string Id { get; set; }
Member Member { get; set; }
}

class Member
{
string Name {get; set; }
string Id {get; set; }

void CustomSerializer()
{
return Id;
}
}
class Report
{
string Name { get; set; }
string Id { get; set; }
Member Member { get; set; }
}

class Member
{
string Name {get; set; }
string Id {get; set; }

void CustomSerializer()
{
return Id;
}
}
{
"Report": {
"Name": "Anything",
"Id": "9124",
"Member": "1832"
}
}
{
"Report": {
"Name": "Anything",
"Id": "9124",
"Member": "1832"
}
}
4 Replies
Arch Leaders
Arch LeadersOP•3y ago
Thanks
Arch Leaders
Arch LeadersOP•3y ago
Any idea what could be causing this?
Arch Leaders
Arch LeadersOP•3y ago
Apparently it's not supported in Visual Basic :(
Arch Leaders
Arch LeadersOP•3y ago
Funny cause like the rest of the language is totally chill with everything else C#. Oh well, guess I'll have to find another way or look at Newtonsoft. Thanks again 👋
Want results from more Discord servers?
Add your server