mirror of https://github.com/Squidex/squidex.git
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.
83 lines
2.7 KiB
83 lines
2.7 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
namespace Squidex.Infrastructure.Json.Newtonsoft
|
|
{
|
|
public sealed class ConverterContractResolver : CamelCasePropertyNamesContractResolver
|
|
{
|
|
private readonly JsonConverter[] converters;
|
|
private readonly object lockObject = new object();
|
|
private Dictionary<Type, JsonConverter> converterCache = new Dictionary<Type, JsonConverter>();
|
|
|
|
public ConverterContractResolver(params JsonConverter[] converters)
|
|
{
|
|
this.converters = converters;
|
|
|
|
foreach (var converter in converters)
|
|
{
|
|
if (converter is ISupportedTypes supportedTypes)
|
|
{
|
|
foreach (var type in supportedTypes.SupportedTypes)
|
|
{
|
|
converterCache[type] = converter;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
|
|
{
|
|
var contract = base.CreateDictionaryContract(objectType);
|
|
|
|
contract.DictionaryKeyResolver = propertyName => propertyName;
|
|
|
|
return contract;
|
|
}
|
|
|
|
protected override JsonConverter ResolveContractConverter(Type objectType)
|
|
{
|
|
var result = base.ResolveContractConverter(objectType);
|
|
|
|
if (result != null)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
var cache = converterCache;
|
|
|
|
if (cache == null || !cache.TryGetValue(objectType, out result))
|
|
{
|
|
foreach (var converter in converters)
|
|
{
|
|
if (converter.CanConvert(objectType))
|
|
{
|
|
result = converter;
|
|
}
|
|
}
|
|
|
|
lock (lockObject)
|
|
{
|
|
cache = converterCache;
|
|
|
|
var updatedCache = (cache != null)
|
|
? new Dictionary<Type, JsonConverter>(cache)
|
|
: new Dictionary<Type, JsonConverter>();
|
|
updatedCache[objectType] = result;
|
|
|
|
converterCache = updatedCache;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
|