Skip to content

Commit 0bc8ad7

Browse files
authored
Change HttpRequest/ResponseJsonExtensions namespace (#23496)
1 parent a97746b commit 0bc8ad7

6 files changed

+62
-78
lines changed

src/Http/Http.Extensions/ref/Microsoft.AspNetCore.Http.Extensions.netcoreapp.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,24 @@ public static partial class HttpContextServerVariableExtensions
1313
{
1414
public static string GetServerVariable(this Microsoft.AspNetCore.Http.HttpContext context, string variableName) { throw null; }
1515
}
16-
public static partial class HttpRequestExtensions
16+
public static partial class HttpRequestJsonExtensions
1717
{
1818
public static bool HasJsonContentType(this Microsoft.AspNetCore.Http.HttpRequest request) { throw null; }
19+
[System.Diagnostics.DebuggerStepThroughAttribute]
20+
public static System.Threading.Tasks.ValueTask<object?> ReadFromJsonAsync(this Microsoft.AspNetCore.Http.HttpRequest request, System.Type type, System.Text.Json.JsonSerializerOptions? options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
21+
public static System.Threading.Tasks.ValueTask<object?> ReadFromJsonAsync(this Microsoft.AspNetCore.Http.HttpRequest request, System.Type type, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
22+
[System.Diagnostics.DebuggerStepThroughAttribute]
23+
public static System.Threading.Tasks.ValueTask<TValue> ReadFromJsonAsync<TValue>(this Microsoft.AspNetCore.Http.HttpRequest request, System.Text.Json.JsonSerializerOptions? options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
24+
public static System.Threading.Tasks.ValueTask<TValue> ReadFromJsonAsync<TValue>(this Microsoft.AspNetCore.Http.HttpRequest request, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
25+
}
26+
public static partial class HttpResponseJsonExtensions
27+
{
28+
public static System.Threading.Tasks.Task WriteAsJsonAsync(this Microsoft.AspNetCore.Http.HttpResponse response, object? value, System.Type type, System.Text.Json.JsonSerializerOptions? options, string? contentType, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
29+
public static System.Threading.Tasks.Task WriteAsJsonAsync(this Microsoft.AspNetCore.Http.HttpResponse response, object? value, System.Type type, System.Text.Json.JsonSerializerOptions? options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
30+
public static System.Threading.Tasks.Task WriteAsJsonAsync(this Microsoft.AspNetCore.Http.HttpResponse response, object? value, System.Type type, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
31+
public static System.Threading.Tasks.Task WriteAsJsonAsync<TValue>(this Microsoft.AspNetCore.Http.HttpResponse response, [System.Diagnostics.CodeAnalysis.AllowNullAttribute] TValue value, System.Text.Json.JsonSerializerOptions? options, string? contentType, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
32+
public static System.Threading.Tasks.Task WriteAsJsonAsync<TValue>(this Microsoft.AspNetCore.Http.HttpResponse response, [System.Diagnostics.CodeAnalysis.AllowNullAttribute] TValue value, System.Text.Json.JsonSerializerOptions? options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
33+
public static System.Threading.Tasks.Task WriteAsJsonAsync<TValue>(this Microsoft.AspNetCore.Http.HttpResponse response, [System.Diagnostics.CodeAnalysis.AllowNullAttribute] TValue value, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
1934
}
2035
public static partial class ResponseExtensions
2136
{
@@ -133,24 +148,6 @@ public void SetList<T>(string name, System.Collections.Generic.IList<T> values)
133148
}
134149
namespace Microsoft.AspNetCore.Http.Json
135150
{
136-
public static partial class HttpRequestJsonExtensions
137-
{
138-
[System.Diagnostics.DebuggerStepThroughAttribute]
139-
public static System.Threading.Tasks.ValueTask<object?> ReadFromJsonAsync(this Microsoft.AspNetCore.Http.HttpRequest request, System.Type type, System.Text.Json.JsonSerializerOptions? options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
140-
public static System.Threading.Tasks.ValueTask<object?> ReadFromJsonAsync(this Microsoft.AspNetCore.Http.HttpRequest request, System.Type type, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
141-
[System.Diagnostics.DebuggerStepThroughAttribute]
142-
public static System.Threading.Tasks.ValueTask<TValue> ReadFromJsonAsync<TValue>(this Microsoft.AspNetCore.Http.HttpRequest request, System.Text.Json.JsonSerializerOptions? options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
143-
public static System.Threading.Tasks.ValueTask<TValue> ReadFromJsonAsync<TValue>(this Microsoft.AspNetCore.Http.HttpRequest request, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
144-
}
145-
public static partial class HttpResponseJsonExtensions
146-
{
147-
public static System.Threading.Tasks.Task WriteAsJsonAsync(this Microsoft.AspNetCore.Http.HttpResponse response, object? value, System.Type type, System.Text.Json.JsonSerializerOptions? options, string? contentType, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
148-
public static System.Threading.Tasks.Task WriteAsJsonAsync(this Microsoft.AspNetCore.Http.HttpResponse response, object? value, System.Type type, System.Text.Json.JsonSerializerOptions? options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
149-
public static System.Threading.Tasks.Task WriteAsJsonAsync(this Microsoft.AspNetCore.Http.HttpResponse response, object? value, System.Type type, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
150-
public static System.Threading.Tasks.Task WriteAsJsonAsync<TValue>(this Microsoft.AspNetCore.Http.HttpResponse response, [System.Diagnostics.CodeAnalysis.AllowNullAttribute] TValue value, System.Text.Json.JsonSerializerOptions? options, string? contentType, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
151-
public static System.Threading.Tasks.Task WriteAsJsonAsync<TValue>(this Microsoft.AspNetCore.Http.HttpResponse response, [System.Diagnostics.CodeAnalysis.AllowNullAttribute] TValue value, System.Text.Json.JsonSerializerOptions? options, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
152-
public static System.Threading.Tasks.Task WriteAsJsonAsync<TValue>(this Microsoft.AspNetCore.Http.HttpResponse response, [System.Diagnostics.CodeAnalysis.AllowNullAttribute] TValue value, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
153-
}
154151
public partial class JsonOptions
155152
{
156153
public JsonOptions() { }

src/Http/Http.Extensions/src/HttpRequestExtensions.cs

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/Http/Http.Extensions/src/HttpRequestJsonExtensions.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
using System.Text.Json;
88
using System.Threading;
99
using System.Threading.Tasks;
10+
using Microsoft.AspNetCore.Http.Json;
1011
using Microsoft.Extensions.DependencyInjection;
1112
using Microsoft.Extensions.Options;
1213
using Microsoft.Extensions.Primitives;
14+
using Microsoft.Net.Http.Headers;
1315

1416
#nullable enable
1517

16-
namespace Microsoft.AspNetCore.Http.Json
18+
namespace Microsoft.AspNetCore.Http
1719
{
1820
public static class HttpRequestJsonExtensions
1921
{
@@ -137,6 +139,47 @@ public static async ValueTask<TValue> ReadFromJsonAsync<TValue>(
137139
}
138140
}
139141

142+
/// <summary>
143+
/// Checks the Content-Type header for JSON types.
144+
/// </summary>
145+
/// <returns>true if the Content-Type header represents a JSON content type; otherwise, false.</returns>
146+
public static bool HasJsonContentType(this HttpRequest request)
147+
{
148+
return request.HasJsonContentType(out _);
149+
}
150+
151+
private static bool HasJsonContentType(this HttpRequest request, out StringSegment charset)
152+
{
153+
if (request == null)
154+
{
155+
throw new ArgumentNullException(nameof(request));
156+
}
157+
158+
if (!MediaTypeHeaderValue.TryParse(request.ContentType, out var mt))
159+
{
160+
charset = StringSegment.Empty;
161+
return false;
162+
}
163+
164+
// Matches application/json
165+
if (mt.MediaType.Equals(JsonConstants.JsonContentType, StringComparison.OrdinalIgnoreCase))
166+
{
167+
charset = mt.Charset;
168+
return true;
169+
}
170+
171+
// Matches +json, e.g. application/ld+json
172+
if (mt.Suffix.Equals("json", StringComparison.OrdinalIgnoreCase))
173+
{
174+
charset = mt.Charset;
175+
return true;
176+
}
177+
178+
charset = StringSegment.Empty;
179+
return false;
180+
}
181+
182+
140183
private static JsonSerializerOptions ResolveSerializerOptions(HttpContext httpContext)
141184
{
142185
// Attempt to resolve options from DI then fallback to default options

src/Http/Http.Extensions/src/HttpResponseJsonExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
using System.Text.Json;
77
using System.Threading;
88
using System.Threading.Tasks;
9+
using Microsoft.AspNetCore.Http.Json;
910
using Microsoft.Extensions.DependencyInjection;
1011
using Microsoft.Extensions.Options;
1112

1213
#nullable enable
1314

14-
namespace Microsoft.AspNetCore.Http.Json
15+
namespace Microsoft.AspNetCore.Http
1516
{
1617
public static partial class HttpResponseJsonExtensions
1718
{

src/Http/Http.Extensions/test/HttpRequestJsonExtensionsTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Text.Json;
99
using System.Threading;
1010
using System.Threading.Tasks;
11-
using Microsoft.AspNetCore.Http.Json;
1211
using Xunit;
1312

1413
#nullable enable

src/Http/Http.Extensions/test/HttpResponseJsonExtensionsTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Diagnostics.CodeAnalysis;
65
using System.IO;
76
using System.Text;
87
using System.Text.Json;
98
using System.Text.Json.Serialization;
109
using System.Threading;
1110
using System.Threading.Tasks;
12-
using Microsoft.AspNetCore.Http.Json;
1311
using Xunit;
1412

1513
#nullable enable

0 commit comments

Comments
 (0)