From dae1c7dd90b2e5c0f2a5ec971c71ccc0782d566e Mon Sep 17 00:00:00 2001 From: Enkidu93 Date: Fri, 6 Oct 2023 12:27:08 -0400 Subject: [PATCH] No json.net --- src/Serval.Shared/Usings.cs | 2 ++ .../Utils/ObjectToInferredTypesConverter.cs | 17 +++++++++++++++++ .../Controllers/TranslationEnginesController.cs | 9 +++++++-- src/Serval.Translation/Usings.cs | 3 +-- 4 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 src/Serval.Shared/Utils/ObjectToInferredTypesConverter.cs diff --git a/src/Serval.Shared/Usings.cs b/src/Serval.Shared/Usings.cs index a0ea2a99..f7de1ea8 100644 --- a/src/Serval.Shared/Usings.cs +++ b/src/Serval.Shared/Usings.cs @@ -1,5 +1,7 @@ global using Grpc.Core; global using Grpc.Net.ClientFactory; +global using System.Text.Json; +global using System.Text.Json.Serialization; global using Microsoft.AspNetCore.Authorization; global using Microsoft.AspNetCore.Http; global using Microsoft.AspNetCore.Mvc; diff --git a/src/Serval.Shared/Utils/ObjectToInferredTypesConverter.cs b/src/Serval.Shared/Utils/ObjectToInferredTypesConverter.cs new file mode 100644 index 00000000..d43fda66 --- /dev/null +++ b/src/Serval.Shared/Utils/ObjectToInferredTypesConverter.cs @@ -0,0 +1,17 @@ +public 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) => + JsonSerializer.Serialize(writer, objectToWrite, objectToWrite.GetType(), options); +} diff --git a/src/Serval.Translation/Controllers/TranslationEnginesController.cs b/src/Serval.Translation/Controllers/TranslationEnginesController.cs index e4eed780..4a8a6240 100644 --- a/src/Serval.Translation/Controllers/TranslationEnginesController.cs +++ b/src/Serval.Translation/Controllers/TranslationEnginesController.cs @@ -984,7 +984,12 @@ private static Build Map(Engine engine, TranslationBuildConfigDto source) } try { - build.Options = JsonConvert.DeserializeObject>(source.Options ?? "{}"); + var jsonSerializerOptions = new JsonSerializerOptions(); + jsonSerializerOptions.Converters.Add(new ObjectToInferredTypesConverter()); + build.Options = JsonSerializer.Deserialize>( + source.Options ?? "{}", + jsonSerializerOptions + ); } catch (Exception e) { @@ -1029,7 +1034,7 @@ private TranslationBuildDto Map(Build source) Message = source.Message, State = source.State, DateFinished = source.DateFinished, - Options = JsonConvert.SerializeObject(source.Options) + Options = JsonSerializer.Serialize(source.Options) }; } diff --git a/src/Serval.Translation/Usings.cs b/src/Serval.Translation/Usings.cs index 88dfcf14..95b17ac5 100644 --- a/src/Serval.Translation/Usings.cs +++ b/src/Serval.Translation/Usings.cs @@ -1,6 +1,5 @@ global using System.Diagnostics.CodeAnalysis; global using System.Linq.Expressions; -global using System.Text.Json.Nodes; global using Asp.Versioning; global using Grpc.Core; global using Grpc.Net.ClientFactory; @@ -11,7 +10,6 @@ global using Microsoft.AspNetCore.Routing; global using Microsoft.Extensions.Configuration; global using Microsoft.Extensions.Options; -global using Newtonsoft.Json; global using NSwag.Annotations; global using Serval.Shared.Configuration; global using Serval.Shared.Contracts; @@ -25,3 +23,4 @@ global using Serval.Translation.Models; global using Serval.Translation.Services; global using SIL.DataAccess; +global using System.Text.Json;