~ruther/NosSmooth

ref: 7dbc666493f91e282ad0518e786d6e4c3ebe07bd NosSmooth/Packets/NosSmooth.PacketSerializer/Converters/Special/Converters/NullableStringConverter.cs -rw-r--r-- 2.4 KiB
7dbc6664 — František Boháček feat: use stackalloc and Span for packet serialization 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
//  NullableStringConverter.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using NosSmooth.PacketSerializer.Abstractions;
using Remora.Results;

namespace NosSmooth.PacketSerializer.Converters.Special.Converters;

#pragma warning disable SA1125
/// <summary>
/// Converter of nullable types.
/// </summary>
/// <typeparam name="T">The nonnullable underlying type.</typeparam>
public class NullableStringConverter<T> : BaseStringConverter<Nullable<T>>
    where T : struct
{
    private readonly IStringSerializer _stringSerializer;

    /// <summary>
    /// Initializes a new instance of the <see cref="NullableStringConverter{T}"/> class.
    /// </summary>
    /// <param name="stringSerializer">The string serializer.</param>
    public NullableStringConverter(IStringSerializer stringSerializer)
    {
        _stringSerializer = stringSerializer;

    }

    /// <inheritdoc />
    public override Result Serialize(T? obj, ref PacketStringBuilder builder)
    {
        if (obj is null)
        {
            builder.Append("-1");
            return Result.FromSuccess();
        }

        return _stringSerializer.Serialize<T>(obj.Value, ref builder);
    }

    /// <inheritdoc />
    public override Result<T?> Deserialize(ref PacketStringEnumerator stringEnumerator, DeserializeOptions options)
    {
        var nextToken = stringEnumerator.GetNextToken(out var packetToken, false);
        if (!nextToken.IsSuccess)
        {
            return Result<T?>.FromError(nextToken);
        }

        if (options.CanBeNull)
        { // even though this is nullable converter and it could be expected
          // that only nullables will be passed, it's possible that
          // due to easier management, a non nullable entity will be passed
          // here.
            if (packetToken.Token.Length == 2 && packetToken.Token.StartsWith("-1"))
            {
                stringEnumerator.GetNextToken(out _); // seek.
                return Result<T?>.FromSuccess(null);
            }
        }

        var result = _stringSerializer.Deserialize<T>(ref stringEnumerator, options);
        if (!result.IsSuccess)
        {
            return Result<T?>.FromError(result);
        }

        return Result<T?>.FromSuccess(result.Entity);
    }
}
#pragma warning restore SA1125
Do not follow this link