Skip to content

Commit

Permalink
Resolved #4905: Allow to add converters to the standard JSON serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed Jul 28, 2020
1 parent 226d584 commit 51bebf4
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ namespace System.Collections.Generic
/// </summary>
public static class AbpListExtensions
{
public static void InsertRange<T>(this IList<T> source, int index, IEnumerable<T> items)
{
foreach (var item in items)
{
source.Insert(index++, item);
}
}

public static int FindIndex<T>(this IList<T> source, Predicate<T> selector)
{
for (var i = 0; i < source.Count; ++i)
Expand Down Expand Up @@ -182,7 +190,7 @@ public static T GetOrAdd<T>([NotNull] this IList<T> source, Func<T, bool> select
/// <param name="getDependencies">Function to resolve the dependencies</param>
/// <returns>
/// Returns a new list ordered by dependencies.
/// If A depends on B, then B will come before than A in the resulting list.
/// If A depends on B, then B will come before than A in the resulting list.
/// </returns>
public static List<T> SortByDependencies<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> getDependencies)
{
Expand All @@ -202,14 +210,15 @@ public static List<T> SortByDependencies<T>(this IEnumerable<T> source, Func<T,
}

/// <summary>
///
///
/// </summary>
/// <typeparam name="T">The type of the members of values.</typeparam>
/// <param name="item">Item to resolve</param>
/// <param name="getDependencies">Function to resolve the dependencies</param>
/// <param name="sorted">List with the sortet items</param>
/// <param name="visited">Dictionary with the visited items</param>
private static void SortByDependenciesVisit<T>(T item, Func<T, IEnumerable<T>> getDependencies, List<T> sorted, Dictionary<T, bool> visited)
private static void SortByDependenciesVisit<T>(T item, Func<T, IEnumerable<T>> getDependencies, List<T> sorted,
Dictionary<T, bool> visited)
{
bool inProcess;
var alreadyVisited = visited.TryGetValue(item, out inProcess);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using Volo.Abp.Http.ProxyScripting.Generators;
using Volo.Abp.Json;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Reflection;
using Volo.Abp.Threading;
using Volo.Abp.Tracing;

Expand Down
11 changes: 9 additions & 2 deletions framework/src/Volo.Abp.Json/Volo/Abp/Json/AbpJsonModule.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
using Volo.Abp.Modularity;
using Volo.Abp.Json.Newtonsoft;
using Volo.Abp.Modularity;
using Volo.Abp.Timing;

namespace Volo.Abp.Json
{
[DependsOn(typeof(AbpTimingModule))]
public class AbpJsonModule : AbpModule
{

public override void ConfigureServices(ServiceConfigurationContext context)
{
Configure<AbpNewtonsoftJsonSerializerOptions>(options =>
{
options.Converters.Add<AbpJsonIsoDateTimeConverter>();
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Newtonsoft.Json;
using Volo.Abp.Collections;

namespace Volo.Abp.Json.Newtonsoft
{
public class AbpNewtonsoftJsonSerializerOptions
{
public ITypeList<JsonConverter> Converters { get; }

public AbpNewtonsoftJsonSerializerOptions()
{
Converters = new TypeList<JsonConverter>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Volo.Abp.DependencyInjection;
Expand All @@ -7,14 +11,19 @@ namespace Volo.Abp.Json.Newtonsoft
{
public class NewtonsoftJsonSerializer : IJsonSerializer, ITransientDependency
{
private readonly AbpJsonIsoDateTimeConverter _dateTimeConverter;

private static readonly CamelCaseExceptDictionaryKeysResolver SharedCamelCaseExceptDictionaryKeysResolver =
new CamelCaseExceptDictionaryKeysResolver();

public NewtonsoftJsonSerializer(AbpJsonIsoDateTimeConverter dateTimeConverter)
protected List<JsonConverter> Converters { get; }

public NewtonsoftJsonSerializer(
IOptions<AbpNewtonsoftJsonSerializerOptions> options,
IServiceProvider serviceProvider)
{
_dateTimeConverter = dateTimeConverter;
Converters = options.Value
.Converters
.Select(c => (JsonConverter) serviceProvider.GetRequiredService(c))
.ToList();
}

public string Serialize(object obj, bool camelCase = true, bool indented = false)
Expand All @@ -36,8 +45,8 @@ protected virtual JsonSerializerSettings CreateSerializerSettings(bool camelCase
{
var settings = new JsonSerializerSettings();

settings.Converters.Insert(0, _dateTimeConverter);
settings.Converters.InsertRange(0, Converters);

if (camelCase)
{
settings.ContractResolver = SharedCamelCaseExceptDictionaryKeysResolver;
Expand All @@ -47,7 +56,7 @@ protected virtual JsonSerializerSettings CreateSerializerSettings(bool camelCase
{
settings.Formatting = Formatting.Indented;
}

return settings;
}

Expand All @@ -63,4 +72,4 @@ protected override JsonDictionaryContract CreateDictionaryContract(Type objectTy
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public async Task Create_Test()
(await _personRepository.FindAsync(resultDto.Id)).ShouldNotBeNull();
}


[Fact]
public async Task Update_Test()
{
Expand Down Expand Up @@ -170,4 +169,4 @@ public async Task DeletePhone_Test()
douglas.Phones.Any(p => p.Number == firstPhone.Number).ShouldBeFalse();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ namespace System.Collections.Generic
{
public class AbpListExtensions_Tests
{
[Fact]
public void InsertRange()
{
var list = Enumerable.Range(1, 3).ToList();
list.InsertRange(1, new[] {7, 8, 9});

list[0].ShouldBe(1);
list[1].ShouldBe(7);
list[2].ShouldBe(8);
list[3].ShouldBe(9);
list[4].ShouldBe(2);
list[5].ShouldBe(3);
}

[Fact]
public void InsertAfter()
{
Expand Down Expand Up @@ -191,7 +205,7 @@ public void SortByDependencies()
{
var list = RandomHelper
.GenerateRandomizedList(new char[] {'A', 'B', 'C', 'D', 'E', 'F', 'G'});

list = list.SortByDependencies(c => dependencies[c]);

foreach (var dependency in dependencies)
Expand All @@ -204,4 +218,4 @@ public void SortByDependencies()
}
}
}
}
}

0 comments on commit 51bebf4

Please sign in to comment.