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.
67 lines
2.5 KiB
67 lines
2.5 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Converters;
|
|
using Squidex.Infrastructure.Json;
|
|
using Squidex.Infrastructure.Json.Newtonsoft;
|
|
using Squidex.Infrastructure.Queries.Json;
|
|
|
|
namespace Squidex.Infrastructure.TestHelpers
|
|
{
|
|
public static class JsonHelper
|
|
{
|
|
public static readonly IJsonSerializer DefaultSerializer = CreateSerializer();
|
|
|
|
public static IJsonSerializer CreateSerializer(TypeNameRegistry typeNameRegistry = null)
|
|
{
|
|
var serializerSettings = DefaultSettings(typeNameRegistry);
|
|
|
|
return new NewtonsoftJsonSerializer(serializerSettings);
|
|
}
|
|
|
|
public static JsonSerializerSettings DefaultSettings(TypeNameRegistry typeNameRegistry = null)
|
|
{
|
|
return new JsonSerializerSettings
|
|
{
|
|
SerializationBinder = new TypeNameSerializationBinder(typeNameRegistry ?? new TypeNameRegistry()),
|
|
|
|
ContractResolver = new ConverterContractResolver(
|
|
new ClaimsPrincipalConverter(),
|
|
new InstantConverter(),
|
|
new EnvelopeHeadersConverter(),
|
|
new FilterConverter(),
|
|
new JsonValueConverter(),
|
|
new LanguageConverter(),
|
|
new NamedGuidIdConverter(),
|
|
new NamedLongIdConverter(),
|
|
new NamedStringIdConverter(),
|
|
new PropertyPathConverter(),
|
|
new RefTokenConverter(),
|
|
new StringEnumConverter()),
|
|
|
|
TypeNameHandling = TypeNameHandling.Auto
|
|
};
|
|
}
|
|
|
|
public static T SerializeAndDeserialize<T>(this T value)
|
|
{
|
|
return DefaultSerializer.Deserialize<Tuple<T>>(DefaultSerializer.Serialize(Tuple.Create(value))).Item1;
|
|
}
|
|
|
|
public static T Deserialize<T>(string value)
|
|
{
|
|
return DefaultSerializer.Deserialize<Tuple<T>>($"{{ \"Item1\": \"{value}\" }}").Item1;
|
|
}
|
|
|
|
public static T Deserialize<T>(object value)
|
|
{
|
|
return DefaultSerializer.Deserialize<Tuple<T>>($"{{ \"Item1\": {value} }}").Item1;
|
|
}
|
|
}
|
|
}
|
|
|