Skip to content

Commit

Permalink
perf: Refactor LocalizationProvider to use class-level JsonConverter …
Browse files Browse the repository at this point in the history
…and JsonSerializer
  • Loading branch information
hangy committed Nov 23, 2024
1 parent 4c0d66e commit 6934156
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions common/src/DbLocalizationProvider/LocalizationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Creates new localization provider with all the required settings and services injected.
Expand All @@ -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()
};
}

/// <summary>
Expand Down Expand Up @@ -220,25 +228,20 @@ public T Translate<T>()
/// <returns>Translated class</returns>
public T Translate<T>(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<T>(jsonToken.ToString(),
new JsonSerializerSettings
{
ContractResolver = new StaticPropertyContractResolver()
});
return jsonToken.ToObject<T>(_serializer);
}

/// <summary>
Expand Down

0 comments on commit 6934156

Please sign in to comment.