Skip to content

Serializer Settings do not appear to be taking effect when using JSON.net #338

@c5racing

Description

@c5racing

Description

C# DateOnly is currently not supported in Newtonsoft so a custom converter needs to be applied. Even when applied, it doesn't appear to be taking effect because I receive the error: NotSupportedException: Serialization and deserialization of 'System.DateOnly' instances are not supported. The unsupported member type is located on type 'System.Object'

Steps to Reproduce

  1. Register Easy Caching with custom serializer settings
  2. Attempt to sSerialize Object with EasyCaching Provider

Related code

 public sealed class DateOnlyJsonConverter : JsonConverter<DateOnly>
   {
       public override void WriteJson(JsonWriter writer, DateOnly value, JsonSerializer serializer)
       {
           writer.WriteValue(value.ToString("O"));
       }

       public override DateOnly ReadJson(JsonReader reader, Type objectType, DateOnly existingValue, bool hasExistingValue,
           JsonSerializer serializer)
       {
           return DateOnly.FromDateTime(reader.ReadAsDateTime().Value);
       }
   }
            services.AddEasyCaching(options =>
            {
                Action<Newtonsoft.Json.JsonSerializerSettings> jsonNET = x =>
                {
                    x.Converters.Add(new DateOnlyJsonConverter());
                    x.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
                };

                options.WithJson(jsonNET, "json");

                options.UseRedis(config =>
                {
                    config.DBConfig.Endpoints.Add(new EasyCaching.Core.Configurations.ServerEndPoint("redis", 6379));
                    config.SerializerName = "json";
                }, "ConfigurationRedisDb");
            });

Expected behavior: [What you expected to happen]
The custom converter is used and DateOnly can be serialized/deserialized

Actual behavior: [What actually happened]
DateOnly cannot be serialized/deserialized

Specifications

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions