Skip to content

JsonConverter not called on deserialization when set as property attribute #54181

@wadeamaral

Description

@wadeamaral

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The Read method in the custom HoursMinutesOnlyJsonConverter is never called during deserialization of a .net core API project. The Request class is being used inside a few Controllers with a [FromBody] tag.

public class Request
{
    [JsonConverter(typeof(HoursMinutesOnlyJsonConverter))]
    public TimeOnly? Time { get; set; }
}
public class Response
{
    [JsonConverter(typeof(HoursMinutesOnlyJsonConverter))]
    public TimeOnly? Time { get; set; }
}
 public class HoursMinutesOnlyJsonConverter : JsonConverter<TimeOnly?>
 {
     public override TimeOnly? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
     {
         // THIS IS NEVER BEING CALLED
         return null;
     }

     public override void Write(Utf8JsonWriter writer, TimeOnly? value, JsonSerializerOptions options)
     {
         // THIS WORKS AS EXPECTED
         if (value == null)
         {
             writer.WriteNullValue();

         }
         else
         {
             writer.WriteStringValue(((TimeOnly)value).ToString("hh:mm"));
         }
     }
 }

Expected Behavior

The custom converter should be called during deserialization.

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

8.0.101

Anything else?

No response

Activity

added
area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates
on Feb 23, 2024
maets

maets commented on Jun 17, 2025

@maets

Having the same problem. Are there any updates?

maets

maets commented on Jun 17, 2025

@maets

Solved my own issue by adding the following override to my custom converter:
public override bool HandleNull => true;

I guess it's optimized by circumventing the converter if encountering null or something like that

added this to the Backlog milestone on Jul 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @captainsafia@gfoidl@wadeamaral@maets

        Issue actions

          JsonConverter not called on deserialization when set as property attribute · Issue #54181 · dotnet/aspnetcore