© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
6 replies
Arkobat

Custom JSON serializer not working

Hello
I have a custom serializer i cannot get to work.
When I use the model in a controller, it just gives me null.

It is the following class I try to serialize
public class ExternalId
{
    public string Id { get; set; }
}
public class ExternalId
{
    public string Id { get; set; }
}

I want to be able to use the following input
{
    "id1": "a",
    "id2": "b",
    "id3": "3"
}
// to e.g. this class
public class MyDTO {
    public ExternalId Id1 {get; set;}
    public ExternalId Id2 {get; set;}
    public ExternalId Id3 {get; set;}
}
{
    "id1": "a",
    "id2": "b",
    "id3": "3"
}
// to e.g. this class
public class MyDTO {
    public ExternalId Id1 {get; set;}
    public ExternalId Id2 {get; set;}
    public ExternalId Id3 {get; set;}
}

This is my serializer.
Is should just take the string, and wrap it in a ExternalId
I have tried to put break points in here also, but they are never reached
public class ExternalIdSerializer : JsonConverter<ExternalId>
{
    public override ExternalId? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        return reader.GetString() is null
            ? null
            : new ExternalId(reader.GetString()!);
    }

    public override void Write(Utf8JsonWriter writer, ExternalId externalId, JsonSerializerOptions options)
    {
        writer.WriteStringValue(externalId.Id);
    }
}
public class ExternalIdSerializer : JsonConverter<ExternalId>
{
    public override ExternalId? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        return reader.GetString() is null
            ? null
            : new ExternalId(reader.GetString()!);
    }

    public override void Write(Utf8JsonWriter writer, ExternalId externalId, JsonSerializerOptions options)
    {
        writer.WriteStringValue(externalId.Id);
    }
}

And last, this is where I add my serializer
services
    .AddControllers()
    .AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
        
        var converters = options.JsonSerializerOptions.Converters;
        converters.Add(new JsonStringEnumConverter());
        converters.Add(new ExternalIdSerializer());
    });
services
    .AddControllers()
    .AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
        
        var converters = options.JsonSerializerOptions.Converters;
        converters.Add(new JsonStringEnumConverter());
        converters.Add(new ExternalIdSerializer());
    });
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

Json Serializer implicit conversion
C#CC# / help
3y ago
❔ Adding logic into a JSON serializer
C#CC# / help
4y ago
urgent ! it's related with json serializer
C#CC# / help
3y ago
✅ launch.json not working
C#CC# / help
2y ago