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

Add JsonEnumMemberNameAttribute. #105032

Merged
merged 14 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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 @@ -1164,6 +1164,12 @@ public JsonStringEnumConverter(System.Text.Json.JsonNamingPolicy? namingPolicy =
public sealed override bool CanConvert(System.Type typeToConvert) { throw null; }
public sealed override System.Text.Json.Serialization.JsonConverter CreateConverter(System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { throw null; }
}
[System.AttributeUsageAttribute(System.AttributeTargets.Field, AllowMultiple=false)]
public partial class JsonStringEnumMemberNameAttribute : System.Attribute
{
public JsonStringEnumMemberNameAttribute(string name) { }
public string Name { get { throw null; } }
}
public enum JsonUnknownDerivedTypeHandling
{
FailSerialization = 0,
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Text.Json/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@
<data name="InvalidCharacterWithinString" xml:space="preserve">
<value>'{0}' is invalid within a JSON string. The string should be correctly escaped.</value>
</data>
<data name="InvalidEnumTypeWithSpecialChar" xml:space="preserve">
<value>Enum type '{0}' uses unsupported identifer name '{1}'.</value>
<data name="UnsupportedEnumIdentifier" xml:space="preserve">
<value>Enum type '{0}' uses unsupported identifier '{1}'. It must not be null, empty, or containing leading or trailing whitespace. Flags enums must additionally not contain commas.</value>
</data>
<data name="InvalidEndOfJsonNonPrimitive" xml:space="preserve">
<value>'{0}' is an invalid token type for the end of the JSON payload. Expected either 'EndArray' or 'EndObject'.</value>
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 @@ -266,6 +266,7 @@ The System.Text.Json library is built-in as part of the shared framework in .NET
<Compile Include="System\Text\Json\Serialization\JsonSerializerOptions.Converters.cs" />
<Compile Include="System\Text\Json\Serialization\JsonSerializerOptions.cs" />
<Compile Include="System\Text\Json\Serialization\JsonStringEnumConverter.cs" />
<Compile Include="System\Text\Json\Serialization\JsonStringEnumMemberNameAttribute.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\FSharpCoreReflectionProxy.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonCollectionInfoValuesOfTCollection.cs" />
<Compile Include="System\Text\Json\Serialization\Metadata\JsonMetadataServices.Collections.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ private static bool HasCustomAttributeWithName(this MemberInfo memberInfo, strin
/// Polyfill for BindingFlags.DoNotWrapExceptions
/// </summary>
public static object? CreateInstanceNoWrapExceptions(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicConstructors)] this Type type,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] this Type type,
Type[] parameterTypes,
object?[] parameters)
{
ConstructorInfo ctorInfo = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, parameterTypes, null)!;
ConstructorInfo ctorInfo = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, parameterTypes, null)!;
#if NET
return ctorInfo.Invoke(BindingFlags.DoNotWrapExceptions, null, parameters, null);
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public static bool HasAllSet(this BitArray bitArray)
/// Gets a Regex instance for recognizing integer representations of enums.
/// </summary>
public static readonly Regex IntegerRegex = CreateIntegerRegex();
private const string IntegerRegexPattern = @"^\s*(\+|\-)?[0-9]+\s*$";
private const string IntegerRegexPattern = @"^\s*(?:\+|\-)?[0-9]+\s*$";
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
private const int IntegerRegexTimeoutMs = 200;

#if NET
Expand Down Expand Up @@ -570,5 +570,52 @@ static int IndexOfFirstTrailingZero(ReadOnlySpan<byte> span)
}
}
}

#if !NET
// Polyfill for MemoryExtensions.Trim
public static ReadOnlySpan<char> Trim(this ReadOnlySpan<char> source)
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
{
int start = 0;
int end = source.Length - 1;

while (start <= end && char.IsWhiteSpace(source[start]))
{
start++;
}

while (end >= start && char.IsWhiteSpace(source[end]))
{
end--;
}

return source.Slice(start, end - start + 1);
}

public static ReadOnlySpan<char> TrimStart(this ReadOnlySpan<char> source)
{
int start = 0;
int end = source.Length - 1;

while (start <= end && char.IsWhiteSpace(source[start]))
{
start++;
}

return source.Slice(start, end - start + 1);
}

public static ReadOnlySpan<char> TrimEnd(this ReadOnlySpan<char> source)
{
int start = 0;
int end = source.Length - 1;

while (end >= start && char.IsWhiteSpace(source[end]))
{
end--;
}

return source.Slice(start, end - start + 1);
}
#endif
}
}
Loading
Loading