-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add JSON extension methods to request and response #21731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <Project> | ||
| <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory)..\, Directory.Build.props))\Directory.Build.props" /> | ||
|
|
||
| <PropertyGroup> | ||
| <Nullable>annotations</Nullable> | ||
| </PropertyGroup> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System; | ||
| using Microsoft.Extensions.Primitives; | ||
| using Microsoft.Net.Http.Headers; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Microsoft.AspNetCore.Http | ||
| { | ||
| public static class HttpRequestExtensions | ||
| { | ||
| /// <summary> | ||
| /// Checks the Content-Type header for JSON types. | ||
| /// </summary> | ||
| /// <returns>true if the Content-Type header represents a JSON content type; otherwise, false.</returns> | ||
| public static bool HasJsonContentType(this HttpRequest request) | ||
| { | ||
| return request.HasJsonContentType(out _); | ||
| } | ||
|
|
||
| internal static bool HasJsonContentType(this HttpRequest request, out StringSegment charset) | ||
| { | ||
| if (request == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(request)); | ||
| } | ||
|
|
||
| if (!MediaTypeHeaderValue.TryParse(request.ContentType, out var mt)) | ||
| { | ||
| charset = StringSegment.Empty; | ||
| return false; | ||
| } | ||
|
|
||
| // Matches application/json | ||
| if (mt.MediaType.Equals(JsonConstants.JsonContentType, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| charset = mt.Charset; | ||
| return true; | ||
| } | ||
|
|
||
| // Matches +json, e.g. application/ld+json | ||
| if (mt.Suffix.Equals("json", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| charset = mt.Charset; | ||
| return true; | ||
| } | ||
|
|
||
| charset = StringSegment.Empty; | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
182 changes: 182 additions & 0 deletions
182
src/Http/Http.Extensions/src/HttpRequestJsonExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Options; | ||
| using Microsoft.Extensions.Primitives; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Microsoft.AspNetCore.Http.Json | ||
| { | ||
| public static class HttpRequestJsonExtensions | ||
| { | ||
| /// <summary> | ||
| /// Read JSON from the request and deserialize to the specified type. | ||
| /// If the request's content-type is not a known JSON type then an error will be thrown. | ||
| /// </summary> | ||
| /// <typeparam name="TValue">The type of object to read.</typeparam> | ||
| /// <param name="request">The request to read from.</param> | ||
| /// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel the operation.</param> | ||
| /// <returns>The task object representing the asynchronous operation.</returns> | ||
| public static ValueTask<TValue> ReadFromJsonAsync<TValue>( | ||
| this HttpRequest request, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| return request.ReadFromJsonAsync<TValue>(options: null, cancellationToken); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Read JSON from the request and deserialize to the specified type. | ||
| /// If the request's content-type is not a known JSON type then an error will be thrown. | ||
| /// </summary> | ||
| /// <typeparam name="TValue">The type of object to read.</typeparam> | ||
| /// <param name="request">The request to read from.</param> | ||
| /// <param name="options">The serializer options use when deserializing the content.</param> | ||
| /// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel the operation.</param> | ||
| /// <returns>The task object representing the asynchronous operation.</returns> | ||
| public static async ValueTask<TValue> ReadFromJsonAsync<TValue>( | ||
| this HttpRequest request, | ||
| JsonSerializerOptions? options, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| if (request == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(request)); | ||
| } | ||
|
|
||
| if (!request.HasJsonContentType(out var charset)) | ||
| { | ||
| throw CreateContentTypeError(request); | ||
| } | ||
|
|
||
| options ??= ResolveSerializerOptions(request.HttpContext); | ||
|
|
||
| var encoding = GetEncodingFromCharset(charset); | ||
| var (inputStream, usesTranscodingStream) = GetInputStream(request.HttpContext, encoding); | ||
|
|
||
| try | ||
| { | ||
| return await JsonSerializer.DeserializeAsync<TValue>(inputStream, options, cancellationToken); | ||
| } | ||
| finally | ||
| { | ||
| if (usesTranscodingStream) | ||
| { | ||
| await inputStream.DisposeAsync(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Read JSON from the request and deserialize to the specified type. | ||
| /// If the request's content-type is not a known JSON type then an error will be thrown. | ||
| /// </summary> | ||
| /// <param name="request">The request to read from.</param> | ||
| /// <param name="type">The type of object to read.</param> | ||
| /// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel the operation.</param> | ||
| /// <returns>The task object representing the asynchronous operation.</returns> | ||
| public static ValueTask<object?> ReadFromJsonAsync( | ||
| this HttpRequest request, | ||
| Type type, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| return request.ReadFromJsonAsync(type, options: null, cancellationToken); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Read JSON from the request and deserialize to the specified type. | ||
| /// If the request's content-type is not a known JSON type then an error will be thrown. | ||
| /// </summary> | ||
| /// <param name="request">The request to read from.</param> | ||
| /// <param name="type">The type of object to read.</param> | ||
| /// <param name="options">The serializer options use when deserializing the content.</param> | ||
| /// <param name="cancellationToken">A <see cref="CancellationToken"/> used to cancel the operation.</param> | ||
| /// <returns>The task object representing the asynchronous operation.</returns> | ||
| public static async ValueTask<object?> ReadFromJsonAsync( | ||
| this HttpRequest request, | ||
| Type type, | ||
| JsonSerializerOptions? options, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| if (request == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(request)); | ||
| } | ||
| if (type == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(type)); | ||
| } | ||
|
|
||
| if (!request.HasJsonContentType(out var charset)) | ||
| { | ||
| throw CreateContentTypeError(request); | ||
| } | ||
|
|
||
| options ??= ResolveSerializerOptions(request.HttpContext); | ||
|
|
||
| var encoding = GetEncodingFromCharset(charset); | ||
| var (inputStream, usesTranscodingStream) = GetInputStream(request.HttpContext, encoding); | ||
|
|
||
| try | ||
| { | ||
| return await JsonSerializer.DeserializeAsync(inputStream, type, options, cancellationToken); | ||
| } | ||
| finally | ||
| { | ||
| if (usesTranscodingStream) | ||
| { | ||
| await inputStream.DisposeAsync(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static JsonSerializerOptions ResolveSerializerOptions(HttpContext httpContext) | ||
| { | ||
| // Attempt to resolve options from DI then fallback to default options | ||
| return httpContext.RequestServices?.GetService<IOptions<JsonOptions>>()?.Value?.SerializerOptions ?? JsonOptions.DefaultSerializerOptions; | ||
| } | ||
|
|
||
| private static InvalidOperationException CreateContentTypeError(HttpRequest request) | ||
| { | ||
| return new InvalidOperationException($"Unable to read the request as JSON because the request content type '{request.ContentType}' is not a known JSON content type."); | ||
| } | ||
|
|
||
| private static (Stream inputStream, bool usesTranscodingStream) GetInputStream(HttpContext httpContext, Encoding? encoding) | ||
| { | ||
| if (encoding == null || encoding.CodePage == Encoding.UTF8.CodePage) | ||
| { | ||
| return (httpContext.Request.Body, false); | ||
| } | ||
|
|
||
| var inputStream = Encoding.CreateTranscodingStream(httpContext.Request.Body, encoding, Encoding.UTF8, leaveOpen: true); | ||
| return (inputStream, true); | ||
| } | ||
|
|
||
| private static Encoding? GetEncodingFromCharset(StringSegment charset) | ||
| { | ||
| if (charset.Equals("utf-8", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| // This is an optimization for utf-8 that prevents the Substring caused by | ||
| // charset.Value | ||
| return Encoding.UTF8; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| // charset.Value might be an invalid encoding name as in charset=invalid. | ||
| return charset.HasValue ? Encoding.GetEncoding(charset.Value) : null; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| throw new InvalidOperationException($"Unable to read the request as JSON because the request content type charset '{charset}' is not a known encoding.", ex); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.