From 69341563b8b70b6f5dbf0afaf2c8a81d96ef661f Mon Sep 17 00:00:00 2001 From: hangy Date: Sat, 23 Nov 2024 23:43:52 +0100 Subject: [PATCH] perf: Refactor LocalizationProvider to use class-level JsonConverter and JsonSerializer --- .../LocalizationProvider.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/common/src/DbLocalizationProvider/LocalizationProvider.cs b/common/src/DbLocalizationProvider/LocalizationProvider.cs index 1a26c3af..ecfcb57a 100644 --- a/common/src/DbLocalizationProvider/LocalizationProvider.cs +++ b/common/src/DbLocalizationProvider/LocalizationProvider.cs @@ -28,6 +28,8 @@ public class LocalizationProvider : ILocalizationProvider private readonly ResourceKeyBuilder _keyBuilder; internal readonly IQueryExecutor _queryExecutor; private readonly ScanState _scanState; + private readonly JsonConverter _converter; + private readonly JsonSerializer _serializer; /// /// Creates new localization provider with all the required settings and services injected. @@ -52,6 +54,12 @@ public LocalizationProvider( _fallbackCollection = context.Value._fallbackCollection; _queryExecutor = queryExecutor; _scanState = scanState; + + _converter = new JsonConverter(_queryExecutor, _scanState); + _serializer = new JsonSerializer + { + ContractResolver = new StaticPropertyContractResolver() + }; } /// @@ -220,25 +228,20 @@ public T Translate() /// Translated class public T Translate(CultureInfo language) { - var converter = new JsonConverter(_queryExecutor, _scanState); var className = typeof(T).FullName; - var json = converter.GetJson(className, language.Name, _fallbackCollection); + var json = _converter.GetJson(className, language.Name, _fallbackCollection); // get the actual class Json representation (we need to select token through FQN of the class) // to supported nested classes - we need to fix a bit resource key name - var jsonToken = json.SelectToken(className.Replace("+", ".")); + var jsonToken = json.SelectToken(className.Replace('+', '.')); if (jsonToken == null) { return (T)Activator.CreateInstance(typeof(T), new object[] { }); } - return JsonConvert.DeserializeObject(jsonToken.ToString(), - new JsonSerializerSettings - { - ContractResolver = new StaticPropertyContractResolver() - }); + return jsonToken.ToObject(_serializer); } ///