Skip to content

Commit

Permalink
No json.net
Browse files Browse the repository at this point in the history
  • Loading branch information
Enkidu93 committed Oct 6, 2023
1 parent e09ae39 commit dae1c7d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Serval.Shared/Usings.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
17 changes: 17 additions & 0 deletions src/Serval.Shared/Utils/ObjectToInferredTypesConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class ObjectToInferredTypesConverter : JsonConverter<object>
{
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,12 @@ private static Build Map(Engine engine, TranslationBuildConfigDto source)
}
try
{
build.Options = JsonConvert.DeserializeObject<IDictionary<string, object>>(source.Options ?? "{}");
var jsonSerializerOptions = new JsonSerializerOptions();
jsonSerializerOptions.Converters.Add(new ObjectToInferredTypesConverter());
build.Options = JsonSerializer.Deserialize<IDictionary<string, object>>(
source.Options ?? "{}",
jsonSerializerOptions
);
}
catch (Exception e)
{
Expand Down Expand Up @@ -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)
};
}

Expand Down
3 changes: 1 addition & 2 deletions src/Serval.Translation/Usings.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -25,3 +23,4 @@
global using Serval.Translation.Models;
global using Serval.Translation.Services;
global using SIL.DataAccess;
global using System.Text.Json;

0 comments on commit dae1c7d

Please sign in to comment.