using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using System.Text.Json.Nodes;
public int SomethingIDoNotWant { get; set; }
public string? Value { get; set; }
[JsonConverter(typeof(NoteDeserializerNet))]
public class NoteListDecorator : IEnumerable<Note>
readonly List<Note> notes;
public NoteListDecorator(List<Note> notes) => this.notes = notes ?? throw new ArgumentNullException(nameof(notes));
public IEnumerator<Note> GetEnumerator() => notes?.GetEnumerator() ?? Enumerable.Empty<Note>().GetEnumerator();
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }
[return: System.Diagnostics.CodeAnalysis.NotNullIfNotNull("notes")]
public static implicit operator NoteListDecorator?(List<Note>? notes) => notes == null ? null : new NoteListDecorator(notes);
[return: System.Diagnostics.CodeAnalysis.NotNullIfNotNull("notes")]
public static implicit operator List<Note>?(NoteListDecorator? notes) => notes?.notes;
class NoteDeserializerNet : JsonConverter<NoteListDecorator>
public override void Write(Utf8JsonWriter writer, NoteListDecorator value, JsonSerializerOptions options) =>
JsonSerializer.Serialize(writer, value.Select(n => n.Value), options);
public override NoteListDecorator? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
JsonSerializer.Deserialize<List<string>>(ref reader, options)?.Select(s => new Note { Value = s }).ToList();
public static void Test()
new Note { Value = "hello", SomethingIDoNotWant = 1 },
new Note { Value = "there", SomethingIDoNotWant = 2 },
var json = JsonSerializer.Serialize<NoteListDecorator>(notes);
List<Note>? notes2 = JsonSerializer.Deserialize<NoteListDecorator>(json);
Assert.AreEqual("""["hello","there"]""", json);
Assert.That(notes2?.Select(n => n.Value).SequenceEqual(notes.Select(n => n.Value)) == true);
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("Failed with unhandled exception: ");