using NodaTime.Serialization.SystemTextJson;
using System.Collections.Generic;
using System.Text.Json.Serialization;
public static void ConfigureJsonOptions(JsonSerializerOptions options)
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.IgnoreNullValues = true;
options.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
ReplaceExistingConverters<Instant>(converters: options.Converters,
newConverter: new NodaPatternConverter<Instant>(InstantPattern.ExtendedIso));
private static void ReplaceExistingConverters<T>(
IList<JsonConverter> converters,
JsonConverter newConverter)
int index = converters.Count - 1;
if (converters[index].CanConvert(typeof(T)))
converters.RemoveAt(index);
converters.Add(newConverter);
public static void Main()
var jsonSerializerOptions = new JsonSerializerOptions();
jsonSerializerOptions.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
var instant1 = SystemClock.Instance.GetCurrentInstant();
var instant2 = Instant.FromUnixTimeSeconds(instant1.ToUnixTimeSeconds());
Console.WriteLine(instant1);
Console.WriteLine(instant2);
Test.ConfigureJsonOptions(jsonSerializerOptions);
var x = new { Time1 = instant1, Time2 = instant2 };
var result = System.Text.Json.JsonSerializer.Serialize(x, jsonSerializerOptions);
Console.WriteLine(result);