Skip to content

Commit f505e72

Browse files
committed
serializer context added
1 parent efc1ef3 commit f505e72

2 files changed

Lines changed: 41 additions & 18 deletions

File tree

src/PartialResponse.AspNetCore.Mvc.Formatters.Json/Internal/JsonSerializerExtensions.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using Newtonsoft.Json;
44
using Newtonsoft.Json.Linq;
55
using System;
6-
using System.Collections.Generic;
76
using System.Linq;
87

98
namespace PartialResponse.AspNetCore.Mvc.Formatters.Json.Internal
@@ -18,13 +17,14 @@ public static void Serialize(this JsonSerializer jsonSerializer, JsonWriter json
1817
}
1918
else
2019
{
20+
var context = new SerializerContext(shouldSerialize);
2121
var token = JToken.FromObject(value, jsonSerializer);
2222

2323
var array = token as JArray;
2424

2525
if (array != null)
2626
{
27-
RemoveArrayElements(array, null, shouldSerialize, new Dictionary<string, bool>());
27+
RemoveArrayElements(array, null, context);
2828

2929
array.WriteTo(jsonWriter);
3030
}
@@ -34,7 +34,7 @@ public static void Serialize(this JsonSerializer jsonSerializer, JsonWriter json
3434

3535
if (@object != null)
3636
{
37-
RemoveObjectProperties(@object, null, shouldSerialize, new Dictionary<string, bool>());
37+
RemoveObjectProperties(@object, null, context);
3838

3939
@object.WriteTo(jsonWriter);
4040
}
@@ -46,11 +46,11 @@ public static void Serialize(this JsonSerializer jsonSerializer, JsonWriter json
4646
}
4747
}
4848

49-
private static void RemoveArrayElements(JArray array, string currentPath, Func<string, bool> shouldSerialize, Dictionary<string, bool> cache)
49+
private static void RemoveArrayElements(JArray array, string currentPath, SerializerContext context)
5050
{
5151
array.OfType<JObject>()
5252
.ToList()
53-
.ForEach(childObject => RemoveObjectProperties(childObject, currentPath, shouldSerialize, cache));
53+
.ForEach(childObject => RemoveObjectProperties(childObject, currentPath, context));
5454

5555
RemoveArrayIfEmpty(array);
5656
}
@@ -70,23 +70,14 @@ private static void RemoveArrayIfEmpty(JArray array)
7070
}
7171
}
7272

73-
private static void RemoveObjectProperties(JObject @object, string currentPath, Func<string, bool> shouldSerialize, Dictionary<string, bool> cache)
73+
private static void RemoveObjectProperties(JObject @object, string currentPath, SerializerContext context)
7474
{
7575
@object.Properties()
7676
.Where(property =>
7777
{
7878
var path = CombinePath(currentPath, property.Name);
7979

80-
if (cache.ContainsKey(path))
81-
{
82-
return cache[path];
83-
}
84-
85-
var result = !shouldSerialize(path);
86-
87-
cache.Add(path, result);
88-
89-
return result;
80+
return context.ShouldSerialize(path);
9081
})
9182
.ToList()
9283
.ForEach(property => property.Remove());
@@ -98,7 +89,7 @@ private static void RemoveObjectProperties(JObject @object, string currentPath,
9889
{
9990
var path = CombinePath(currentPath, property.Name);
10091

101-
RemoveObjectProperties((JObject)property.Value, path, shouldSerialize, cache);
92+
RemoveObjectProperties((JObject)property.Value, path, context);
10293
});
10394

10495
@object.Properties()
@@ -108,7 +99,7 @@ private static void RemoveObjectProperties(JObject @object, string currentPath,
10899
{
109100
var path = CombinePath(currentPath, property.Name);
110101

111-
RemoveArrayElements((JArray)property.Value, path, shouldSerialize, cache);
102+
RemoveArrayElements((JArray)property.Value, path, context);
112103
});
113104

114105
RemoveObjectIfEmpty(@object);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) Arjen Post. See License.txt and Notice.txt in the project root for license information.
2+
3+
using System;
4+
using System.Collections.Generic;
5+
6+
namespace PartialResponse.AspNetCore.Mvc.Formatters.Json.Internal
7+
{
8+
internal class SerializerContext
9+
{
10+
private readonly Func<string, bool> shouldSerialize;
11+
private readonly Dictionary<string, bool> cache = new Dictionary<string, bool>();
12+
13+
public SerializerContext(Func<string, bool> shouldSerialize)
14+
{
15+
this.shouldSerialize = shouldSerialize;
16+
}
17+
18+
public bool ShouldSerialize(string path)
19+
{
20+
if (this.cache.ContainsKey(path))
21+
{
22+
return this.cache[path];
23+
}
24+
25+
var result = !this.shouldSerialize(path);
26+
27+
this.cache.Add(path, result);
28+
29+
return result;
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)