Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON support required properties #73063

Merged
merged 4 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
6 changes: 6 additions & 0 deletions src/libraries/System.Text.Json/ref/System.Text.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,11 @@ public sealed partial class JsonPropertyOrderAttribute : System.Text.Json.Serial
public JsonPropertyOrderAttribute(int order) { }
public int Order { get { throw null; } }
}
[System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple = false)]
public sealed partial class JsonRequiredAttribute : System.Text.Json.Serialization.JsonAttribute
{
public JsonRequiredAttribute() { }
}
[System.AttributeUsageAttribute(System.AttributeTargets.Class, AllowMultiple=true)]
public sealed partial class JsonSerializableAttribute : System.Text.Json.Serialization.JsonAttribute
{
Expand Down Expand Up @@ -1152,6 +1157,7 @@ internal JsonPropertyInfo() { }
public System.Text.Json.Serialization.JsonConverter? CustomConverter { get { throw null; } set { } }
public System.Func<object, object?>? Get { get { throw null; } set { } }
public bool IsExtensionData { get { throw null; } set { } }
public bool IsRequired { get { throw null; } set { } }
public string Name { get { throw null; } set { } }
public System.Text.Json.Serialization.JsonNumberHandling? NumberHandling { get { throw null; } set { } }
public System.Text.Json.JsonSerializerOptions Options { get { throw null; } }
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ The System.Text.Json library is built-in as part of the shared framework in .NET
<Compile Include="System\Text\Json\Serialization\Attributes\JsonNumberHandlingAttribute.cs" />
<Compile Include="System\Text\Json\Serialization\Attributes\JsonPolymorphicAttribute.cs" />
<Compile Include="System\Text\Json\Serialization\Attributes\JsonPropertyNameAttribute.cs" />
<Compile Include="System\Text\Json\Serialization\Attributes\JsonRequiredAttribute.cs" />
<Compile Include="System\Text\Json\Serialization\Attributes\JsonPropertyOrderAttribute.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\CastingConverter.cs" />
<Compile Include="System\Text\Json\Serialization\Converters\Collection\ImmutableDictionaryOfTKeyTValueConverterWithReflection.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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
{
/// <summary>
/// Specifies the property is required when deserializing.
krwq marked this conversation as resolved.
Show resolved Hide resolved
krwq marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
krwq marked this conversation as resolved.
Show resolved Hide resolved
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
krwq marked this conversation as resolved.
Show resolved Hide resolved
public sealed class JsonRequiredAttribute : JsonAttribute
{
/// <summary>
/// Initializes a new instance of <see cref="JsonRequiredAttribute"/>.
/// </summary>
public JsonRequiredAttribute()
krwq marked this conversation as resolved.
Show resolved Hide resolved
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,18 @@ public bool IsExtensionData

private bool _isExtensionDataProperty;

internal bool IsRequired
/// <summary>
/// Specifies whether the current property is required during deserialization.
krwq marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <exception cref="InvalidOperationException">
/// The <see cref="JsonPropertyInfo"/> instance has been locked for further modification.
/// </exception>
/// <remarks>
/// For contracts originating from <see cref="DefaultJsonTypeInfoResolver"/> or <see cref="JsonSerializerContext"/>,
/// the value of this property will be mapped from based on presence of <see cref="JsonRequiredAttribute"/> annotation or presence of <see langword="required"/> keyword.
/// If SetsRequiredMembersAttribute is specified on the constructor the <see langword="required"/> keyword will not influence value of this property.
krwq marked this conversation as resolved.
Show resolved Hide resolved
/// </remarks>
public bool IsRequired
{
get => _isRequired;
set
Expand Down Expand Up @@ -506,10 +517,8 @@ private bool NumberHandingIsApplicable()

private void DetermineIsRequired(MemberInfo memberInfo, bool shouldCheckForRequiredKeyword)
{
if (shouldCheckForRequiredKeyword && memberInfo.HasRequiredMemberAttribute())
{
IsRequired = true;
}
IsRequired = memberInfo.GetCustomAttribute<JsonRequiredAttribute>(inherit: false) != null
|| (shouldCheckForRequiredKeyword && memberInfo.HasRequiredMemberAttribute());
}

internal abstract bool GetMemberAndWriteJson(object obj, ref WriteStack state, Utf8JsonWriter writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,10 +646,9 @@ public static JsonSerializerOptions ResolveOptionsInstanceWithSmallBuffer(JsonSe

JsonSerializerOptions smallBufferCopy = new JsonSerializerOptions(options)
{
// Copy the resolver explicitly until https://github.com/dotnet/aspnetcore/issues/38720 is resolved.
TypeInfoResolver = options.TypeInfoResolver,
DefaultBufferSize = 1,
};

s_smallBufferMap.Add(options, smallBufferCopy);
return smallBufferCopy;
}
Expand Down
Loading