简体   繁体   中英

How to serialize an IEnumerable<DateOnly>?

I wrote a simple custom JsonConverter class for serializing the DateOnly, but I have no idea how to apply the converter to a collection like IEnumerable.

nothing on top needed, just serialize your IEnumerable:

var list = new List<DateOnly>() { new DateOnly(1,2,3), new DateOnly(4, 5, 6) };
IEnumerable<DateOnly> x = list.Where( x => x.Day<100 );         
var options = new JsonSerializerOptions
{           
    Converters = { new DateConverter() } 
};
var json = System.Text.Json.JsonSerializer.Serialize( x, options);
Console.WriteLine(json);

generates ["0001-02-03","0004-05-06"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM