1+ using System . Collections . Generic ;
2+ using System . IO ;
3+ using System . Text ;
4+ using Newtonsoft . Json ;
5+ using PartialResponse . AspNetCore . Mvc . Formatters . Json . Internal ;
6+ using Xunit ;
7+
8+ namespace PartialResponse . AspNetCore . Mvc . Formatters . Json
9+ {
10+ public class JsonSerializerExtensionsTests
11+ {
12+ private readonly JsonSerializer jsonSerializer = JsonSerializer . Create ( new JsonSerializerSettings ( ) ) ;
13+ private readonly StringBuilder result = new StringBuilder ( ) ;
14+ private readonly JsonWriter jsonWriter ;
15+
16+ public JsonSerializerExtensionsTests ( )
17+ {
18+ this . jsonWriter = new JsonTextWriter ( new StringWriter ( this . result ) ) ;
19+ }
20+
21+ [ Fact ]
22+ public void TheSerializeMethodShouldFilterArrayElements ( )
23+ {
24+ // Arrange
25+ var value = new List < dynamic > { new { foo = "bar" , baz = "qux" } } ;
26+
27+ // Act
28+ jsonSerializer . Serialize ( this . jsonWriter , value , _ => _ == "foo" ) ;
29+
30+ // Assert
31+ Assert . Equal ( "[{\" foo\" :\" bar\" }]" , result . ToString ( ) ) ;
32+ }
33+
34+ [ Fact ]
35+ public void TheSerializeMethodShouldRemoveEmptyArray ( )
36+ {
37+ // Arrange
38+ var value = new { foo = new List < dynamic > { new { bar = "baz" } } , qux = "quux" } ;
39+
40+ // Act
41+ jsonSerializer . Serialize ( this . jsonWriter , value , _ => _ == "qux" ) ;
42+
43+ // Assert
44+ Assert . Equal ( "{\" qux\" :\" quux\" }" , result . ToString ( ) ) ;
45+ }
46+
47+ [ Fact ]
48+ public void TheSerializeMethodShouldNotRemoveEmptyArrayIfRoot ( )
49+ {
50+ // Arrange
51+ var value = new List < dynamic > { new { foo = "bar" } } ;
52+
53+ // Act
54+ jsonSerializer . Serialize ( this . jsonWriter , value , _ => false ) ;
55+
56+ // Assert
57+ Assert . Equal ( "[]" , result . ToString ( ) ) ;
58+ }
59+
60+ [ Fact ]
61+ public void TheSerializeMethodShouldFilterObjectPropertiesInsideArrayElement ( )
62+ {
63+ // Arrange
64+ var value = new List < dynamic > { new { foo = new { bar = "baz" , qux = "quux" } } } ;
65+
66+ // Act
67+ jsonSerializer . Serialize ( this . jsonWriter , value , _ => _ == "foo" || _ == "foo/bar" ) ;
68+
69+ // Assert
70+ Assert . Equal ( "[{\" foo\" :{\" bar\" :\" baz\" }}]" , result . ToString ( ) ) ;
71+ }
72+
73+ [ Fact ]
74+ public void TheSerializeMethodShouldFilterObjectProperties ( )
75+ {
76+ // Arrange
77+ var value = new { foo = "bar" , baz = "qux" } ;
78+
79+ // Act
80+ jsonSerializer . Serialize ( this . jsonWriter , value , _ => _ == "foo" ) ;
81+
82+ // Assert
83+ Assert . Equal ( "{\" foo\" :\" bar\" }" , result . ToString ( ) ) ;
84+ }
85+
86+ [ Fact ]
87+ public void TheSerializeMethodShouldRemoveEmptyObject ( )
88+ {
89+ // Arrange
90+ var value = new { foo = new { bar = "baz" } , qux = "quux" } ;
91+
92+ // Act
93+ jsonSerializer . Serialize ( this . jsonWriter , value , _ => _ == "qux" ) ;
94+
95+ // Assert
96+ Assert . Equal ( "{\" qux\" :\" quux\" }" , result . ToString ( ) ) ;
97+ }
98+
99+ [ Fact ]
100+ public void TheSerializeMethodShouldNotRemoveEmptyObjectIfRoot ( )
101+ {
102+ // Arrange
103+ var value = new { foo = "bar" } ;
104+
105+ // Act
106+ jsonSerializer . Serialize ( this . jsonWriter , value , _ => false ) ;
107+
108+ // Assert
109+ Assert . Equal ( "{}" , result . ToString ( ) ) ;
110+ }
111+
112+ [ Fact ]
113+ public void TheSerializeMethodShouldFilterObjectPropertiesInsideObjectProperties ( )
114+ {
115+ // Arrange
116+ var value = new { foo = new { bar = "baz" , qux = "quux" } } ;
117+
118+ // Act
119+ jsonSerializer . Serialize ( this . jsonWriter , value , _ => _ == "foo" || _ == "foo/bar" ) ;
120+
121+ // Assert
122+ Assert . Equal ( "{\" foo\" :{\" bar\" :\" baz\" }}" , result . ToString ( ) ) ;
123+ }
124+
125+ [ Fact ]
126+ public void TheSerializeMethodShouldFilterArrayElementsInsideObjectProperties ( )
127+ {
128+ // Arrange
129+ var value = new { foo = new List < dynamic > { new { bar = "baz" , qux = "quux" } } } ;
130+
131+ // Act
132+ jsonSerializer . Serialize ( this . jsonWriter , value , _ => _ == "foo" || _ == "foo/bar" ) ;
133+
134+ // Assert
135+ Assert . Equal ( "{\" foo\" :[{\" bar\" :\" baz\" }]}" , result . ToString ( ) ) ;
136+ }
137+
138+ [ Fact ]
139+ public void TheSerializeMethodShouldFilterArrayElementInsideArrayElement ( )
140+ {
141+ // Arrange
142+ var value = new List < dynamic > { new { foo = new List < dynamic > { new { bar = "baz" , qux = "quux" } } } } ;
143+
144+ // Act
145+ jsonSerializer . Serialize ( this . jsonWriter , value , _ => _ == "foo" || _ == "foo/bar" ) ;
146+
147+ // Assert
148+ Assert . Equal ( "[{\" foo\" :[{\" bar\" :\" baz\" }]}]" , result . ToString ( ) ) ;
149+ }
150+
151+ [ Fact ]
152+ public void TheSerializeMethodShouldApplyCaching ( )
153+ {
154+ // Arrange
155+ var value = new List < dynamic > { new { foo = "bar" } , new { foo = "bar" } } ;
156+ var count = 0 ;
157+
158+ // Act
159+ jsonSerializer . Serialize ( this . jsonWriter , value , _ => { count ++ ; return _ == "foo" ; } ) ;
160+
161+ // Assert
162+ Assert . Equal ( 1 , count ) ;
163+ }
164+
165+ [ Fact ]
166+ public void TheSerializeMethodFilterCorrectlyUsingCache ( )
167+ {
168+ // Arrange
169+ var value = new List < dynamic > { new { foo = "bar" , baz = "qux" } , new { foo = "bar" , baz = "qux" } } ;
170+
171+ // Act
172+ jsonSerializer . Serialize ( this . jsonWriter , value , _ => _ == "foo" ) ;
173+
174+ // Assert
175+ Assert . Equal ( "[{\" foo\" :\" bar\" },{\" foo\" :\" bar\" }]" , result . ToString ( ) ) ;
176+ }
177+ }
178+ }
0 commit comments