Browse Source

Fix serialization.

pull/380/head
Sebastian Stehle 7 years ago
parent
commit
67376121d4
  1. 12
      src/Squidex.Infrastructure/Json/Newtonsoft/ConverterContractResolver.cs
  2. 2
      src/Squidex/app/features/settings/pages/workflows/workflow-step.component.html
  3. 2
      tests/Squidex.Domain.Apps.Core.Tests/TestUtils.cs
  4. 46
      tests/Squidex.Infrastructure.Tests/Json/Newtonsoft/ReadOnlyDictionaryTests.cs

12
src/Squidex.Infrastructure/Json/Newtonsoft/ConverterContractResolver.cs

@ -36,6 +36,18 @@ namespace Squidex.Infrastructure.Json.Newtonsoft
}
}
protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
{
if (objectType.IsGenericType && objectType.GetGenericTypeDefinition() == typeof(IReadOnlyDictionary<,>))
{
var implementationType = typeof(Dictionary<,>).MakeGenericType(objectType.GetGenericArguments());
return base.CreateDictionaryContract(implementationType);
}
return base.CreateDictionaryContract(objectType);
}
protected override JsonConverter ResolveContractConverter(Type objectType)
{
var result = base.ResolveContractConverter(objectType);

2
src/Squidex/app/features/settings/pages/workflows/workflow-step.component.html

@ -20,7 +20,7 @@
<sqx-editable-title
[name]="step.name"
(nameChanged)="changeName($event)"
[disabled]="step.isLocked || transitionAdd">
[disabled]="step.isLocked || disabled">
</sqx-editable-title>
</div>
<div class="col">

2
tests/Squidex.Domain.Apps.Core.Tests/TestUtils.cs

@ -38,7 +38,7 @@ namespace Squidex.Domain.Apps.Core
var serializerSettings = new JsonSerializerSettings
{
SerializationBinder = new TypeNameSerializationBinder(typeNameRegistry),
SerializationBinder = new ReadOnlyDictionaryBinder(new TypeNameSerializationBinder(typeNameRegistry)),
ContractResolver = new ConverterContractResolver(
new AppClientsConverter(),

46
tests/Squidex.Infrastructure.Tests/Json/Newtonsoft/ReadOnlyDictionaryTests.cs

@ -0,0 +1,46 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Collections.Generic;
using Newtonsoft.Json;
using Xunit;
namespace Squidex.Infrastructure.Json.Newtonsoft
{
public class ReadOnlyDictionaryTests
{
public sealed class MyClass
{
public IReadOnlyDictionary<int, int> Values { get; set; }
}
[Fact]
public void Should_serialize_and_deserialize_without_type_name()
{
var source = new MyClass
{
Values = new Dictionary<int, int>
{
[2] = 4,
[3] = 9
}
};
var serializerSettings = new JsonSerializerSettings
{
ContractResolver = new ConverterContractResolver()
};
var json = JsonConvert.SerializeObject(source, serializerSettings);
var serialized = JsonConvert.DeserializeObject<MyClass>(json);
Assert.DoesNotContain("$type", json);
Assert.Equal(2, serialized.Values.Count);
}
}
}
Loading…
Cancel
Save