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

Make JSON support required properties #72937

Merged
merged 11 commits into from
Jul 29, 2022
15 changes: 15 additions & 0 deletions src/libraries/System.Text.Json/src/System/Text/Json/JsonHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -147,5 +148,19 @@ public static void ValidateInt32MaxArrayLength(uint length)
ThrowHelper.ThrowOutOfMemoryException(length);
}
}

public static bool AllBitsEqual(this BitArray bitArray, bool value)
{
// Optimize this when https://github.com/dotnet/runtime/issues/72999 is fixed
for (int i = 0; i < bitArray.Count; i++)
{
if (bitArray[i] != value)
{
return false;
}
}

return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void MarkRequiredPropertyAsRead(JsonPropertyInfo propertyInfo)
if (propertyInfo.IsRequired)
krwq marked this conversation as resolved.
Show resolved Hide resolved
{
Debug.Assert(RequiredPropertiesLeft != null);
RequiredPropertiesLeft.Set(propertyInfo.RequiredPropertyIndex, false);
RequiredPropertiesLeft[propertyInfo.RequiredPropertyIndex] = false;
}
}

Expand All @@ -140,13 +140,9 @@ internal void ValidateAllRequiredPropertiesAreRead(JsonTypeInfo typeInfo)
{
Debug.Assert(RequiredPropertiesLeft != null);

// Optimize this when https://github.com/dotnet/runtime/issues/72999 is fixed
for (int i = 0; i < RequiredPropertiesLeft.Count; i++)
if (!RequiredPropertiesLeft.AllBitsEqual(false))
{
if (RequiredPropertiesLeft[i])
{
ThrowHelper.ThrowJsonException_JsonRequiredPropertyMissing(typeInfo, RequiredPropertiesLeft);
}
ThrowHelper.ThrowJsonException_JsonRequiredPropertyMissing(typeInfo, RequiredPropertiesLeft);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,38 +223,22 @@ public static void ThrowJsonException_JsonRequiredPropertyMissing(JsonTypeInfo p

Debug.Assert(parent.PropertyCache != null);

int propertyIdx = 0;

for (int requiredPropertyIdx = 0; requiredPropertyIdx < missingJsonProperties.Length; requiredPropertyIdx++)
for (int propertyIdx = 0; propertyIdx < parent.PropertyCache.List.Count; propertyIdx++)
{
if (!missingJsonProperties[requiredPropertyIdx])
{
continue;
}
JsonPropertyInfo property = parent.PropertyCache.List[propertyIdx].Value;

JsonPropertyInfo? missingProperty = null;

// requiredPropertyIdx indices occur consecutively so we can resume iteration
for (; propertyIdx < parent.PropertyCache.List.Count; propertyIdx++)
if (!property.IsRequired || !missingJsonProperties[property.RequiredPropertyIndex])
{
JsonPropertyInfo maybeMissingProperty = parent.PropertyCache.List[propertyIdx].Value;
if (maybeMissingProperty.IsRequired && maybeMissingProperty.RequiredPropertyIndex == requiredPropertyIdx)
{
missingProperty = maybeMissingProperty;
break;
}
continue;
}

Debug.Assert(propertyIdx != parent.PropertyCache.List.Count);
Debug.Assert(missingProperty != null);

if (!first)
{
listOfMissingPropertiesBuilder.Append(CultureInfo.CurrentUICulture.TextInfo.ListSeparator);
listOfMissingPropertiesBuilder.Append(' ');
}

listOfMissingPropertiesBuilder.Append(missingProperty.Name);
listOfMissingPropertiesBuilder.Append(property.Name);
krwq marked this conversation as resolved.
Show resolved Hide resolved
first = false;
}

Expand Down