C
C#15mo ago
Kiel

❔ System.Text.Json deserializing and serializing polymorphic classes WITHOUT a discriminator

I have a base class (record) and two derived records:
public abstract record LfsResponseObject(
string Hash,
long Size);

public record LfsResponseDataObject(
string Hash,
long Size,
IReadOnlyDictionary<string, LfsResponseObjectAction> Actions,
bool? Authenticated = null) : LfsResponseObject(Hash, Size);

public record LfsResponseErrorObject(
string Hash,
long Size,
LfsObjectError Error) : LfsResponseObject(Hash, Size)
public abstract record LfsResponseObject(
string Hash,
long Size);

public record LfsResponseDataObject(
string Hash,
long Size,
IReadOnlyDictionary<string, LfsResponseObjectAction> Actions,
bool? Authenticated = null) : LfsResponseObject(Hash, Size);

public record LfsResponseErrorObject(
string Hash,
long Size,
LfsObjectError Error) : LfsResponseObject(Hash, Size)
I need to be able to serialize these in a JSON API response, and I cannot utilize discriminators because I am adhering to the git-lfs API spec. Is my only option to create a custom JsonConverter? I don't remember Newtonsoft.Json having these kinds of issues... Edit. I can't seem to close posts because @Accord is down. I've resolved this issue by going about it a different way.
1 Reply
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.