Skip to content

Commit

Permalink
Added support for serializing dateonly items
Browse files Browse the repository at this point in the history
  • Loading branch information
mivano committed Jun 13, 2023
1 parent ad0a346 commit c27c154
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/OutputFormatters/JsonOutputFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using AzureCostCli.CostApi;
using DevLab.JmesPath;
using Spectre.Console;
Expand Down Expand Up @@ -70,7 +71,14 @@ public override Task WriteDailyCost(DailyCostSettings settings, IEnumerable<Cost

private static void WriteJson(CostSettings settings, object items)
{
var json = JsonSerializer.Serialize(items, new JsonSerializerOptions { WriteIndented = true });

var options = new JsonSerializerOptions { WriteIndented = true };

#if NET6_0
options.Converters.Add(new DateOnlyJsonConverter());
#endif

var json = JsonSerializer.Serialize(items, options );

if (!string.IsNullOrWhiteSpace(settings.Query))
{
Expand Down Expand Up @@ -100,4 +108,18 @@ private static void WriteJson(CostSettings settings, object items)
}


}

public sealed class DateOnlyJsonConverter : JsonConverter<DateOnly>
{
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateOnly.FromDateTime(reader.GetDateTime());
}

public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
{
var isoDate = value.ToString("O");
writer.WriteStringValue(isoDate);
}
}

0 comments on commit c27c154

Please sign in to comment.