-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathDateJsonConverter.cs
33 lines (29 loc) · 1.25 KB
/
DateJsonConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
namespace ShipEngineSDK;
using System;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
/// <summary>
/// Converts a Date to and from JSON
/// </summary>
public class DateJsonConverter : JsonConverter<DateTime>
{
private const string Format = "yyyy-MM-dd";
/// <summary>
/// Read a Date from a JSON string
/// </summary>
/// <param name="reader">JSON reader</param>
/// <param name="typeToConvert">Object type to convert</param>
/// <param name="options">Serializer options</param>
/// <returns>The object converted from the JSON string</returns>
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
DateTime.ParseExact(reader.GetString()!, Format, CultureInfo.InvariantCulture);
/// <summary>
/// Write a Date to a JSON string
/// </summary>
/// <param name="writer">JSON writer</param>
/// <param name="value">Object to be converted into a JSON string</param>
/// <param name="options">Serializer options</param>
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) =>
writer.WriteStringValue(value.ToString(Format, CultureInfo.InvariantCulture));
}