Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update source-gen APIs according to review
  • Loading branch information
layomia committed Sep 15, 2021
commit 32f39f139b654eb2f49d16ee9e6d8e9fc55c8d7e
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ sealed class JsonSourceGenerationOptionsAttribute : JsonAttribute
/// </summary>
public bool IgnoreReadOnlyProperties { get; set; }

/// <summary>
/// Specifies whether to ignore custom converters provided at run time.
/// </summary>
public bool IgnoreRuntimeCustomConverters { get; set; }

/// <summary>
/// Specifies whether to include fields for serialization and deserialization.
/// </summary>
Expand Down
200 changes: 104 additions & 96 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs

Large diffs are not rendered by default.

14 changes: 3 additions & 11 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -565,14 +565,6 @@ private static bool TryGetClassDeclarationList(INamedTypeSymbol typeSymbol, [Not
}
}
break;
case nameof(JsonSourceGenerationOptionsAttribute.IgnoreRuntimeCustomConverters):
{
if (bool.TryParse(propertyValueStr, out bool value))
{
options.IgnoreRuntimeCustomConverters = value;
}
}
break;
case nameof(JsonSourceGenerationOptionsAttribute.IncludeFields):
{
if (bool.TryParse(propertyValueStr, out bool value))
Expand Down Expand Up @@ -640,7 +632,7 @@ private TypeGenerationSpec GetOrAddTypeGenerationSpec(Type type, JsonSourceGener
string? converterInstatiationLogic = null;
bool implementsIJsonOnSerialized = false;
bool implementsIJsonOnSerializing = false;
bool hasEncounteredInitOnlyProperties = false;
bool hasInitOnlyProperties = false;
bool hasTypeFactoryConverter = false;
bool hasPropertyFactoryConverters = false;

Expand Down Expand Up @@ -972,10 +964,10 @@ void CacheMemberHelper()
_implicitlyRegisteredTypes.Add(dataExtensionPropGenSpec);
}

if (!hasEncounteredInitOnlyProperties && spec.CanUseSetter && spec.IsInitOnlySetter)
if (!hasInitOnlyProperties && spec.CanUseSetter && spec.IsInitOnlySetter)
{
_sourceGenerationContext.ReportDiagnostic(Diagnostic.Create(InitOnlyPropertyDeserializationNotSupported, Location.None, new string[] { type.Name }));
hasEncounteredInitOnlyProperties = true;
hasInitOnlyProperties = true;
}

if (spec.HasJsonInclude && (!spec.CanUseGetter || !spec.CanUseSetter || !spec.IsPublic))
Expand Down
120 changes: 75 additions & 45 deletions src/libraries/System.Text.Json/ref/System.Text.Json.cs

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,6 @@ System.Text.Json.Utf8JsonReader</PackageDescription>
<Compile Include="System\Text\Json\Serialization\JsonSerializer.Write.Element.cs" />
<Compile Include="System\Text\Json\Serialization\JsonSerializer.Write.Node.cs" />
<Compile Include="System\Text\Json\Serialization\JsonSerializerContext.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\FSharpCoreReflectionProxy.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonMetadataServices.Collections.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonMetadataServices.Converters.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonObjectInfoValues.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonTypeInfoInternalOfT.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonTypeInfoOfT.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonMetadataServices.cs" />
<Compile Include="System\Text\Json\Serialization\ReferenceEqualsWrapper.cs" />
<Compile Include="System\Text\Json\Serialization\ConverterStrategy.cs" />
<Compile Include="System\Text\Json\Serialization\ConverterList.cs" />
Expand Down Expand Up @@ -230,14 +223,23 @@ System.Text.Json.Utf8JsonReader</PackageDescription>
<Compile Include="System\Text\Json\Serialization\JsonSerializerOptions.cs" />
<Compile Include="System\Text\Json\Serialization\JsonStringEnumConverter.cs" />
<Compile Include="System\Text\Json\Serialization\JsonUnknownTypeHandling.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\FSharpCoreReflectionProxy.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\GenericMethodHolder.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonCollectionInfoValuesOfTCollection.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonMetadataServices.Collections.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonMetadataServices.Converters.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonMetadataServices.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonObjectInfoValuesOfT.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonParameterInfoValues.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonParameterInfo.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonParameterInfoOfT.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonPropertyInfo.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonPropertyInfoOfT.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonTypeInfo.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonPropertyInfoValuesOfT.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonTypeInfoInternalOfT.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonTypeInfoOfT.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonTypeInfo.Cache.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonTypeInfo.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\MemberAccessor.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\ParameterRef.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\PropertyRef.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,30 @@ namespace System.Text.Json.Serialization
public abstract partial class JsonSerializerContext
{
private bool? _canUseSerializationLogic;
private JsonSerializerOptions? _defaultOptions;

internal JsonSerializerOptions? _options;

/// <summary>
/// Gets the run time specified options of the context. If no options were passed
/// when instanciating the context, then a new instance is bound and returned.
/// </summary>
/// <remarks>
/// The instance cannot be mutated once it is bound with the context instance.
/// </remarks>
public JsonSerializerOptions Options
{
get
{
if (_options == null)
{
_options = new JsonSerializerOptions();
_options._context = this;
}

return _options;
}
}

/// <summary>
/// Indicates whether pre-generated serialization logic for types in the context
/// is compatible with the run time specified <see cref="JsonSerializerOptions"/>.
Expand All @@ -27,7 +47,7 @@ internal bool CanUseSerializationLogic
{
if (!_canUseSerializationLogic.HasValue)
{
if (_defaultOptions == null)
if (GeneratedSerializerOptions == null)
{
_canUseSerializationLogic = false;
}
Expand All @@ -46,13 +66,13 @@ internal bool CanUseSerializationLogic
#pragma warning restore SYSLIB0020

// Ensure options values are consistent with expected defaults.
Options.DefaultIgnoreCondition == _defaultOptions.DefaultIgnoreCondition &&
Options.IgnoreReadOnlyFields == _defaultOptions.IgnoreReadOnlyFields &&
Options.IgnoreReadOnlyProperties == _defaultOptions.IgnoreReadOnlyProperties &&
Options.IncludeFields == _defaultOptions.IncludeFields &&
Options.PropertyNamingPolicy == _defaultOptions.PropertyNamingPolicy &&
Options.DictionaryKeyPolicy == _defaultOptions.DictionaryKeyPolicy &&
Options.WriteIndented == _defaultOptions.WriteIndented;
Options.DefaultIgnoreCondition == GeneratedSerializerOptions.DefaultIgnoreCondition &&
Options.IgnoreReadOnlyFields == GeneratedSerializerOptions.IgnoreReadOnlyFields &&
Options.IgnoreReadOnlyProperties == GeneratedSerializerOptions.IgnoreReadOnlyProperties &&
Options.IncludeFields == GeneratedSerializerOptions.IncludeFields &&
Options.PropertyNamingPolicy == GeneratedSerializerOptions.PropertyNamingPolicy &&
Options.DictionaryKeyPolicy == GeneratedSerializerOptions.DictionaryKeyPolicy &&
Options.WriteIndented == GeneratedSerializerOptions.WriteIndented;
}
}

Expand All @@ -61,49 +81,30 @@ internal bool CanUseSerializationLogic
}

/// <summary>
/// Gets the run time specified options of the context. If no options were passed
/// when instanciating the context, then a new instance is bound and returned.
/// The default run time options for the context. Its values are defined at design-time via <see cref="JsonSourceGenerationOptionsAttribute"/>.
/// </summary>
/// <remarks>
/// The instance cannot be mutated once it is bound with the context instance.
/// </remarks>
public JsonSerializerOptions Options
{
get
{
if (_options == null)
{
_options = new JsonSerializerOptions();
_options._context = this;
}

return _options;
}
}
protected abstract JsonSerializerOptions? GeneratedSerializerOptions { get; }

/// <summary>
/// Creates an instance of <see cref="JsonSerializerContext"/> and binds it with the indicated <see cref="JsonSerializerOptions"/>.
/// </summary>
/// <param name="instanceOptions">The run time provided options for the context instance.</param>
/// <param name="defaultOptions">The default run time options for the context. Its values are defined at design-time via <see cref="JsonSourceGenerationOptionsAttribute"/>.</param>
/// <param name="options">The run time provided options for the context instance.</param>
/// <remarks>
/// If no instance options are passed, then no options are set until the context is bound using <see cref="JsonSerializerOptions.AddContext{TContext}"/>,
/// or until <see cref="Options"/> is called, where a new options instance is created and bound.
/// </remarks>
protected JsonSerializerContext(JsonSerializerOptions? instanceOptions, JsonSerializerOptions? defaultOptions)
protected JsonSerializerContext(JsonSerializerOptions? options)
{
if (instanceOptions != null)
if (options != null)
{
if (instanceOptions._context != null)
if (options._context != null)
{
ThrowHelper.ThrowInvalidOperationException_JsonSerializerOptionsAlreadyBoundToContext();
}

_options = instanceOptions;
instanceOptions._context = this;
_options = options;
options._context = this;
}

_defaultOptions = defaultOptions;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.Text.Json.Serialization.Metadata
{
/// <summary>
/// Provides serialization metadata about a collection type.
/// </summary>
/// <typeparam name="TCollection">The collection type.</typeparam>
public sealed class JsonCollectionInfoValues<TCollection>
{
/// <summary>
/// A <see cref="Func{TResult}"/> to create an instance of the collection when deserializing.
/// </summary>
public Func<TCollection>? ObjectCreator { get; init; }

/// <summary>
/// If a dictionary type, the <see cref="JsonTypeInfo"/> instance representing the key type.
/// </summary>
public JsonTypeInfo? KeyInfo { get; init; }

/// <summary>
/// A <see cref="JsonTypeInfo"/> instance representing the element type.
/// </summary>
public JsonTypeInfo ElementInfo { get; init; } = null!;

/// <summary>
/// The <see cref="JsonNumberHandling"/> option to apply to number collection elements.
/// </summary>
public JsonNumberHandling NumberHandling { get; init; }

/// <summary>
/// An optimized serialization implementation assuming pre-determined <see cref="JsonSourceGenerationOptionsAttribute"/> defaults.
/// </summary>
public Action<Utf8JsonWriter, TCollection>? SerializeHandler { get; init; }
}
}
Loading