0

Like many others already had the problem, I'm having problem with serializing a JSON from string/object to DateOnly.

I had already tried to implement solutions that were given in these posts. I also use Newtonsoft.Json.

https://github.com/JamesNK/Newtonsoft.Json/issues/2521

Thats my current DTO

using Newtonsoft.Json;
using JsonSerializer = Newtonsoft.Json.JsonSerializer;

namespace ......;

public class PostDto
{
    public PostDto(
        DateOnly postDate
    )
    {
        PostDate = postDate;
    }

    [JsonConverter(typeof(DateOnlyJsonConverter))]
    public DateOnly PostDate { get; }
}

If I build the converter like this, I get the problem that reader.Value can be null. If I bypass the warning, which I do, I get this error.

public class DateOnlyJsonConverter : JsonConverter<DateOnly>
{
    private const string Format = "yyyy-MM-dd";

    public override void WriteJson(JsonWriter writer, DateOnly value, JsonSerializer serializer) =>
        writer.WriteValue(value.ToString(Format, CultureInfo.InvariantCulture));

    public override DateOnly ReadJson(
        JsonReader reader, Type objectType, DateOnly existingValue, bool hasExistingValue,
        JsonSerializer serializer) => DateOnly.ParseExact(((string)
        reader.Value)!, Format, CultureInfo.InvariantCulture);
}

System.ArgumentNullException: Value cannot be null. (Parameter 's') at System.DateOnly.ParseExact(String s, String format, IFormatProvider provider, DateTimeStyles style)

The JSON I am Posting

   "postDate": {
      "year": 2020,
      "month": 10,
      "day": 10,
      "dayOfWeek": 0
    },

Program.cs

builder.Services.AddControllers().AddNewtonsoftJson();
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
maxfromgermany
  • 239
  • 4
  • 17
  • It seems you want `DateOnly?` not `DateOnly`, and then you need to handle `null`. – ProgrammingLlama Jul 14 '22 at 07:30
  • what do you mean with: It seems you want `DateOnly`? not `DateOnly`, – maxfromgermany Jul 14 '22 at 07:31
  • `DateOnly?` as in `Nullable`. Since you've said that the value in your JSON can be `null`, you have to do something about that in .NET. You either need to use a default value, or reflect that `null` value. – ProgrammingLlama Jul 14 '22 at 07:32
  • @DiplomacyNotWar which line of code do you mean exactly. And how exactly do I deal with it? – maxfromgermany Jul 14 '22 at 07:34
  • It was explicitly said on github, this is the solution. I wonder how it works for them. – maxfromgermany Jul 14 '22 at 07:36
  • Can you please share json which you are posting? How do you enable `Newtonsoft.Json`? – Guru Stron Jul 14 '22 at 07:40
  • to use this converter you should post data as `"postDate":"2020-10-10"` – Guru Stron Jul 14 '22 at 07:43
  • And please post the corresponding controller method code. Cause this will non work for Minimal APIs - see [this](https://stackoverflow.com/questions/69850917/how-to-configure-newtonsoftjson-with-minimalapi-in-net-6-0/69867815#69867815) – Guru Stron Jul 14 '22 at 07:44
  • Is there a better way to get rid of the possible nullable error? – maxfromgermany Jul 14 '22 at 07:47
  • @maxfromgermany value can be null (cause nothing prevents it from being one if somebody does not post value for it). So it is up to you how to handle this cases. You can throw an exception or provide a default value after checking if the string is null. – Guru Stron Jul 14 '22 at 07:52

1 Answers1

0

To use the converter from this answer you need to post data in correct format. The this DateOnlyJsonConverter specifies it "yyyy-MM-dd" so your post body should look like:

{
   "postDate": "2020-10-10"
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132