Created
March 10, 2025 07:44
-
-
Save ufcpp/2fadd913258bfef97a0b27f15a541c34 to your computer and use it in GitHub Desktop.
System.Text.Json.Serialization の Source Generator、そういう仕様なんだ…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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