33using Newtonsoft . Json ;
44using Newtonsoft . Json . Linq ;
55using System ;
6- using System . Collections . Generic ;
76using System . Linq ;
87
98namespace 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 ) ;
0 commit comments