public sealed override string ConvertName(string name)
{
+ if (name is null)
+ {
+ ThrowHelper.ThrowArgumentNullException(nameof(name));
+ }
+
// Rented buffer 20% longer that the input.
int rentedBufferLength = (12 * name.Length) / 10;
char[]? rentedBuffer = rentedBufferLength > JsonConstants.StackallocCharThreshold
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.Diagnostics.CodeAnalysis;
+
+namespace System.Text.Json
+{
+ internal static partial class ThrowHelper
+ {
+ [DoesNotReturn]
+ public static void ThrowArgumentNullException(string parameterName)
+ {
+ throw new ArgumentNullException(parameterName);
+ }
+ }
+}
<Compile Include="..\Common\JsonSourceGenerationOptionsAttribute.cs" Link="Common\System\Text\Json\Serialization\JsonSourceGenerationOptionsAttribute.cs" />
<Compile Include="..\Common\ReflectionExtensions.cs" Link="Common\System\Text\Json\Serialization\ReflectionExtensions.cs" />
<Compile Include="..\Common\JsonUnmappedMemberHandling.cs" Link="Common\System\Text\Json\Serialization\JsonUnmappedMemberHandling.cs" />
+ <Compile Include="..\Common\ThrowHelper.cs" Link="Common\System\Text\Json\ThrowHelper.cs" />
<Compile Include="$(CommonPath)\Roslyn\GetBestTypeByMetadataName.cs" Link="Common\Roslyn\GetBestTypeByMetadataName.cs" />
<Compile Include="ClassType.cs" />
<Compile Include="CollectionType.cs" />
<Compile Include="..\Common\JsonSourceGenerationMode.cs" Link="Common\System\Text\Json\Serialization\JsonSourceGenerationMode.cs" />
<Compile Include="..\Common\JsonSourceGenerationOptionsAttribute.cs" Link="Common\System\Text\Json\Serialization\JsonSourceGenerationOptionsAttribute.cs" />
<Compile Include="..\Common\ReflectionExtensions.cs" Link="Common\System\Text\Json\Serialization\ReflectionExtensions.cs" />
+ <Compile Include="..\Common\ThrowHelper.cs" Link="Common\System\Text\Json\ThrowHelper.cs" />
<Compile Include="System\Text\Json\AppContextSwitchHelper.cs" />
<Compile Include="System\Text\Json\BitStack.cs" />
<Compile Include="System\Text\Json\Document\JsonDocument.cs" />
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
-using System.Text.Json.Serialization.Metadata;
namespace System.Text.Json
{
// If the exception source is this value, the serializer will re-throw as JsonException.
public const string ExceptionSourceValueToRethrowAsJsonException = "System.Text.Json.Rethrowable";
- [DoesNotReturn]
- public static void ThrowArgumentNullException(string parameterName)
- {
- throw new ArgumentNullException(parameterName);
- }
-
[DoesNotReturn]
public static void ThrowArgumentOutOfRangeException_MaxDepthMustBePositive(string parameterName)
{
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;
await Assert.ThrowsAsync<InvalidOperationException>(async () => await Serializer.SerializeWrapper(new IntPropertyNamesDifferentByCaseOnly_TestClass(), options));
}
}
+
+ [Fact]
+ public void CamelCasePolicyToleratesNullOrEmpty()
+ {
+ Assert.Null(JsonNamingPolicy.CamelCase.ConvertName(null));
+ Assert.Equal(string.Empty, JsonNamingPolicy.CamelCase.ConvertName(string.Empty));
+ }
+
+ [Theory]
+ [MemberData(nameof(JsonSeparatorNamingPolicyInstances))]
+ public void InboxSeparatorNamingPolicies_ThrowsOnNullInput(JsonNamingPolicy policy)
+ {
+ Assert.Throws<ArgumentNullException>(() => policy.ConvertName(null));
+ }
+
+ [Theory]
+ [MemberData(nameof(JsonSeparatorNamingPolicyInstances))]
+ public void InboxSeparatorNamingPolicies_EmptyInput(JsonNamingPolicy policy)
+ {
+ Assert.Equal(string.Empty, policy.ConvertName(string.Empty));
+ }
+
+ public static IEnumerable<object[]> JsonSeparatorNamingPolicyInstances()
+ {
+ yield return new object[] { JsonNamingPolicy.SnakeCaseLower };
+ yield return new object[] { JsonNamingPolicy.SnakeCaseUpper };
+ yield return new object[] { JsonNamingPolicy.KebabCaseLower };
+ yield return new object[] { JsonNamingPolicy.KebabCaseUpper };
+ }
}
}