Repository navigation
Expand file tree
/
Copy pathWorkloadManifestReader.SystemTextJson.cs
More file actions
128 lines (98 loc) · 4.55 KB
/
Copy pathWorkloadManifestReader.SystemTextJson.cs
File metadata and controls
128 lines (98 loc) · 4.55 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Text.Json;
namespace Microsoft.NET.Sdk.WorkloadManifestReader
{
public partial class WorkloadManifestReader
{
public static WorkloadManifest ReadWorkloadManifest(string manifestId, Stream manifestStream, Stream? localizationStream, string manifestPath)
{
var readerOptions = new JsonReaderOptions
{
AllowTrailingCommas = true,
CommentHandling = JsonCommentHandling.Skip
};
var localizationCatalog = ReadLocalizationCatalog(localizationStream, readerOptions);
var manifestReader = new Utf8JsonStreamReader(manifestStream, readerOptions);
return ReadWorkloadManifest(manifestId, manifestPath, localizationCatalog, ref manifestReader);
}
private static LocalizationCatalog? ReadLocalizationCatalog(Stream? localizationStream, JsonReaderOptions readerOptions)
{
if (localizationStream == null)
{
return null;
}
var localizationReader = new Utf8JsonStreamReader(localizationStream, readerOptions);
return ReadLocalizationCatalog(ref localizationReader);
}
internal ref struct Utf8JsonStreamReader
{
static ReadOnlySpan<byte> utf8Bom => new byte[] { 0xEF, 0xBB, 0xBF };
const int segmentSize = 4096;
Utf8JsonReader reader;
readonly Stream stream;
byte[]? buffer;
Span<byte> span;
public Utf8JsonStreamReader(Stream stream, JsonReaderOptions readerOptions)
{
this.stream = stream;
buffer = ArrayPool<byte>.Shared.Rent(segmentSize);
var readCount = stream.Read(buffer, 0, buffer.Length);
span = buffer.AsSpan().Slice(0, readCount);
if (span.StartsWith(utf8Bom))
{
span = span.Slice(utf8Bom.Length, span.Length - utf8Bom.Length);
}
reader = new Utf8JsonReader(span, readCount >= stream.Length, new JsonReaderState(readerOptions));
}
public bool Read()
{
while (!reader.Read())
{
if (reader.IsFinalBlock)
{
if (buffer != null)
{
ArrayPool<byte>.Shared.Return(buffer);
buffer = null;
}
return false;
}
var newSegmentSize = segmentSize;
// if the value was too big to fit in the buffer, get a bigger buffer
if (reader.BytesConsumed == span.Length)
{
newSegmentSize = span.Length * 2;
}
int remaining = (int)(span.Length - reader.BytesConsumed);
var newBuffer = ArrayPool<byte>.Shared.Rent(newSegmentSize);
if (remaining > 0)
{
span.Slice((int)reader.BytesConsumed).CopyTo(newBuffer);
}
var readCount = stream.Read(newBuffer, remaining, newBuffer.Length - remaining);
if (buffer != null)
{
ArrayPool<byte>.Shared.Return(buffer);
}
buffer = newBuffer;
span = newBuffer.AsSpan().Slice(0, remaining + readCount);
reader = new Utf8JsonReader(span, stream.Position >= stream.Length, reader.CurrentState);
}
return true;
}
public long TokenStartIndex => reader.TokenStartIndex;
public JsonTokenType TokenType => reader.TokenType;
public int CurrentDepth => reader.CurrentDepth;
public string? GetString() => reader.GetString();
public bool TryGetInt64(out long value) => reader.TryGetInt64(out value);
public bool GetBool() => reader.GetBoolean();
}
}
internal static class JsonTokenTypeExtensions
{
public static bool IsBool(this JsonTokenType tokenType) => tokenType == JsonTokenType.True || tokenType == JsonTokenType.False;
public static bool IsInt(this JsonTokenType tokenType) => tokenType == JsonTokenType.Number;
}
}