Skip to content

Commit e8eac9a

Browse files
kblokactions-userjnyrupcampersau
authored
Migrate to system.text.json (#2713)
* Some progress * More progress * more progress * More progress * Docs changes * Library builds * More progress * Solution builds * Docs changes * format * It seems that the demo runs * Fix expectation loading * Fix accessibility tests * Fix tests using json * Docs changes * Fix aria tests and RemoteObject type checks * Fix more accessibility steps and chrome version checker * more json fixes * Fix enum parser * Fix page event test * fix bounding box * Fix CSS Coverage Test * Fix console tests * Docs changes * Add enum member to MouseButton * Improve enums * Fix tracing tests * Fix JS coverage tests * Fix json tests * Fix SetUserAgentTests * Fix QueryAllTests * Fix security details * Fix keyboard tests * Fix ResponseJsonTests * Fix ElementHandleScreenshotTests * Some early review * Internal sealed converters * fix condition * Make visible to the Nunit project * Fix touch tests * Fix JS Coverage tests * Fix screenshot rounding * Fix evaluate with nulls * Fix evaluation exceptions * Fix complex objects test * Fix JS Handle validation * Fix complex object tests * Remove Mock verbosity * Fix wait for frame * Fix console logs * Fix PageWorkerTests * Convert context id in Firefox * Flag beta version * Restore some json config * undo a change * We don't need this * AOT Support! * Remove extra types * Small .NET framework test * Test AOT * AOT only on net8.0 * .NET Framework does not like generic attributes * remove remaining newtonsoft * remove unused imports * Docs changes * some styles * internal sealed * Include System.Text.Json only for net standard * remove extra constructor * Update lib/PuppeteerSharp/Helpers/Json/AnyTypeToStringConverter.cs Co-authored-by: Jonas Nyrup <[email protected]> * cr * cr * Way too generic serializing attribute * Update lib/PuppeteerSharp.Nunit/PuppeteerTestAttribute.cs Co-authored-by: campersau <[email protected]> * Improve serialization * Add more types * rollback AOT by default * AOT tests * Docs changes * beta2 * Revert "AOT tests" This reverts commit 0d94b1b. * You can't AOT a test * Docs changes * Add AOT demo --------- Co-authored-by: GitHub Action <[email protected]> Co-authored-by: Jonas Nyrup <[email protected]> Co-authored-by: campersau <[email protected]>
1 parent c4c67db commit e8eac9a

File tree

264 files changed

+1433
-1119
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

264 files changed

+1433
-1119
lines changed

.github/workflows/demo.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,17 @@ jobs:
3535
working-directory: ./demos/PuppeteerSharpPdfDemo
3636
run: |
3737
dotnet restore PuppeteerSharpPdfDemo-Local.csproj
38-
- name: Run Project
38+
- name: Run on .NET
3939
working-directory: ./demos/PuppeteerSharpPdfDemo
4040
run: |
41-
dotnet run --project PuppeteerSharpPdfDemo-Local.csproj auto-exit
41+
dotnet run --project PuppeteerSharpPdfDemo-Local.csproj auto-exit -f net8.0
42+
- name: Run with AOT
43+
if: matrix.os == 'macos-latest'
44+
working-directory: ./demos/PuppeteerSharpPdfDemo
45+
run: |
46+
dotnet run --project PuppeteerSharpPdfDemo-Local.csproj -r osx-arm64 -c Release -f net8.0 auto-exit
47+
- name: Run on .NET Framework
48+
if: matrix.os == 'windows-2022'
49+
working-directory: ./demos/PuppeteerSharpPdfDemo
50+
run: |
51+
dotnet run --project PuppeteerSharpPdfDemo-Local.csproj auto-exit -f net471

.github/workflows/dotnet.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ jobs:
8484
Get-ChildItem -Path cert:\CurrentUSer\my | where { $_.friendlyname -eq "Puppeteer" } | Export-Certificate -FilePath $env:GITHUB_WORKSPACE\lib\PuppeteerSharp.TestServer\testCert.cer
8585
- name: Check formatting
8686
if: ${{ matrix.os == 'ubuntu-latest' && matrix.browser == 'CHROME' && matrix.mode == 'headless' }}
87-
run: dotnet format ./lib/PuppeteerSharp.sln --verify-no-changes
87+
run: dotnet format ./lib/PuppeteerSharp.sln --verify-no-changes --exclude-diagnostics CA1865
8888
- name: Build
8989
working-directory: lib
9090
run: dotnet build PuppeteerSharp.sln

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ var result = await page.GetContentAsync();
112112
```cs
113113
await using var page = await browser.NewPageAsync();
114114
var seven = await page.EvaluateExpressionAsync<int>("4 + 3");
115-
var someObject = await page.EvaluateFunctionAsync<dynamic>("(value) => ({a: value})", 5);
116-
Console.WriteLine(someObject.a);
115+
var someObject = await page.EvaluateFunctionAsync<JsonElement>("(value) => ({a: value})", 5);
116+
Console.WriteLine(someObject.GetProperty("a").GetString());
117117
```
118-
<sup><a href='https://github.com/hardkoded/puppeteer-sharp/blob/master/lib/PuppeteerSharp.Tests/QuerySelectorTests/ElementHandleQuerySelectorEvalTests.cs#L16-L21' title='Snippet source file'>snippet source</a> | <a href='#snippet-Evaluate' title='Start of snippet'>anchor</a></sup>
118+
<sup><a href='https://github.com/hardkoded/puppeteer-sharp/blob/master/lib/PuppeteerSharp.Tests/QuerySelectorTests/ElementHandleQuerySelectorEvalTests.cs#L17-L22' title='Snippet source file'>snippet source</a> | <a href='#snippet-Evaluate' title='Start of snippet'>anchor</a></sup>
119119
<!-- endSnippet -->
120120

121121
### Wait For Selector

demos/PuppeteerSharpPdfDemo/Program.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Linq;
33
using System.IO;
4+
using System.Text.Json.Serialization;
5+
using System.Text.Json.Serialization.Metadata;
46
using System.Threading.Tasks;
57
using PuppeteerSharp;
68

@@ -10,6 +12,10 @@ class MainClass
1012
{
1113
public static async Task Main(string[] args)
1214
{
15+
#if NET8_0_OR_GREATER
16+
Puppeteer.ExtraJsonSerializerContext = DemoJsonSerializationContext.Default;
17+
#endif
18+
1319
var options = new LaunchOptions { Headless = true };
1420

1521
Console.WriteLine("Downloading chromium");
@@ -28,10 +34,26 @@ public static async Task Main(string[] args)
2834

2935
Console.WriteLine("Export completed");
3036

37+
#if NET8_0_OR_GREATER
38+
// AOT Test
39+
var result = await page.EvaluateFunctionAsync<TestClass>("test => test", new TestClass { Name = "Dario"});
40+
Console.WriteLine($"Name evaluated to {result.Name}");
41+
#endif
3142
if (!args.Any(arg => arg == "auto-exit"))
3243
{
3344
Console.ReadLine();
3445
}
3546
}
3647
}
48+
49+
#if NET8_0_OR_GREATER
50+
public class TestClass
51+
{
52+
public string Name { get; set; }
53+
}
54+
55+
[JsonSerializable(typeof(TestClass))]
56+
public partial class DemoJsonSerializationContext : JsonSerializerContext
57+
{}
58+
#endif
3759
}

demos/PuppeteerSharpPdfDemo/PuppeteerSharpPdfDemo-Local.csproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
32
<PropertyGroup>
43
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
6-
<LangVersion>8.0</LangVersion>
4+
<TargetFrameworks>net8.0;net471</TargetFrameworks>
5+
<LangVersion>12</LangVersion>
6+
</PropertyGroup>
7+
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
8+
<PublishAot>true</PublishAot>
79
</PropertyGroup>
810
<ItemGroup>
911
<ProjectReference Include="..\..\lib\PuppeteerSharp\PuppeteerSharp.csproj" />

lib/.editorconfig

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,18 @@ dotnet_diagnostic.CA1054.severity = none
247247
dotnet_diagnostic.CA1056.severity = none
248248
# Avoid empty interfaces
249249
dotnet_diagnostic.CA1040.severity = none
250+
# Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance
251+
# This is not netstandard compatible
252+
dotnet_diagnostic.CA1510.severity = none
253+
# Use 'string.StartsWith(char)' instead of 'string.StartsWith(string)' when you have a string with a single char
254+
# This is not .NET 8 compatible
255+
dotnet_diagnostic.CA1865.severity = none
256+
# Use 'ObjectDisposedException.ThrowIf' instead of explicitly throwing a new exception instance
257+
# This is not netstandard compatible
258+
dotnet_diagnostic.CA1513.severity = none
259+
# Use Change the 'WriteAsync' method call to use the 'Stream.WriteAsync(ReadOnlyMemory<byte>, CancellationToken)' overload
260+
# This is not netstandard compatible
261+
dotnet_diagnostic.CA1835.severity = none
250262
# Use ConfigureAwait
251263
dotnet_diagnostic.CA2007.severity = error
252264
# CA1711: Identifiers should not have incorrect suffix
@@ -264,6 +276,8 @@ dotnet_diagnostic.CA1815.severity = suggestion
264276
# CA1848: Use the LoggerMessage delegates
265277
# TODO: REMOVE
266278
dotnet_diagnostic.CA1848.severity = none
279+
# internal sealed
280+
dotnet_diagnostic.CA1852.severity = error
267281
# CA1859: Use concrete types when possible for improved performance
268282
# It collides with https://www.jetbrains.com/help/rider/ReturnTypeCanBeEnumerable.Local.html
269283
dotnet_diagnostic.CA1859.severity = none
@@ -303,7 +317,7 @@ dotnet_diagnostic.CA1001.severity = error
303317
dotnet_diagnostic.CA1304.severity = error
304318
# CA1305: String.Format with culture
305319
dotnet_diagnostic.CA1305.severity = error
306-
# CA1304: CA1308: Normalize strings to uppercase
320+
# CA1308: Normalize strings to uppercase
307321
dotnet_diagnostic.CA1308.severity = none
308322
# CA1721: The property name 'DefaultArgs' is confusing given the existence of method 'GetDefaultArgs'. Rename or remove one of these members.
309323
dotnet_diagnostic.CA1721.severity = error

lib/Common/CommonProps.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
</None>
1212
</ItemGroup>
1313
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
14-
<Exec Command="dotnet format $(ProjectPath)" />
14+
<Exec Command="dotnet format $(ProjectPath) --exclude-diagnostics CA1865" />
1515
</Target>
1616
</Project>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using Newtonsoft.Json;
1+
using System.Text.Json.Serialization;
22

33
namespace PuppeteerSharp.DevicesFetcher
44
{
55
public class Device
66
{
7-
[JsonProperty("userAgent")]
7+
[JsonPropertyName("userAgent")]
88
public string UserAgent { get; set; }
9-
[JsonProperty("name")]
9+
[JsonPropertyName("name")]
1010
public string Name { get; set; }
11-
[JsonProperty("viewport")]
11+
[JsonPropertyName("viewport")]
1212
public ViewPort Viewport { get; set; }
1313
}
1414
}

lib/PuppeteerSharp.DevicesFetcher/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
using System.Linq;
55
using System.Net.Http;
66
using System.Text;
7+
using System.Text.Json;
78
using System.Threading.Tasks;
8-
using Newtonsoft.Json;
9+
910
namespace PuppeteerSharp.DevicesFetcher
1011
{
1112
static class Program
@@ -34,7 +35,7 @@ static async Task Main(string[] args)
3435
Device[] devices;
3536
try
3637
{
37-
devices = JsonConvert.DeserializeObject<Device[]>(text);
38+
devices = JsonSerializer.Deserialize<Device[]>(text);
3839
}
3940
catch (Exception ex)
4041
{

lib/PuppeteerSharp.DevicesFetcher/PuppeteerSharp.DevicesFetcher.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
<TargetFramework>net8.0</TargetFramework>
66
<LangVersion>12</LangVersion>
77
</PropertyGroup>
8-
<ItemGroup>
9-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
10-
</ItemGroup>
11-
128
<ItemGroup>
139
<ProjectReference Include="..\PuppeteerSharp\PuppeteerSharp.csproj" />
1410
</ItemGroup>

0 commit comments

Comments
 (0)