Skip to content

Commit

Permalink
feat: add JsonSerializerOptions to JsonAsync (microsoft#2817) (micros…
Browse files Browse the repository at this point in the history
  • Loading branch information
Exoow authored Jan 12, 2024
1 parent e8117f0 commit dbeac36
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/Playwright.Tests/BrowserContextFetchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,19 @@ record SimpleObject
public string Foo { get; set; }
}

[PlaywrightTest("", "should parse response JSON while passing a type with serializer options")]
public async Task ShouldParseResponseJSONWhilePassingATypeWithSerializerOptions()
{
var response = await Context.APIRequest.GetAsync(Server.Prefix + "/simple.json");
var json = await response.JsonAsync<SimpleObject>(new() { PropertyNameCaseInsensitive = true });
Assert.AreEqual("bar", json.Foo);
}

record SimplerObject
{
public string Foo { get; set; }
}

[PlaywrightTest("browsercontext-fetch.spec.ts", "should accept bool and numeric params")]
public async Task ShouldAcceptBoolAndNumericParams()
{
Expand Down
5 changes: 4 additions & 1 deletion src/Playwright/API/Supplements/IAPIResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
*/

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

#nullable enable

namespace Microsoft.Playwright;

public partial interface IAPIResponse : IAsyncDisposable
{
Task<T> JsonAsync<T>();
Task<T?> JsonAsync<T>(JsonSerializerOptions? options = null);
}
4 changes: 3 additions & 1 deletion src/Playwright/Core/APIResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
using System.Threading.Tasks;
using Microsoft.Playwright.Helpers;

#nullable enable

namespace Microsoft.Playwright.Core;

internal class APIResponse : IAPIResponse
Expand Down Expand Up @@ -77,7 +79,7 @@ public async Task<byte[]> BodyAsync()

public async Task<JsonElement?> JsonAsync() => JsonSerializer.Deserialize<JsonElement>(await BodyAsync().ConfigureAwait(false));

public async Task<T> JsonAsync<T>() => JsonSerializer.Deserialize<T>(await BodyAsync().ConfigureAwait(false));
public async Task<T?> JsonAsync<T>(JsonSerializerOptions? options) => JsonSerializer.Deserialize<T>(await BodyAsync().ConfigureAwait(false), options);

public async Task<string> TextAsync()
{
Expand Down

0 comments on commit dbeac36

Please sign in to comment.