using System.Collections;
using System.Collections.Generic;
using System.Text.Json.Serialization;
public class NuGetVersionConverter : System.Text.Json.Serialization.JsonConverter<NuGetVersion>
public override NuGetVersion? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
NuGetVersion.Parse(reader.GetString());
public override void Write(Utf8JsonWriter writer, NuGetVersion value, JsonSerializerOptions options) =>
writer.WriteStringValue(value.ToString());
public class VersionRangeConverter : System.Text.Json.Serialization.JsonConverter<VersionRange>
public override VersionRange? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
VersionRange.Parse(reader.GetString());
public override void Write(Utf8JsonWriter writer, VersionRange value, JsonSerializerOptions options) =>
writer.WriteStringValue((VersionRange.Parse(value.ToString())).ToString());
public string? Name { get; set; }
public string? Id { get; set; }
[System.Text.Json.Serialization.JsonConverter(typeof(NuGetVersionConverter))]
public NuGetVersion Version { get; set; }
public string? PackageSource { get; set; }
public System.DateTimeOffset BlobCreation { get; set; }
public static void Test()
Name = "NuGet.Versioning",
Version = NuGetVersion.Parse("6.0.3-rc.1"),
BlobCreation = DateTimeOffset.Parse("2022-11-09T19:09:55.7275382+00:00"),
var options = new JsonSerializerOptions
Converters = { new NuGetVersionConverter(), new VersionRangeConverter(), new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) },
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
var json = JsonSerializer.Serialize(nuget1, options);
var nuget2 = JsonSerializer.Deserialize<Nuget>(json, options);
Assert.AreEqual(nuget1.Version, nuget2?.Version);
var json2 = Newtonsoft.Json.JsonConvert.SerializeObject(nuget1, Newtonsoft.Json.Formatting.Indented, NuGet.Protocol.JsonExtensions.ObjectSerializationSettings);
Console.WriteLine(json2);
var nuget3 = JsonSerializer.Deserialize<Nuget>(json2, options);
Assert.AreEqual(nuget1.Version, nuget3?.Version);
TestTestNuGetProtocolJsonExtensionsSettings(nuget1, json);
TestNuGetProtocolJsonExtensionsFromJson(nuget1);
static void TestTestNuGetProtocolJsonExtensionsSettings(Nuget nuget1, string json)
var nuget = Newtonsoft.Json.JsonConvert.DeserializeObject<Nuget>(json,
NuGet.Protocol.JsonExtensions.ObjectSerializationSettings);
Assert.AreEqual(nuget1.Version, nuget?.Version);
static void TestNuGetProtocolJsonExtensionsFromJson(Nuget nuget1)
var json = NuGet.Protocol.JsonExtensions.ToJson(nuget1);
var nuget = NuGet.Protocol.JsonExtensions.FromJson<Nuget>(json);
Assert.AreEqual(nuget1.Version, nuget?.Version);
public static void Main()
Console.WriteLine("Environment version: {0} ({1}), {2}", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
Console.WriteLine("System.Text.Json version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("{0} version: {1}", typeof(NuGetVersion).Assembly.GetName().Name, typeof(NuGetVersion).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");