mirror of https://github.com/abpframework/abp.git
csharpabpc-sharpframeworkblazoraspnet-coredotnet-coreaspnetcorearchitecturesaasdomain-driven-designangularmulti-tenancy
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.7 KiB
49 lines
1.7 KiB
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Text.Json;
|
|
using Microsoft.Extensions.Options;
|
|
using Volo.Abp.DependencyInjection;
|
|
|
|
namespace Volo.Abp.Json.SystemTextJson;
|
|
|
|
public class AbpSystemTextJsonSerializer : IJsonSerializer, ITransientDependency
|
|
{
|
|
protected AbpSystemTextJsonSerializerOptions Options { get; }
|
|
|
|
public AbpSystemTextJsonSerializer(IOptions<AbpSystemTextJsonSerializerOptions> options)
|
|
{
|
|
Options = options.Value;
|
|
}
|
|
|
|
public string Serialize(object obj, bool camelCase = true, bool indented = false)
|
|
{
|
|
return JsonSerializer.Serialize(obj, CreateJsonSerializerOptions(camelCase, indented));
|
|
}
|
|
|
|
public T Deserialize<T>(string jsonString, bool camelCase = true)
|
|
{
|
|
return JsonSerializer.Deserialize<T>(jsonString, CreateJsonSerializerOptions(camelCase))!;
|
|
}
|
|
|
|
public object Deserialize(Type type, string jsonString, bool camelCase = true)
|
|
{
|
|
return JsonSerializer.Deserialize(jsonString, type, CreateJsonSerializerOptions(camelCase))!;
|
|
}
|
|
|
|
private static readonly ConcurrentDictionary<object, JsonSerializerOptions> JsonSerializerOptionsCache =
|
|
new ConcurrentDictionary<object, JsonSerializerOptions>();
|
|
|
|
protected virtual JsonSerializerOptions CreateJsonSerializerOptions(bool camelCase = true, bool indented = false)
|
|
{
|
|
return JsonSerializerOptionsCache.GetOrAdd(new
|
|
{
|
|
camelCase,
|
|
indented,
|
|
Options.JsonSerializerOptions
|
|
}, _ => new JsonSerializerOptions(Options.JsonSerializerOptions)
|
|
{
|
|
PropertyNamingPolicy = camelCase ? JsonNamingPolicy.CamelCase : null,
|
|
WriteIndented = indented
|
|
});
|
|
}
|
|
}
|
|
|