From 55fa5fe522b7f8bdd889a58dcc141ef6dcc694cc Mon Sep 17 00:00:00 2001 From: Ciaran Liedeman Date: Sat, 17 Feb 2024 21:12:36 +0200 Subject: [PATCH 1/3] chore: Upgrade to dotnet 8 --- .github/workflows/check_updates.yml | 2 +- .github/workflows/ci.yml | 4 ++-- DprintPluginRoslyn.Tests/DprintPluginRoslyn.Tests.csproj | 2 +- DprintPluginRoslyn/DprintPluginRoslyn.csproj | 2 +- test/src/lib.rs | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/check_updates.yml b/.github/workflows/check_updates.yml index 49e1ca6..46782be 100644 --- a/.github/workflows/check_updates.yml +++ b/.github/workflows/check_updates.yml @@ -22,7 +22,7 @@ jobs: - uses: denoland/setup-deno@v1 - uses: actions/setup-dotnet@v1 with: - dotnet-version: "7.0.x" + dotnet-version: "8.0.x" - name: Run script run: | diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6169783..2be6d56 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: - uses: denoland/setup-deno@v1 - uses: actions/setup-dotnet@v1 with: - dotnet-version: "7.0.x" + dotnet-version: "8.0.x" - name: Build (Debug) run: dotnet build DprintPluginRoslyn @@ -42,7 +42,7 @@ jobs: - name: Package run: | - RELEASE_DIR=$GITHUB_WORKSPACE/DprintPluginRoslyn/bin/Release/net7.0 + RELEASE_DIR=$GITHUB_WORKSPACE/DprintPluginRoslyn/bin/Release/net8.0 # cd to each output directory so we only include its files cd $RELEASE_DIR/osx-x64 zip -r ../../../../../dprint-plugin-roslyn-x86_64-apple-darwin.zip ./* diff --git a/DprintPluginRoslyn.Tests/DprintPluginRoslyn.Tests.csproj b/DprintPluginRoslyn.Tests/DprintPluginRoslyn.Tests.csproj index d163fa0..6c580a9 100644 --- a/DprintPluginRoslyn.Tests/DprintPluginRoslyn.Tests.csproj +++ b/DprintPluginRoslyn.Tests/DprintPluginRoslyn.Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net8.0 false diff --git a/DprintPluginRoslyn/DprintPluginRoslyn.csproj b/DprintPluginRoslyn/DprintPluginRoslyn.csproj index a073d2a..1417b53 100644 --- a/DprintPluginRoslyn/DprintPluginRoslyn.csproj +++ b/DprintPluginRoslyn/DprintPluginRoslyn.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net8.0 Dprint.Plugins.Roslyn dprint-plugin-roslyn enable diff --git a/test/src/lib.rs b/test/src/lib.rs index 96b0f0c..b7e3e94 100644 --- a/test/src/lib.rs +++ b/test/src/lib.rs @@ -189,7 +189,7 @@ mod test { async fn new_communicator() -> ProcessPluginCommunicator { let exe_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("../DprintPluginRoslyn/bin/Debug/net7.0/") + .join("../DprintPluginRoslyn/bin/Debug/net8.0/") .join(if cfg!(windows) { "dprint-plugin-roslyn.exe" } else { "dprint-plugin-roslyn" }); ProcessPluginCommunicator::new(&exe_path, |err| eprintln!("{}", err)).await.unwrap() } From 58e53914d6c05151ef393bae983ee46ff665c86c Mon Sep 17 00:00:00 2001 From: Ciaran Liedeman Date: Sun, 18 Feb 2024 13:56:32 +0200 Subject: [PATCH 2/3] feat: Migrate to System.Text.Json --- DprintPluginRoslyn/DprintPluginRoslyn.csproj | 1 - .../Serialization/JsonSerializer.cs | 21 ++++++++----------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/DprintPluginRoslyn/DprintPluginRoslyn.csproj b/DprintPluginRoslyn/DprintPluginRoslyn.csproj index 1417b53..95b60da 100644 --- a/DprintPluginRoslyn/DprintPluginRoslyn.csproj +++ b/DprintPluginRoslyn/DprintPluginRoslyn.csproj @@ -18,7 +18,6 @@ - diff --git a/DprintPluginRoslyn/Serialization/JsonSerializer.cs b/DprintPluginRoslyn/Serialization/JsonSerializer.cs index d778676..b3ceb56 100644 --- a/DprintPluginRoslyn/Serialization/JsonSerializer.cs +++ b/DprintPluginRoslyn/Serialization/JsonSerializer.cs @@ -1,32 +1,29 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; -using System; -using System.Text; +using System; namespace Dprint.Plugins.Roslyn.Serialization; +using System.Text; +using System.Text.Json; + public class JsonSerializer { public T Deserialize(byte[] jsonData) { var jsonText = Encoding.UTF8.GetString(jsonData); - return JsonConvert.DeserializeObject(jsonText, GetSettings()) ?? throw new Exception("Error deserializing JSON."); + return System.Text.Json.JsonSerializer.Deserialize(jsonText, GetSettings()) ?? throw new Exception("Error deserializing JSON."); } public byte[] Serialize(T obj) { - var jsonText = JsonConvert.SerializeObject(obj, GetSettings()) ?? throw new Exception("Error serializing to JSON."); + var jsonText = System.Text.Json.JsonSerializer.Serialize(obj, GetSettings()) ?? throw new Exception("Error serializing to JSON."); return Encoding.UTF8.GetBytes(jsonText); } - private JsonSerializerSettings GetSettings() + private JsonSerializerOptions GetSettings() { - return new JsonSerializerSettings + return new JsonSerializerOptions { - ContractResolver = new DefaultContractResolver - { - NamingStrategy = new CamelCaseNamingStrategy() - } + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }; } } From b4e94caac9a9377203d47bdd764d61f94267faf4 Mon Sep 17 00:00:00 2001 From: Ciaran Liedeman Date: Sun, 18 Feb 2024 19:33:31 +0200 Subject: [PATCH 3/3] fix: Apply suggested workaround --- .../Serialization/JsonSerializer.cs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/DprintPluginRoslyn/Serialization/JsonSerializer.cs b/DprintPluginRoslyn/Serialization/JsonSerializer.cs index b3ceb56..3bceaea 100644 --- a/DprintPluginRoslyn/Serialization/JsonSerializer.cs +++ b/DprintPluginRoslyn/Serialization/JsonSerializer.cs @@ -4,6 +4,7 @@ namespace Dprint.Plugins.Roslyn.Serialization; using System.Text; using System.Text.Json; +using System.Text.Json.Serialization; public class JsonSerializer { @@ -24,6 +25,35 @@ private JsonSerializerOptions GetSettings() return new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + Converters = + { + new ObjectToInferredTypesConverter(), + } }; } + + // Match Newtonsoft Behaviour + // https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/migrate-from-newtonsoft?pivots=dotnet-8-0#deserialization-of-object-properties + private class ObjectToInferredTypesConverter : JsonConverter + { + public override object Read( + ref Utf8JsonReader reader, + Type typeToConvert, + JsonSerializerOptions options) => reader.TokenType switch + { + JsonTokenType.True => true, + JsonTokenType.False => false, + JsonTokenType.Number when reader.TryGetInt64(out long l) => l, + JsonTokenType.Number => reader.GetDouble(), + JsonTokenType.String when reader.TryGetDateTime(out DateTime datetime) => datetime, + JsonTokenType.String => reader.GetString()!, + _ => JsonDocument.ParseValue(ref reader).RootElement.Clone() + }; + + public override void Write( + Utf8JsonWriter writer, + object objectToWrite, + JsonSerializerOptions options) => + System.Text.Json.JsonSerializer.Serialize(writer, objectToWrite, objectToWrite.GetType(), options); + } }