Skip to content

Commit b9be494

Browse files
authored
Support JsonExtensionData in STJ source-gen (#58912)
* Support JsonExtensionData in STJ source-gen * Address review feedback
1 parent 096e3c4 commit b9be494

40 files changed

+2163
-1586
lines changed

src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs

+19-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ private sealed partial class Emitter
4848
private const string IListTypeRef = "global::System.Collections.Generic.IList";
4949
private const string KeyValuePairTypeRef = "global::System.Collections.Generic.KeyValuePair";
5050
private const string ListTypeRef = "global::System.Collections.Generic.List";
51-
private const string DictionaryTypeRef = "global::System.Collections.Generic.Dictionary";
5251
private const string JsonEncodedTextTypeRef = "global::System.Text.Json.JsonEncodedText";
5352
private const string JsonNamingPolicyTypeRef = "global::System.Text.Json.JsonNamingPolicy";
5453
private const string JsonSerializerTypeRef = "global::System.Text.Json.JsonSerializer";
@@ -253,6 +252,12 @@ private void GenerateTypeInfo(TypeGenerationSpec typeGenerationSpec)
253252
GenerateTypeInfo(spec.TypeGenerationSpec);
254253
}
255254
}
255+
256+
TypeGenerationSpec? extPropTypeSpec = typeGenerationSpec.ExtensionDataPropertyTypeSpec;
257+
if (extPropTypeSpec != null)
258+
{
259+
GenerateTypeInfo(extPropTypeSpec);
260+
}
256261
}
257262
break;
258263
case ClassType.KnownUnsupportedType:
@@ -431,9 +436,18 @@ private string GenerateForCollection(TypeGenerationSpec typeGenerationSpec)
431436
CollectionType collectionType = typeGenerationSpec.CollectionType;
432437

433438
string typeRef = typeGenerationSpec.TypeRef;
434-
string createObjectFuncArg = typeGenerationSpec.ConstructionStrategy == ObjectConstructionStrategy.ParameterlessConstructor
435-
? $"createObjectFunc: () => new {typeRef}()"
436-
: "createObjectFunc: null";
439+
440+
string createObjectFuncArg;
441+
if (typeGenerationSpec.RuntimeTypeRef != null)
442+
{
443+
createObjectFuncArg = $"createObjectFunc: () => new {typeGenerationSpec.RuntimeTypeRef}()";
444+
}
445+
else
446+
{
447+
createObjectFuncArg = typeGenerationSpec.ConstructionStrategy == ObjectConstructionStrategy.ParameterlessConstructor
448+
? $"createObjectFunc: () => new {typeRef}()"
449+
: "createObjectFunc: null";
450+
}
437451

438452
string collectionInfoCreationPrefix = collectionType switch
439453
{
@@ -741,6 +755,7 @@ private string GeneratePropMetadataInitFunc(TypeGenerationSpec typeGenerationSpe
741755
{setterNamedArg},
742756
{ignoreConditionNamedArg},
743757
hasJsonInclude: {ToCSharpKeyword(memberMetadata.HasJsonInclude)},
758+
isExtensionData: {ToCSharpKeyword(memberMetadata.IsExtensionData)},
744759
numberHandling: {GetNumberHandlingAsStr(memberMetadata.NumberHandling)},
745760
propertyName: ""{clrPropertyName}"",
746761
{jsonPropertyNameNamedArg});

src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs

+146-47
Large diffs are not rendered by default.

src/libraries/System.Text.Json/gen/PropertyGenerationSpec.cs

+5
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ internal sealed class PropertyGenerationSpec
7575
/// </summary>
7676
public bool HasJsonInclude { get; init; }
7777

78+
/// <summary>
79+
/// Whether the property has the JsonExtensionDataAttribute.
80+
/// </summary>
81+
public bool IsExtensionData { get; init; }
82+
7883
/// <summary>
7984
/// Generation specification for the property's type.
8085
/// </summary>

src/libraries/System.Text.Json/gen/Resources/Strings.resx

+12
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,16 @@
141141
<data name="MultipleJsonConstructorAttributeTitle" xml:space="preserve">
142142
<value>Type has multiple constructors annotated with JsonConstructorAttribute.</value>
143143
</data>
144+
<data name="MultipleJsonExtensionDataAttributeFormat" xml:space="preserve">
145+
<value>Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</value>
146+
</data>
147+
<data name="MultipleJsonExtensionDataAttributeTitle" xml:space="preserve">
148+
<value>Type has multiple members annotated with JsonExtensionDataAttribute.</value>
149+
</data>
150+
<data name="DataExtensionPropertyInvalidFormat" xml:space="preserve">
151+
<value>The data extension property '{0}.{1}' is invalid. It must implement 'IDictionary&lt;string, JsonElement&gt;' or 'IDictionary&lt;string, object&gt;', or be 'JsonObject'.</value>
152+
</data>
153+
<data name="DataExtensionPropertyInvalidTitle" xml:space="preserve">
154+
<value>Data extension property type invalid.</value>
155+
</data>
144156
</root>

src/libraries/System.Text.Json/gen/Resources/xlf/Strings.cs.xlf

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
<target state="translated">Odvozené typy JsonSerializerContext a všechny obsahující typy musí být částečné.</target>
1313
<note />
1414
</trans-unit>
15+
<trans-unit id="DataExtensionPropertyInvalidFormat">
16+
<source>The data extension property '{0}.{1}' is invalid. It must implement 'IDictionary&lt;string, JsonElement&gt;' or 'IDictionary&lt;string, object&gt;', or be 'JsonObject'.</source>
17+
<target state="new">The data extension property '{0}.{1}' is invalid. It must implement 'IDictionary&lt;string, JsonElement&gt;' or 'IDictionary&lt;string, object&gt;', or be 'JsonObject'.</target>
18+
<note />
19+
</trans-unit>
20+
<trans-unit id="DataExtensionPropertyInvalidTitle">
21+
<source>Data extension property type invalid.</source>
22+
<target state="new">Data extension property type invalid.</target>
23+
<note />
24+
</trans-unit>
1525
<trans-unit id="DuplicateTypeNameMessageFormat">
1626
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
1727
<target state="translated">Existuje několik typů s názvem {0}. Zdroj se vygeneroval pro první zjištěný typ. Tuto kolizi vyřešíte pomocí JsonSerializableAttribute.TypeInfoPropertyName.</target>
@@ -32,6 +42,16 @@
3242
<target state="translated">Typ obsahuje více konstruktorů anotovaných s JsonConstructorAttribute.</target>
3343
<note />
3444
</trans-unit>
45+
<trans-unit id="MultipleJsonExtensionDataAttributeFormat">
46+
<source>Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</source>
47+
<target state="new">Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</target>
48+
<note />
49+
</trans-unit>
50+
<trans-unit id="MultipleJsonExtensionDataAttributeTitle">
51+
<source>Type has multiple members annotated with JsonExtensionDataAttribute.</source>
52+
<target state="new">Type has multiple members annotated with JsonExtensionDataAttribute.</target>
53+
<note />
54+
</trans-unit>
3555
<trans-unit id="TypeNotSupportedMessageFormat">
3656
<source>Did not generate serialization metadata for type '{0}'.</source>
3757
<target state="translated">Nevygenerovala se metadata serializace pro typ {0}.</target>

src/libraries/System.Text.Json/gen/Resources/xlf/Strings.de.xlf

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
<target state="translated">Abgeleitete JsonSerializerContext-Typen und alle enthaltenden Typen müssen partiell sein.</target>
1313
<note />
1414
</trans-unit>
15+
<trans-unit id="DataExtensionPropertyInvalidFormat">
16+
<source>The data extension property '{0}.{1}' is invalid. It must implement 'IDictionary&lt;string, JsonElement&gt;' or 'IDictionary&lt;string, object&gt;', or be 'JsonObject'.</source>
17+
<target state="new">The data extension property '{0}.{1}' is invalid. It must implement 'IDictionary&lt;string, JsonElement&gt;' or 'IDictionary&lt;string, object&gt;', or be 'JsonObject'.</target>
18+
<note />
19+
</trans-unit>
20+
<trans-unit id="DataExtensionPropertyInvalidTitle">
21+
<source>Data extension property type invalid.</source>
22+
<target state="new">Data extension property type invalid.</target>
23+
<note />
24+
</trans-unit>
1525
<trans-unit id="DuplicateTypeNameMessageFormat">
1626
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
1727
<target state="translated">Es sind mehrere Typen namens "{0}" vorhanden. Die Quelle wurde für den ersten festgestellten Typ generiert. Verwenden Sie "JsonSerializableAttribute.TypeInfoPropertyName", um diesen Konflikt zu beheben.</target>
@@ -32,6 +42,16 @@
3242
<target state="translated">Der Typ weist mehrere Konstruktoren mit dem Kommentar JsonConstructorAttribute auf.</target>
3343
<note />
3444
</trans-unit>
45+
<trans-unit id="MultipleJsonExtensionDataAttributeFormat">
46+
<source>Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</source>
47+
<target state="new">Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</target>
48+
<note />
49+
</trans-unit>
50+
<trans-unit id="MultipleJsonExtensionDataAttributeTitle">
51+
<source>Type has multiple members annotated with JsonExtensionDataAttribute.</source>
52+
<target state="new">Type has multiple members annotated with JsonExtensionDataAttribute.</target>
53+
<note />
54+
</trans-unit>
3555
<trans-unit id="TypeNotSupportedMessageFormat">
3656
<source>Did not generate serialization metadata for type '{0}'.</source>
3757
<target state="translated">Die Serialisierungsmetadaten für den Typ "{0}" wurden nicht generiert.</target>

src/libraries/System.Text.Json/gen/Resources/xlf/Strings.es.xlf

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
<target state="translated">Los tipos derivados "JsonSerializerContext" y todos los tipos que contienen deben ser parciales.</target>
1313
<note />
1414
</trans-unit>
15+
<trans-unit id="DataExtensionPropertyInvalidFormat">
16+
<source>The data extension property '{0}.{1}' is invalid. It must implement 'IDictionary&lt;string, JsonElement&gt;' or 'IDictionary&lt;string, object&gt;', or be 'JsonObject'.</source>
17+
<target state="new">The data extension property '{0}.{1}' is invalid. It must implement 'IDictionary&lt;string, JsonElement&gt;' or 'IDictionary&lt;string, object&gt;', or be 'JsonObject'.</target>
18+
<note />
19+
</trans-unit>
20+
<trans-unit id="DataExtensionPropertyInvalidTitle">
21+
<source>Data extension property type invalid.</source>
22+
<target state="new">Data extension property type invalid.</target>
23+
<note />
24+
</trans-unit>
1525
<trans-unit id="DuplicateTypeNameMessageFormat">
1626
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
1727
<target state="translated">Hay varios tipos denominados {0}. El origen se generó para el primero detectado. Use "JsonSerializableAttribute.TypeInfoPropertyName" para resolver esta colisión.</target>
@@ -32,6 +42,16 @@
3242
<target state="translated">El tipo tiene varios constructores anotados con JsonConstructorAttribute.</target>
3343
<note />
3444
</trans-unit>
45+
<trans-unit id="MultipleJsonExtensionDataAttributeFormat">
46+
<source>Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</source>
47+
<target state="new">Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</target>
48+
<note />
49+
</trans-unit>
50+
<trans-unit id="MultipleJsonExtensionDataAttributeTitle">
51+
<source>Type has multiple members annotated with JsonExtensionDataAttribute.</source>
52+
<target state="new">Type has multiple members annotated with JsonExtensionDataAttribute.</target>
53+
<note />
54+
</trans-unit>
3555
<trans-unit id="TypeNotSupportedMessageFormat">
3656
<source>Did not generate serialization metadata for type '{0}'.</source>
3757
<target state="translated">No generó metadatos de serialización para el tipo '{0}".</target>

src/libraries/System.Text.Json/gen/Resources/xlf/Strings.fr.xlf

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
<target state="translated">Les types dérivés 'JsonSerializerContext' et tous les types conteneurs doivent être partiels.</target>
1313
<note />
1414
</trans-unit>
15+
<trans-unit id="DataExtensionPropertyInvalidFormat">
16+
<source>The data extension property '{0}.{1}' is invalid. It must implement 'IDictionary&lt;string, JsonElement&gt;' or 'IDictionary&lt;string, object&gt;', or be 'JsonObject'.</source>
17+
<target state="new">The data extension property '{0}.{1}' is invalid. It must implement 'IDictionary&lt;string, JsonElement&gt;' or 'IDictionary&lt;string, object&gt;', or be 'JsonObject'.</target>
18+
<note />
19+
</trans-unit>
20+
<trans-unit id="DataExtensionPropertyInvalidTitle">
21+
<source>Data extension property type invalid.</source>
22+
<target state="new">Data extension property type invalid.</target>
23+
<note />
24+
</trans-unit>
1525
<trans-unit id="DuplicateTypeNameMessageFormat">
1626
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
1727
<target state="translated">Plusieurs types nommés {0}. La source a été générée pour la première détection détectée. Utilisez « JsonSerializableAttribute.TypeInfoPropertyName » pour résoudre cette collision.</target>
@@ -32,6 +42,16 @@
3242
<target state="translated">Le type a plusieurs constructeurs annotés avec JsonConstructorAttribute.</target>
3343
<note />
3444
</trans-unit>
45+
<trans-unit id="MultipleJsonExtensionDataAttributeFormat">
46+
<source>Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</source>
47+
<target state="new">Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</target>
48+
<note />
49+
</trans-unit>
50+
<trans-unit id="MultipleJsonExtensionDataAttributeTitle">
51+
<source>Type has multiple members annotated with JsonExtensionDataAttribute.</source>
52+
<target state="new">Type has multiple members annotated with JsonExtensionDataAttribute.</target>
53+
<note />
54+
</trans-unit>
3555
<trans-unit id="TypeNotSupportedMessageFormat">
3656
<source>Did not generate serialization metadata for type '{0}'.</source>
3757
<target state="translated">Les métadonnées de sérialisation pour le type « {0} » n’ont pas été générées.</target>

src/libraries/System.Text.Json/gen/Resources/xlf/Strings.it.xlf

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
<target state="translated">I tipi derivati 'JsonSerializerContext' e tutti i tipi contenenti devono essere parziali.</target>
1313
<note />
1414
</trans-unit>
15+
<trans-unit id="DataExtensionPropertyInvalidFormat">
16+
<source>The data extension property '{0}.{1}' is invalid. It must implement 'IDictionary&lt;string, JsonElement&gt;' or 'IDictionary&lt;string, object&gt;', or be 'JsonObject'.</source>
17+
<target state="new">The data extension property '{0}.{1}' is invalid. It must implement 'IDictionary&lt;string, JsonElement&gt;' or 'IDictionary&lt;string, object&gt;', or be 'JsonObject'.</target>
18+
<note />
19+
</trans-unit>
20+
<trans-unit id="DataExtensionPropertyInvalidTitle">
21+
<source>Data extension property type invalid.</source>
22+
<target state="new">Data extension property type invalid.</target>
23+
<note />
24+
</trans-unit>
1525
<trans-unit id="DuplicateTypeNameMessageFormat">
1626
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
1727
<target state="translated">Sono presenti più tipi denominati {0}. L'origine è stata generata per il primo tipo rilevato. Per risolvere questa collisione, usare 'JsonSerializableAttribute.TypeInfoPropertyName'.</target>
@@ -32,6 +42,16 @@
3242
<target state="translated">Il tipo contiene più costruttori che presentano l'annotazione JsonConstructorAttribute.</target>
3343
<note />
3444
</trans-unit>
45+
<trans-unit id="MultipleJsonExtensionDataAttributeFormat">
46+
<source>Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</source>
47+
<target state="new">Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</target>
48+
<note />
49+
</trans-unit>
50+
<trans-unit id="MultipleJsonExtensionDataAttributeTitle">
51+
<source>Type has multiple members annotated with JsonExtensionDataAttribute.</source>
52+
<target state="new">Type has multiple members annotated with JsonExtensionDataAttribute.</target>
53+
<note />
54+
</trans-unit>
3555
<trans-unit id="TypeNotSupportedMessageFormat">
3656
<source>Did not generate serialization metadata for type '{0}'.</source>
3757
<target state="translated">Non sono stati generati metadati di serializzazione per il tipo '{0}'.</target>

src/libraries/System.Text.Json/gen/Resources/xlf/Strings.ja.xlf

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
<target state="translated">派生した 'JsonSerializerContext' 型と含まれているすべての型は部分的である必要があります。</target>
1313
<note />
1414
</trans-unit>
15+
<trans-unit id="DataExtensionPropertyInvalidFormat">
16+
<source>The data extension property '{0}.{1}' is invalid. It must implement 'IDictionary&lt;string, JsonElement&gt;' or 'IDictionary&lt;string, object&gt;', or be 'JsonObject'.</source>
17+
<target state="new">The data extension property '{0}.{1}' is invalid. It must implement 'IDictionary&lt;string, JsonElement&gt;' or 'IDictionary&lt;string, object&gt;', or be 'JsonObject'.</target>
18+
<note />
19+
</trans-unit>
20+
<trans-unit id="DataExtensionPropertyInvalidTitle">
21+
<source>Data extension property type invalid.</source>
22+
<target state="new">Data extension property type invalid.</target>
23+
<note />
24+
</trans-unit>
1525
<trans-unit id="DuplicateTypeNameMessageFormat">
1626
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
1727
<target state="translated">{0} と名前が付けられた種類が複数あります。最初に検出されたものに対してソースが生成されました。この問題を解決するには、'JsonSerializableAttribute.TypeInfoPropertyName' を使用します。</target>
@@ -32,6 +42,16 @@
3242
<target state="translated">型には、JsonConstructorAttribute で注釈が付けられた複数のコンストラクターがあります。</target>
3343
<note />
3444
</trans-unit>
45+
<trans-unit id="MultipleJsonExtensionDataAttributeFormat">
46+
<source>Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</source>
47+
<target state="new">Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</target>
48+
<note />
49+
</trans-unit>
50+
<trans-unit id="MultipleJsonExtensionDataAttributeTitle">
51+
<source>Type has multiple members annotated with JsonExtensionDataAttribute.</source>
52+
<target state="new">Type has multiple members annotated with JsonExtensionDataAttribute.</target>
53+
<note />
54+
</trans-unit>
3555
<trans-unit id="TypeNotSupportedMessageFormat">
3656
<source>Did not generate serialization metadata for type '{0}'.</source>
3757
<target state="translated">'{0}'型 のシリアル化メタデータを生成ませんでした。</target>

0 commit comments

Comments
 (0)