| | | 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 | | |
| | | 4 | | namespace System.Text.Json |
| | | 5 | | { |
| | | 6 | | internal sealed class JsonCamelCaseNamingPolicy : JsonNamingPolicy |
| | | 7 | | { |
| | | 8 | | public override string ConvertName(string name) |
| | 0 | 9 | | { |
| | 0 | 10 | | if (string.IsNullOrEmpty(name) || !char.IsUpper(name[0])) |
| | 0 | 11 | | { |
| | 0 | 12 | | return name; |
| | | 13 | | } |
| | | 14 | | |
| | 0 | 15 | | return string.Create(name.Length, name, (chars, name) => |
| | 0 | 16 | | { |
| | 0 | 17 | | name.AsSpan().CopyTo(chars); |
| | 0 | 18 | | FixCasing(chars); |
| | 0 | 19 | | }); |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | private static void FixCasing(Span<char> chars) |
| | 0 | 23 | | { |
| | 0 | 24 | | for (int i = 0; i < chars.Length; i++) |
| | 0 | 25 | | { |
| | 0 | 26 | | if (i == 1 && !char.IsUpper(chars[i])) |
| | 0 | 27 | | { |
| | 0 | 28 | | break; |
| | | 29 | | } |
| | | 30 | | |
| | 0 | 31 | | bool hasNext = (i + 1 < chars.Length); |
| | | 32 | | |
| | | 33 | | // Stop when next char is already lowercase. |
| | 0 | 34 | | if (i > 0 && hasNext && !char.IsUpper(chars[i + 1])) |
| | 0 | 35 | | { |
| | | 36 | | // If the next char is a space, lowercase current char before exiting. |
| | 0 | 37 | | if (chars[i + 1] == ' ') |
| | 0 | 38 | | { |
| | 0 | 39 | | chars[i] = char.ToLowerInvariant(chars[i]); |
| | 0 | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | break; |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | chars[i] = char.ToLowerInvariant(chars[i]); |
| | 0 | 46 | | } |
| | 0 | 47 | | } |
| | | 48 | | } |
| | | 49 | | } |