Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ public static partial class HttpClientJsonExtensions
public static System.Threading.Tasks.Task<TValue?> GetFromJsonAsync<TValue>(this System.Net.Http.HttpClient client, System.Uri? requestUri, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.Task<TValue?> GetFromJsonAsync<TValue>(this System.Net.Http.HttpClient client, System.Uri? requestUri, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsJsonAsync<TValue>(this System.Net.Http.HttpClient client, string? requestUri, TValue value, System.Text.Json.JsonSerializerOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsJsonAsync<TValue>(this System.Net.Http.HttpClient client, string? requestUri, TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsJsonAsync<TValue>(this System.Net.Http.HttpClient client, string? requestUri, TValue value, System.Threading.CancellationToken cancellationToken) { throw null; }
public static System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsJsonAsync<TValue>(this System.Net.Http.HttpClient client, System.Uri? requestUri, TValue value, System.Text.Json.JsonSerializerOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsJsonAsync<TValue>(this System.Net.Http.HttpClient client, System.Uri? requestUri, TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PostAsJsonAsync<TValue>(this System.Net.Http.HttpClient client, System.Uri? requestUri, TValue value, System.Threading.CancellationToken cancellationToken) { throw null; }
public static System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PutAsJsonAsync<TValue>(this System.Net.Http.HttpClient client, string? requestUri, TValue value, System.Text.Json.JsonSerializerOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PutAsJsonAsync<TValue>(this System.Net.Http.HttpClient client, string? requestUri, TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PutAsJsonAsync<TValue>(this System.Net.Http.HttpClient client, string? requestUri, TValue value, System.Threading.CancellationToken cancellationToken) { throw null; }
public static System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PutAsJsonAsync<TValue>(this System.Net.Http.HttpClient client, System.Uri? requestUri, TValue value, System.Text.Json.JsonSerializerOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PutAsJsonAsync<TValue>(this System.Net.Http.HttpClient client, System.Uri? requestUri, TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
public static System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> PutAsJsonAsync<TValue>(this System.Net.Http.HttpClient client, System.Uri? requestUri, TValue value, System.Threading.CancellationToken cancellationToken) { throw null; }
}
public static partial class HttpContentJsonExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<argument>ILLink</argument>
<argument>IL2072</argument>
<property name="Scope">member</property>
<property name="Target">M:System.Net.Http.Json.JsonContent.&lt;SerializeToStreamAsyncCore&gt;d__18.MoveNext</property>
<property name="Target">M:System.Net.Http.Json.JsonContent.&lt;SerializeToStreamAsyncCore&gt;d__13.MoveNext</property>
</attribute>
<attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
<argument>ILLink</argument>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);net5.0;netstandard2.0;net461</TargetFrameworks>
<ExcludeCurrentNetCoreAppFromPackage>true</ExcludeCurrentNetCoreAppFromPackage>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="System\Net\Http\Json\JsonHelpers.cs" />
<Compile Include="System\Net\Http\Json\HttpClientJsonExtensions.Get.cs" />
<Compile Include="System\Net\Http\Json\HttpClientJsonExtensions.Post.cs" />
<Compile Include="System\Net\Http\Json\HttpClientJsonExtensions.Put.cs" />
<Compile Include="System\Net\Http\Json\HttpContentJsonExtensions.cs" />
<Compile Include="System\Net\Http\Json\JsonContent.cs" />
<Compile Include="System\Net\Http\Json\JsonContentOfT.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'net461'">
<Compile Include="System\Net\Http\Json\HttpContentJsonExtensions.netcoreapp.cs" />
<Compile Include="System\Net\Http\Json\JsonContent.netcoreapp.cs" />
<Compile Include="System\Net\Http\Json\JsonContentOfT.netcoreapp.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or
$(TargetFramework.StartsWith('net4'))">
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or $(TargetFramework.StartsWith('net4'))">
<Compile Include="System\ArraySegmentExtensions.netstandard.cs" />
<Compile Include="System\Net\Http\Json\HttpContentJsonExtensions.netstandard.cs" />
<Compile Include="System\Net\Http\Json\TranscodingReadStream.cs" />
Expand All @@ -30,8 +32,7 @@
<Reference Include="System.Runtime" />
<Reference Include="System.Threading.Tasks.Extensions" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or
$(TargetFramework.StartsWith('net4'))">
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or $(TargetFramework.StartsWith('net4'))">
<PackageReference Include="System.Buffers" Version="$(SystemBuffersVersion)" />
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="$(SystemThreadingTasksExtensionsVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -36,5 +37,27 @@ public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this HttpClient

public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this HttpClient client, Uri? requestUri, TValue value, CancellationToken cancellationToken)
=> client.PostAsJsonAsync(requestUri, value, options: null, cancellationToken);

public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this HttpClient client, string? requestUri, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}

JsonContent<TValue> content = new(value, jsonTypeInfo);
return client.PostAsync(requestUri, content, cancellationToken);
}

public static Task<HttpResponseMessage> PostAsJsonAsync<TValue>(this HttpClient client, Uri? requestUri, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}

JsonContent<TValue> content = new(value, jsonTypeInfo);
return client.PostAsync(requestUri, content, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -36,5 +37,27 @@ public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this HttpClient c

public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this HttpClient client, Uri? requestUri, TValue value, CancellationToken cancellationToken)
=> client.PutAsJsonAsync(requestUri, value, options: null, cancellationToken);

public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this HttpClient client, string? requestUri, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}

JsonContent<TValue> content = new(value, jsonTypeInfo);
return client.PutAsync(requestUri, content, cancellationToken);
}

public static Task<HttpResponseMessage> PutAsJsonAsync<TValue>(this HttpClient client, Uri? requestUri, TValue value, JsonTypeInfo<TValue> jsonTypeInfo, CancellationToken cancellationToken = default)
{
if (client == null)
{
throw new ArgumentNullException(nameof(client));
}

JsonContent<TValue> content = new(value, jsonTypeInfo);
return client.PutAsync(requestUri, content, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static partial class HttpContentJsonExtensions
throw new ArgumentNullException(nameof(content));
}

Encoding? sourceEncoding = JsonContent.GetEncoding(content.Headers.ContentType?.CharSet);
Encoding? sourceEncoding = JsonHelpers.GetEncoding(content.Headers.ContentType?.CharSet);

return ReadFromJsonAsyncCore(content, type, sourceEncoding, options, cancellationToken);
}
Expand All @@ -32,7 +32,7 @@ public static partial class HttpContentJsonExtensions
throw new ArgumentNullException(nameof(content));
}

Encoding? sourceEncoding = JsonContent.GetEncoding(content.Headers.ContentType?.CharSet);
Encoding? sourceEncoding = JsonHelpers.GetEncoding(content.Headers.ContentType?.CharSet);

return ReadFromJsonAsyncCore<T>(content, sourceEncoding, options, cancellationToken);
}
Expand Down Expand Up @@ -60,7 +60,7 @@ public static partial class HttpContentJsonExtensions
throw new ArgumentNullException(nameof(content));
}

Encoding? sourceEncoding = JsonContent.GetEncoding(content.Headers.ContentType?.CharSet);
Encoding? sourceEncoding = JsonHelpers.GetEncoding(content.Headers.ContentType?.CharSet);

return ReadFromJsonAsyncCore(content, type, sourceEncoding, context, cancellationToken);
}
Expand All @@ -72,7 +72,7 @@ public static partial class HttpContentJsonExtensions
throw new ArgumentNullException(nameof(content));
}

Encoding? sourceEncoding = JsonContent.GetEncoding(content.Headers.ContentType?.CharSet);
Encoding? sourceEncoding = JsonHelpers.GetEncoding(content.Headers.ContentType?.CharSet);

return ReadFromJsonAsyncCore<T>(content, sourceEncoding, jsonTypeInfo, cancellationToken);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NETCOREAPP
using System.Diagnostics;
#endif
using System.IO;
using System.Net.Http.Headers;
using System.Text;
Expand All @@ -13,12 +15,6 @@ namespace System.Net.Http.Json
{
public sealed partial class JsonContent : HttpContent
{
internal const string JsonMediaType = "application/json";
internal const string JsonType = "application";
internal const string JsonSubtype = "json";
private static MediaTypeHeaderValue DefaultMediaType
=> new MediaTypeHeaderValue(JsonMediaType) { CharSet = "utf-8" };

internal static readonly JsonSerializerOptions s_defaultSerializerOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web);

private readonly JsonSerializerOptions? _jsonSerializerOptions;
Expand All @@ -39,7 +35,7 @@ private JsonContent(object? inputValue, Type inputType, MediaTypeHeaderValue? me

Value = inputValue;
ObjectType = inputType;
Headers.ContentType = mediaType ?? DefaultMediaType;
Headers.ContentType = mediaType ?? JsonHelpers.GetDefaultMediaType();
_jsonSerializerOptions = options ?? s_defaultSerializerOptions;
}

Expand All @@ -60,7 +56,7 @@ protected override bool TryComputeLength(out long length)

private async Task SerializeToStreamAsyncCore(Stream targetStream, bool async, CancellationToken cancellationToken)
{
Encoding? targetEncoding = GetEncoding(Headers.ContentType?.CharSet);
Encoding? targetEncoding = JsonHelpers.GetEncoding(Headers.ContentType?.CharSet);

// Wrap provided stream into a transcoding stream that buffers the data transcoded from utf-8 to the targetEncoding.
if (targetEncoding != null && targetEncoding != Encoding.UTF8)
Expand Down Expand Up @@ -126,34 +122,5 @@ private async Task SerializeToStreamAsyncCore(Stream targetStream, bool async, C
}
}
}

internal static Encoding? GetEncoding(string? charset)
{
Encoding? encoding = null;

if (charset != null)
{
try
{
// Remove at most a single set of quotes.
if (charset.Length > 2 && charset[0] == '\"' && charset[charset.Length - 1] == '\"')
{
encoding = Encoding.GetEncoding(charset.Substring(1, charset.Length - 2));
}
else
{
encoding = Encoding.GetEncoding(charset);
}
}
catch (ArgumentException e)
{
throw new InvalidOperationException(SR.CharSetInvalid, e);
}

Debug.Assert(encoding != null);
}

return encoding;
}
}
}
Loading