System.DateTimeOnly icon indicating copy to clipboard operation
System.DateTimeOnly copied to clipboard

RestSharp Json format as YYYY-MM-DD / HH:mm:ss

Open deg-mallard opened this issue 1 year ago • 1 comments

We are using a combination of DateOnly and RestSharp for our API environment.

When serializing the DateOnly object in .Net Framework it comes out as { "Year": 2024, "Month": 9, "Day": 2........}

Is there anyway to get it to serialize as "YYYY-MM-DD"?

deg-mallard avatar Sep 02 '24 15:09 deg-mallard

AFAIK the latest version of RestSharp uses the System.Text.Json package as its default JSON serialization/deserialization library.

Fortunately, we already have a companion NuGet package Portable.System.DateTimeOnly.Json that contains converters and attributes that you can use with this library. So, you must apply the System.Text.Json.DateOnlyConverterAttribute attribute from this package to your DateOnly properties/fields.

If you use RestSharp in the old way with the Newtonsoft.Json package as a JSON serialization/deserialization library, you have to implement the converter manually. See more information about it in the #71 discussion thread.

OlegRa avatar Sep 16 '24 06:09 OlegRa

@all-contributors please add @deg-mallard for bug

OlegRa avatar Nov 14 '24 06:11 OlegRa

@OlegRa

I've put up a pull request to add @deg-mallard! :tada:

allcontributors[bot] avatar Nov 14 '24 06:11 allcontributors[bot]

I close this one due to inactivity. @deg-mallard, if you still have questions about RestSharp interoperability, please re-open this issue or create another one with detailed reproducing steps.

OlegRa avatar Nov 14 '24 06:11 OlegRa