-
Notifications
You must be signed in to change notification settings - Fork 411
Expand file tree
/
Copy pathJsonSerializer.cs
More file actions
88 lines (76 loc) · 3.13 KB
/
JsonSerializer.cs
File metadata and controls
88 lines (76 loc) · 3.13 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
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipelines;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace Docker.DotNet
{
/// <summary>
/// Facade for <see cref="System.Text.Json.JsonSerializer"/> serialization.
/// </summary>
internal class JsonSerializer
{
public JsonSerializer()
{
DockerDotnetJsonSerializerContext.PreserveReflection();
}
// Adapted from https://github.com/dotnet/runtime/issues/33030#issuecomment-1524227075
public async IAsyncEnumerable<T> Deserialize<T>(Stream stream, [EnumeratorCancellation] CancellationToken cancellationToken)
{
var reader = PipeReader.Create(stream);
while (true)
{
var result = await reader.ReadAsync(cancellationToken);
var buffer = result.Buffer;
while (!buffer.IsEmpty && TryParseJson(ref buffer, out var jsonDocument))
{
var deserializedObj = jsonDocument.Deserialize(typeof(T), DockerDotnetJsonSerializerContext.Default);
yield return (T) deserializedObj;
}
if (result.IsCompleted)
{
break;
}
reader.AdvanceTo(buffer.Start, buffer.End);
}
await reader.CompleteAsync();
}
private static bool TryParseJson(ref ReadOnlySequence<byte> buffer, out JsonDocument jsonDocument)
{
var reader = new Utf8JsonReader(buffer, isFinalBlock: false, default);
if (JsonDocument.TryParseValue(ref reader, out jsonDocument))
{
buffer = buffer.Slice(reader.BytesConsumed);
return true;
}
return false;
}
public T DeserializeObject<T>(byte[] json)
{
var deserializedObj = System.Text.Json.JsonSerializer.Deserialize(json, typeof(T), DockerDotnetJsonSerializerContext.Default);
return (T)deserializedObj;
}
public byte[] SerializeObject<T>(T value)
{
var jsonString = System.Text.Json.JsonSerializer.Serialize(value, typeof(T), DockerDotnetJsonSerializerContext.Default);
return Encoding.UTF8.GetBytes(jsonString);
}
public HttpContent GetHttpContent<T>(T value)
{
var jsonString = System.Text.Json.JsonSerializer.Serialize(value, typeof(T), DockerDotnetJsonSerializerContext.Default);
HttpContent httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");
return httpContent;
}
public async Task<T> DeserializeAsync<T>(HttpContent content, CancellationToken token)
{
var jsonString = await content.ReadAsStringAsync(token);
var deserializedObj = System.Text.Json.JsonSerializer.Deserialize(jsonString, typeof(T), DockerDotnetJsonSerializerContext.Default);
return (T)deserializedObj;
}
}
}