Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
JsonContent.cs
Go to the documentation of this file.
2using System.IO;
4using System.Text;
9
10namespace System.Net.Http.Json;
11
12public sealed class JsonContent : HttpContent
13{
15
16 public Type ObjectType { get; }
17
18 public object? Value { get; }
19
20 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
22 {
23 if (inputType == null)
24 {
25 throw new ArgumentNullException("inputType");
26 }
27 if (inputValue != null && !inputType.IsAssignableFrom(inputValue.GetType()))
28 {
30 }
35 }
36
37 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
39 {
40 return Create(inputValue, typeof(T), mediaType, options);
41 }
42
43 [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
45 {
46 return new JsonContent(inputValue, inputType, mediaType, options);
47 }
48
53
54 protected override bool TryComputeLength(out long length)
55 {
56 length = 0L;
57 return false;
58 }
59
60 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "The ctor is annotated with RequiresUnreferencedCode.")]
62 {
63 Encoding encoding = JsonHelpers.GetEncoding(base.Headers.ContentType?.CharSet);
64 if (encoding != null && encoding != Encoding.UTF8)
65 {
67 try
68 {
69 if (async)
70 {
72 }
73 else
74 {
76 }
77 }
78 finally
79 {
80 if (async)
81 {
82 await transcodingStream.DisposeAsync().ConfigureAwait(continueOnCapturedContext: false);
83 }
84 else
85 {
86 transcodingStream.Dispose();
87 }
88 }
89 }
90 else if (async)
91 {
93 }
94 else
95 {
97 }
98 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Workaround for https://github.com/mono/linker/issues/1416. The outer method is marked as RequiresUnreferencedCode.")]
100 {
102 }
103 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Workaround for https://github.com/mono/linker/issues/1416. The outer method is marked as RequiresUnreferencedCode.")]
105 {
107 }
108 }
109
111 {
112 SerializeToStreamAsyncCore(stream, async: false, cancellationToken).GetAwaiter().GetResult();
113 }
114
119}
120internal sealed class JsonContent<TValue> : HttpContent
121{
123
124 private readonly TValue _typedValue;
125
132
137
138 protected override bool TryComputeLength(out long length)
139 {
140 length = 0L;
141 return false;
142 }
143
145 {
146 Encoding encoding = JsonHelpers.GetEncoding(base.Headers.ContentType?.CharSet);
147 if (encoding != null && encoding != Encoding.UTF8)
148 {
150 try
151 {
152 if (async)
153 {
155 }
156 else
157 {
159 }
160 }
161 finally
162 {
163 if (async)
164 {
165 await transcodingStream.DisposeAsync().ConfigureAwait(continueOnCapturedContext: false);
166 }
167 else
168 {
169 transcodingStream.Dispose();
170 }
171 }
172 }
173 else if (async)
174 {
176 }
177 else
178 {
180 }
181 }
182
184 {
185 SerializeToStreamAsyncCore(stream, async: false, cancellationToken).GetAwaiter().GetResult();
186 }
187
192}
override Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken)
JsonContent(TValue inputValue, JsonTypeInfo< TValue > jsonTypeInfo)
static JsonContent Create(object? inputValue, Type inputType, MediaTypeHeaderValue? mediaType=null, JsonSerializerOptions? options=null)
async Task SerializeToStreamAsyncCore(Stream targetStream, bool async, CancellationToken cancellationToken)
override Task SerializeToStreamAsync(Stream stream, TransportContext context)
readonly JsonSerializerOptions _jsonSerializerOptions
readonly JsonTypeInfo< TValue > _typeInfo
override void SerializeToStream(Stream stream, TransportContext? context, CancellationToken cancellationToken)
JsonContent(object inputValue, Type inputType, MediaTypeHeaderValue mediaType, JsonSerializerOptions options)
override void SerializeToStream(Stream stream, TransportContext context, CancellationToken cancellationToken)
static JsonContent Create< T >(T inputValue, MediaTypeHeaderValue? mediaType=null, JsonSerializerOptions? options=null)
override bool TryComputeLength(out long length)
override Task SerializeToStreamAsync(Stream stream, TransportContext context, CancellationToken cancellationToken)
override Task SerializeToStreamAsync(Stream stream, TransportContext? context)
static Encoding GetEncoding(string charset)
static MediaTypeHeaderValue GetDefaultMediaType()
static readonly JsonSerializerOptions s_defaultSerializerOptions
Definition JsonHelpers.cs:9
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string SerializeWrongType
Definition SR.cs:16
Definition SR.cs:7
static Encoding UTF8
Definition Encoding.cs:526
static Stream CreateTranscodingStream(Stream innerStream, Encoding innerStreamEncoding, Encoding outerStreamEncoding, bool leaveOpen=false)
Definition Encoding.cs:1046
static Task SerializeAsync(Stream utf8Json, object? value, Type inputType, JsonSerializerOptions? options=null, CancellationToken cancellationToken=default(CancellationToken))
static void Serialize(Stream utf8Json, object? value, Type inputType, JsonSerializerOptions? options=null)