• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

neon-sunset / U8String / 6005208900

28 Aug 2023 09:36PM UTC coverage: 18.096% (-0.2%) from 18.326%
6005208900

push

github

neon-sunset
feat: Extend NativeU8String and restructure solution to account for increased line count, add roadmap draft

134 of 1050 branches covered (0.0%)

Branch coverage included in aggregate %.

1058 of 1058 new or added lines in 25 files covered. (100.0%)

478 of 2332 relevant lines covered (20.5%)

35305.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Serialization/U8StringJsonConverter.cs
1
using System.Buffers;
2
using System.Diagnostics;
3
using System.Diagnostics.CodeAnalysis;
4
using System.Text.Json;
5
using System.Text.Json.Serialization;
6

7
namespace U8Primitives.Serialization;
8

9
/// <summary>
10
/// A <see cref="JsonConverter{T}"/> for <see cref="U8String"/>.
11
/// </summary>
12
public sealed class U8StringJsonConverter : JsonConverter<U8String>
13
{
14
    /// <inheritdoc/>
15
    public override U8String Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
16
    {
17
        var readerValue = reader; // Dereference once
×
18
        if (readerValue.TokenType is JsonTokenType.String)
×
19
        {
20
            var buffer = !readerValue.HasValueSequence
×
21
                ? readerValue.ValueSpan.ToArray()
×
22
                : readerValue.ValueSequence.ToArray();
×
23
            var length = buffer.Length;
×
24

25
            return new(buffer, 0, length);
×
26
        }
27

28
        return JsonException(readerValue.TokenType);
×
29
    }
30

31
    /// <inheritdoc/>
32
    public override void Write(Utf8JsonWriter writer, U8String value, JsonSerializerOptions options)
33
    {
34
        // It would be really nice to be able to skip validation here
35
        // but that would require either modifying JsonSerializerOptions
36
        // which we can't do because users don't expect it to get transiently changed
37
        // nor can we tell the writer to skip validation either since there is no overload for that
38
        // which leaves us with a choice to either contributing performance fix for utf8 string validation
39
        // to System.Text.Json hoping it would get accepted, possibly adjusting the overloads and impl choices
40
        // or copying the string to an intermediate buffer, doing escaping and adding quotes manually, and only
41
        // then passing the final result to the writer
42
        // byte[]? toReturn = null;
43

44
        // TODO: Check if the value needs to be escaped and calculate the escaped length,
45
        // would probably then need another pass to find+replace characters as a single action,
46
        // // maybe optimize for the common case of no escaping needed and then grow the buffer if needed?
47
        // var length = value.Length + 2;
48
        // var buffer = length <= 256
49
        //     ? stackalloc byte[256]
50
        //     : (toReturn = ArrayPool<byte>.Shared.Rent(length));
51

52
        // buffer[0] = (byte)'"';
53
        // value.AsSpan().CopyTo(buffer[1..]);
54
        // buffer[length - 1] = (byte)'"';
55

56
        // writer.WriteRawValue(buffer[..length], skipInputValidation: true);
57

58
        // if (toReturn != null) ArrayPool<byte>.Shared.Return(toReturn);
59
        writer.WriteStringValue(value);
×
60
    }
×
61

62
    [DoesNotReturn, StackTraceHidden]
63
    static U8String JsonException(JsonTokenType tokenType)
64
    {
65
        throw new JsonException($"Unexpected token type: {tokenType}");
×
66
    }
67
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc