< Summary

Information
Class: System.Text.Json.JsonCamelCaseNamingPolicy
Assembly: System.Text.Json
File(s): C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\Common\JsonCamelCaseNamingPolicy.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 27
Coverable lines: 27
Total lines: 55
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
ConvertName(...)0%440%
FixCasing(...)0%12120%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\Common\JsonCamelCaseNamingPolicy.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4namespace System.Text.Json
 5{
 6    internal sealed class JsonCamelCaseNamingPolicy : JsonNamingPolicy
 7    {
 8        public override string ConvertName(string name)
 09        {
 010            if (string.IsNullOrEmpty(name) || !char.IsUpper(name[0]))
 011            {
 012                return name;
 13            }
 14
 15#if NET
 016            return string.Create(name.Length, name, (chars, name) =>
 017            {
 018                name.CopyTo(chars);
 019                FixCasing(chars);
 020            });
 21#else
 22            char[] chars = name.ToCharArray();
 23            FixCasing(chars);
 24            return new string(chars);
 25#endif
 026        }
 27
 28        private static void FixCasing(Span<char> chars)
 029        {
 030            for (int i = 0; i < chars.Length; i++)
 031            {
 032                if (i == 1 && !char.IsUpper(chars[i]))
 033                {
 034                    break;
 35                }
 36
 037                bool hasNext = (i + 1 < chars.Length);
 38
 39                // Stop when next char is already lowercase.
 040                if (i > 0 && hasNext && !char.IsUpper(chars[i + 1]))
 041                {
 42                    // If the next char is a space, lowercase current char before exiting.
 043                    if (chars[i + 1] == ' ')
 044                    {
 045                        chars[i] = char.ToLowerInvariant(chars[i]);
 046                    }
 47
 048                    break;
 49                }
 50
 051                chars[i] = char.ToLowerInvariant(chars[i]);
 052            }
 053        }
 54    }
 55}