Welcome to mirror list, hosted at ThFree Co, Russian Federation.

JSStreamReferenceJsonConverter.cs « Infrastructure « src « Microsoft.JSInterop « JSInterop « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 79b22dda51d73454b3649186d5e4136882fd9ee8 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.JSInterop.Implementation;

namespace Microsoft.JSInterop.Infrastructure;

internal sealed class JSStreamReferenceJsonConverter : JsonConverter<IJSStreamReference>
{
    private static readonly JsonEncodedText _jsStreamReferenceLengthKey = JsonEncodedText.Encode("__jsStreamReferenceLength");

    private readonly JSRuntime _jsRuntime;

    public JSStreamReferenceJsonConverter(JSRuntime jsRuntime)
    {
        _jsRuntime = jsRuntime;
    }

    public override bool CanConvert(Type typeToConvert)
        => typeToConvert == typeof(IJSStreamReference) || typeToConvert == typeof(JSStreamReference);

    public override IJSStreamReference? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        long? id = null;
        long? length = null;

        while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
        {
            if (reader.TokenType == JsonTokenType.PropertyName)
            {
                if (id is null && reader.ValueTextEquals(JSObjectReferenceJsonWorker.JSObjectIdKey.EncodedUtf8Bytes))
                {
                    reader.Read();
                    id = reader.GetInt64();
                }
                else if (length is null && reader.ValueTextEquals(_jsStreamReferenceLengthKey.EncodedUtf8Bytes))
                {
                    reader.Read();
                    length = reader.GetInt64();
                }
                else
                {
                    throw new JsonException($"Unexpected JSON property {reader.GetString()}.");
                }
            }
            else
            {
                throw new JsonException($"Unexpected JSON token {reader.TokenType}");
            }
        }

        if (!id.HasValue)
        {
            throw new JsonException($"Required property {JSObjectReferenceJsonWorker.JSObjectIdKey} not found.");
        }

        if (!length.HasValue)
        {
            throw new JsonException($"Required property {_jsStreamReferenceLengthKey} not found.");
        }

        return new JSStreamReference(_jsRuntime, id.Value, length.Value);
    }

    public override void Write(Utf8JsonWriter writer, IJSStreamReference value, JsonSerializerOptions options)
    {
        JSObjectReferenceJsonWorker.WriteJSObjectReference(writer, (JSStreamReference)value);
    }
}