Skip to content

Instantly share code, notes, and snippets.

@ufcpp
Created March 10, 2025 07:44
Show Gist options
  • Save ufcpp/2fadd913258bfef97a0b27f15a541c34 to your computer and use it in GitHub Desktop.
Save ufcpp/2fadd913258bfef97a0b27f15a541c34 to your computer and use it in GitHub Desktop.
System.Text.Json.Serialization の Source Generator、そういう仕様なんだ…
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
IJsonTypeInfoResolver r = new SourceGenerationContext();
Console.WriteLine(r.GetTypeInfo(typeof((One, TwoThree)), new()) is null); // not null
Console.WriteLine(r.GetTypeInfo(typeof((OneTwo, Three)), new()) is null); // null
// タプルのとき、 source generator が作るメソッドの名前は Create_Value{T1}{T2} らしく…
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof((One, TwoThree)))] // これと
[JsonSerializable(typeof((OneTwo, Three)))] // これはどっちも Create_ValueTupleOneTwoThree になって名前被り。
internal partial class SourceGenerationContext : JsonSerializerContext
{
}
// ↑名前がかぶると最初の1個以外無視されてるっぽい。
struct One;
struct TwoThree;
struct OneTwo;
struct Three;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment