Skip to content

Commit

Permalink
Support JsonExtensionData in STJ source-gen (#58912)
Browse files Browse the repository at this point in the history
* Support JsonExtensionData in STJ source-gen

* Address review feedback
  • Loading branch information
layomia committed Sep 11, 2021
1 parent 096e3c4 commit b9be494
Show file tree
Hide file tree
Showing 40 changed files with 2,163 additions and 1,586 deletions.
23 changes: 19 additions & 4 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ private sealed partial class Emitter
private const string IListTypeRef = "global::System.Collections.Generic.IList";
private const string KeyValuePairTypeRef = "global::System.Collections.Generic.KeyValuePair";
private const string ListTypeRef = "global::System.Collections.Generic.List";
private const string DictionaryTypeRef = "global::System.Collections.Generic.Dictionary";
private const string JsonEncodedTextTypeRef = "global::System.Text.Json.JsonEncodedText";
private const string JsonNamingPolicyTypeRef = "global::System.Text.Json.JsonNamingPolicy";
private const string JsonSerializerTypeRef = "global::System.Text.Json.JsonSerializer";
Expand Down Expand Up @@ -253,6 +252,12 @@ private void GenerateTypeInfo(TypeGenerationSpec typeGenerationSpec)
GenerateTypeInfo(spec.TypeGenerationSpec);
}
}

TypeGenerationSpec? extPropTypeSpec = typeGenerationSpec.ExtensionDataPropertyTypeSpec;
if (extPropTypeSpec != null)
{
GenerateTypeInfo(extPropTypeSpec);
}
}
break;
case ClassType.KnownUnsupportedType:
Expand Down Expand Up @@ -431,9 +436,18 @@ private string GenerateForCollection(TypeGenerationSpec typeGenerationSpec)
CollectionType collectionType = typeGenerationSpec.CollectionType;

string typeRef = typeGenerationSpec.TypeRef;
string createObjectFuncArg = typeGenerationSpec.ConstructionStrategy == ObjectConstructionStrategy.ParameterlessConstructor
? $"createObjectFunc: () => new {typeRef}()"
: "createObjectFunc: null";

string createObjectFuncArg;
if (typeGenerationSpec.RuntimeTypeRef != null)
{
createObjectFuncArg = $"createObjectFunc: () => new {typeGenerationSpec.RuntimeTypeRef}()";
}
else
{
createObjectFuncArg = typeGenerationSpec.ConstructionStrategy == ObjectConstructionStrategy.ParameterlessConstructor
? $"createObjectFunc: () => new {typeRef}()"
: "createObjectFunc: null";
}

string collectionInfoCreationPrefix = collectionType switch
{
Expand Down Expand Up @@ -741,6 +755,7 @@ private string GeneratePropMetadataInitFunc(TypeGenerationSpec typeGenerationSpe
{setterNamedArg},
{ignoreConditionNamedArg},
hasJsonInclude: {ToCSharpKeyword(memberMetadata.HasJsonInclude)},
isExtensionData: {ToCSharpKeyword(memberMetadata.IsExtensionData)},
numberHandling: {GetNumberHandlingAsStr(memberMetadata.NumberHandling)},
propertyName: ""{clrPropertyName}"",
{jsonPropertyNameNamedArg});
Expand Down
193 changes: 146 additions & 47 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/libraries/System.Text.Json/gen/PropertyGenerationSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ internal sealed class PropertyGenerationSpec
/// </summary>
public bool HasJsonInclude { get; init; }

/// <summary>
/// Whether the property has the JsonExtensionDataAttribute.
/// </summary>
public bool IsExtensionData { get; init; }

/// <summary>
/// Generation specification for the property's type.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,16 @@
<data name="MultipleJsonConstructorAttributeTitle" xml:space="preserve">
<value>Type has multiple constructors annotated with JsonConstructorAttribute.</value>
</data>
<data name="MultipleJsonExtensionDataAttributeFormat" xml:space="preserve">
<value>Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</value>
</data>
<data name="MultipleJsonExtensionDataAttributeTitle" xml:space="preserve">
<value>Type has multiple members annotated with JsonExtensionDataAttribute.</value>
</data>
<data name="DataExtensionPropertyInvalidFormat" xml:space="preserve">
<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>
</data>
<data name="DataExtensionPropertyInvalidTitle" xml:space="preserve">
<value>Data extension property type invalid.</value>
</data>
</root>
20 changes: 20 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
<target state="translated">Odvozené typy JsonSerializerContext a všechny obsahující typy musí být částečné.</target>
<note />
</trans-unit>
<trans-unit id="DataExtensionPropertyInvalidFormat">
<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>
<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>
<note />
</trans-unit>
<trans-unit id="DataExtensionPropertyInvalidTitle">
<source>Data extension property type invalid.</source>
<target state="new">Data extension property type invalid.</target>
<note />
</trans-unit>
<trans-unit id="DuplicateTypeNameMessageFormat">
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
<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>
Expand All @@ -32,6 +42,16 @@
<target state="translated">Typ obsahuje více konstruktorů anotovaných s JsonConstructorAttribute.</target>
<note />
</trans-unit>
<trans-unit id="MultipleJsonExtensionDataAttributeFormat">
<source>Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</source>
<target state="new">Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</target>
<note />
</trans-unit>
<trans-unit id="MultipleJsonExtensionDataAttributeTitle">
<source>Type has multiple members annotated with JsonExtensionDataAttribute.</source>
<target state="new">Type has multiple members annotated with JsonExtensionDataAttribute.</target>
<note />
</trans-unit>
<trans-unit id="TypeNotSupportedMessageFormat">
<source>Did not generate serialization metadata for type '{0}'.</source>
<target state="translated">Nevygenerovala se metadata serializace pro typ {0}.</target>
Expand Down
20 changes: 20 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
<target state="translated">Abgeleitete JsonSerializerContext-Typen und alle enthaltenden Typen müssen partiell sein.</target>
<note />
</trans-unit>
<trans-unit id="DataExtensionPropertyInvalidFormat">
<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>
<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>
<note />
</trans-unit>
<trans-unit id="DataExtensionPropertyInvalidTitle">
<source>Data extension property type invalid.</source>
<target state="new">Data extension property type invalid.</target>
<note />
</trans-unit>
<trans-unit id="DuplicateTypeNameMessageFormat">
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
<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>
Expand All @@ -32,6 +42,16 @@
<target state="translated">Der Typ weist mehrere Konstruktoren mit dem Kommentar JsonConstructorAttribute auf.</target>
<note />
</trans-unit>
<trans-unit id="MultipleJsonExtensionDataAttributeFormat">
<source>Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</source>
<target state="new">Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</target>
<note />
</trans-unit>
<trans-unit id="MultipleJsonExtensionDataAttributeTitle">
<source>Type has multiple members annotated with JsonExtensionDataAttribute.</source>
<target state="new">Type has multiple members annotated with JsonExtensionDataAttribute.</target>
<note />
</trans-unit>
<trans-unit id="TypeNotSupportedMessageFormat">
<source>Did not generate serialization metadata for type '{0}'.</source>
<target state="translated">Die Serialisierungsmetadaten für den Typ "{0}" wurden nicht generiert.</target>
Expand Down
20 changes: 20 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
<target state="translated">Los tipos derivados "JsonSerializerContext" y todos los tipos que contienen deben ser parciales.</target>
<note />
</trans-unit>
<trans-unit id="DataExtensionPropertyInvalidFormat">
<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>
<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>
<note />
</trans-unit>
<trans-unit id="DataExtensionPropertyInvalidTitle">
<source>Data extension property type invalid.</source>
<target state="new">Data extension property type invalid.</target>
<note />
</trans-unit>
<trans-unit id="DuplicateTypeNameMessageFormat">
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
<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>
Expand All @@ -32,6 +42,16 @@
<target state="translated">El tipo tiene varios constructores anotados con JsonConstructorAttribute.</target>
<note />
</trans-unit>
<trans-unit id="MultipleJsonExtensionDataAttributeFormat">
<source>Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</source>
<target state="new">Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</target>
<note />
</trans-unit>
<trans-unit id="MultipleJsonExtensionDataAttributeTitle">
<source>Type has multiple members annotated with JsonExtensionDataAttribute.</source>
<target state="new">Type has multiple members annotated with JsonExtensionDataAttribute.</target>
<note />
</trans-unit>
<trans-unit id="TypeNotSupportedMessageFormat">
<source>Did not generate serialization metadata for type '{0}'.</source>
<target state="translated">No generó metadatos de serialización para el tipo '{0}".</target>
Expand Down
20 changes: 20 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
<target state="translated">Les types dérivés 'JsonSerializerContext' et tous les types conteneurs doivent être partiels.</target>
<note />
</trans-unit>
<trans-unit id="DataExtensionPropertyInvalidFormat">
<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>
<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>
<note />
</trans-unit>
<trans-unit id="DataExtensionPropertyInvalidTitle">
<source>Data extension property type invalid.</source>
<target state="new">Data extension property type invalid.</target>
<note />
</trans-unit>
<trans-unit id="DuplicateTypeNameMessageFormat">
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
<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>
Expand All @@ -32,6 +42,16 @@
<target state="translated">Le type a plusieurs constructeurs annotés avec JsonConstructorAttribute.</target>
<note />
</trans-unit>
<trans-unit id="MultipleJsonExtensionDataAttributeFormat">
<source>Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</source>
<target state="new">Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</target>
<note />
</trans-unit>
<trans-unit id="MultipleJsonExtensionDataAttributeTitle">
<source>Type has multiple members annotated with JsonExtensionDataAttribute.</source>
<target state="new">Type has multiple members annotated with JsonExtensionDataAttribute.</target>
<note />
</trans-unit>
<trans-unit id="TypeNotSupportedMessageFormat">
<source>Did not generate serialization metadata for type '{0}'.</source>
<target state="translated">Les métadonnées de sérialisation pour le type « {0} » n’ont pas été générées.</target>
Expand Down
20 changes: 20 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
<target state="translated">I tipi derivati 'JsonSerializerContext' e tutti i tipi contenenti devono essere parziali.</target>
<note />
</trans-unit>
<trans-unit id="DataExtensionPropertyInvalidFormat">
<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>
<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>
<note />
</trans-unit>
<trans-unit id="DataExtensionPropertyInvalidTitle">
<source>Data extension property type invalid.</source>
<target state="new">Data extension property type invalid.</target>
<note />
</trans-unit>
<trans-unit id="DuplicateTypeNameMessageFormat">
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
<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>
Expand All @@ -32,6 +42,16 @@
<target state="translated">Il tipo contiene più costruttori che presentano l'annotazione JsonConstructorAttribute.</target>
<note />
</trans-unit>
<trans-unit id="MultipleJsonExtensionDataAttributeFormat">
<source>Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</source>
<target state="new">Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</target>
<note />
</trans-unit>
<trans-unit id="MultipleJsonExtensionDataAttributeTitle">
<source>Type has multiple members annotated with JsonExtensionDataAttribute.</source>
<target state="new">Type has multiple members annotated with JsonExtensionDataAttribute.</target>
<note />
</trans-unit>
<trans-unit id="TypeNotSupportedMessageFormat">
<source>Did not generate serialization metadata for type '{0}'.</source>
<target state="translated">Non sono stati generati metadati di serializzazione per il tipo '{0}'.</target>
Expand Down
20 changes: 20 additions & 0 deletions src/libraries/System.Text.Json/gen/Resources/xlf/Strings.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
<target state="translated">派生した 'JsonSerializerContext' 型と含まれているすべての型は部分的である必要があります。</target>
<note />
</trans-unit>
<trans-unit id="DataExtensionPropertyInvalidFormat">
<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>
<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>
<note />
</trans-unit>
<trans-unit id="DataExtensionPropertyInvalidTitle">
<source>Data extension property type invalid.</source>
<target state="new">Data extension property type invalid.</target>
<note />
</trans-unit>
<trans-unit id="DuplicateTypeNameMessageFormat">
<source>There are multiple types named {0}. Source was generated for the first one detected. Use 'JsonSerializableAttribute.TypeInfoPropertyName' to resolve this collision.</source>
<target state="translated">{0} と名前が付けられた種類が複数あります。最初に検出されたものに対してソースが生成されました。この問題を解決するには、'JsonSerializableAttribute.TypeInfoPropertyName' を使用します。</target>
Expand All @@ -32,6 +42,16 @@
<target state="translated">型には、JsonConstructorAttribute で注釈が付けられた複数のコンストラクターがあります。</target>
<note />
</trans-unit>
<trans-unit id="MultipleJsonExtensionDataAttributeFormat">
<source>Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</source>
<target state="new">Type '{0}' has multiple members annotated with 'JsonExtensionDataAttribute'.</target>
<note />
</trans-unit>
<trans-unit id="MultipleJsonExtensionDataAttributeTitle">
<source>Type has multiple members annotated with JsonExtensionDataAttribute.</source>
<target state="new">Type has multiple members annotated with JsonExtensionDataAttribute.</target>
<note />
</trans-unit>
<trans-unit id="TypeNotSupportedMessageFormat">
<source>Did not generate serialization metadata for type '{0}'.</source>
<target state="translated">'{0}'型 のシリアル化メタデータを生成ませんでした。</target>
Expand Down
Loading

0 comments on commit b9be494

Please sign in to comment.