Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
/MvcPublic archive

Commit68974b2

Browse files
committed
Add tests covering more JSON serialization cases
-#4844
1 parentb5a6303 commit68974b2

File tree

1 file changed

+191
-3
lines changed

1 file changed

+191
-3
lines changed

‎test/Microsoft.AspNetCore.Mvc.Formatters.Json.Test/JsonOutputFormatterTests.cs‎

Lines changed: 191 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
usingSystem;
55
usingSystem.Buffers;
6+
usingSystem.Collections.Generic;
67
usingSystem.IO;
78
usingSystem.Linq;
89
usingSystem.Text;
@@ -51,7 +52,7 @@ public void Constructor_UsesSerializerSettings()
5152
publicasyncTaskChangesTo_SerializerSettings_AffectSerialization()
5253
{
5354
// Arrange
54-
varperson=newUser(){Name="John",Age=35};
55+
varperson=newUser(){FullName="John",age=35};
5556
varoutputFormatterContext=GetOutputFormatterContext(person,typeof(User));
5657

5758
varsettings=newJsonSerializerSettings
@@ -79,7 +80,7 @@ public async Task ChangesTo_SerializerSettings_AffectSerialization()
7980
publicasyncTaskChangesTo_SerializerSettings_AfterSerialization_DoNotAffectSerialization()
8081
{
8182
// Arrange
82-
varperson=newUser(){Name="John",Age=35};
83+
varperson=newUser(){FullName="John",age=35};
8384
varexpectedOutput=JsonConvert.SerializeObject(person,newJsonSerializerSettings());
8485

8586
varjsonFormatter=newTestableJsonOutputFormatter(newJsonSerializerSettings());
@@ -107,6 +108,172 @@ public async Task ChangesTo_SerializerSettings_AfterSerialization_DoNotAffectSer
107108
Assert.Equal(expectedOutput,content);
108109
}
109110

111+
publicstaticTheoryData<NamingStrategy,string>NamingStrategy_AffectsSerializationData
112+
{
113+
get
114+
{
115+
returnnewTheoryData<NamingStrategy,string>
116+
{
117+
{newCamelCaseNamingStrategy(),"{\"fullName\":\"John\",\"age\":35}"},
118+
{newDefaultNamingStrategy(),"{\"FullName\":\"John\",\"age\":35}"},
119+
{newSnakeCaseNamingStrategy(),"{\"full_name\":\"John\",\"age\":35}"},
120+
};
121+
}
122+
}
123+
124+
[Theory]
125+
[MemberData(nameof(NamingStrategy_AffectsSerializationData))]
126+
publicasyncTaskNamingStrategy_AffectsSerialization(NamingStrategystrategy,stringexpected)
127+
{
128+
// Arrange
129+
varuser=newUser{FullName="John",age=35};
130+
varcontext=GetOutputFormatterContext(user,typeof(User));
131+
132+
varsettings=newJsonSerializerSettings
133+
{
134+
ContractResolver=newDefaultContractResolver
135+
{
136+
NamingStrategy=strategy,
137+
},
138+
};
139+
varformatter=newTestableJsonOutputFormatter(settings);
140+
141+
// Act
142+
awaitformatter.WriteResponseBodyAsync(context,Encoding.UTF8);
143+
144+
// Assert
145+
varbody=context.HttpContext.Response.Body;
146+
147+
Assert.NotNull(body);
148+
body.Position=0;
149+
150+
varcontent=newStreamReader(body,Encoding.UTF8).ReadToEnd();
151+
Assert.Equal(expected,content);
152+
}
153+
154+
publicstaticTheoryData<NamingStrategy>NamingStrategy_DoesNotAffectSerializationData
155+
{
156+
get
157+
{
158+
returnnewTheoryData<NamingStrategy>
159+
{
160+
{newCamelCaseNamingStrategy()},
161+
{newDefaultNamingStrategy()},
162+
{newSnakeCaseNamingStrategy()},
163+
};
164+
}
165+
}
166+
167+
[Theory]
168+
[MemberData(nameof(NamingStrategy_DoesNotAffectSerializationData))]
169+
publicasyncTaskNamingStrategy_DoesNotAffectDictionarySerialization(NamingStrategystrategy)
170+
{
171+
// Arrange
172+
vardictionary=newDictionary<string,int>(StringComparer.Ordinal)
173+
{
174+
{"id",12},
175+
{"Id",12},
176+
{"fullName",12},
177+
{"full-name",12},
178+
{"FullName",12},
179+
{"full_Name",12},
180+
};
181+
varexpected="{\"id\":12,\"Id\":12,\"fullName\":12,\"full-name\":12,\"FullName\":12,\"full_Name\":12}";
182+
varcontext=GetOutputFormatterContext(dictionary,typeof(Dictionary<string,int>));
183+
184+
varsettings=newJsonSerializerSettings
185+
{
186+
ContractResolver=newDefaultContractResolver
187+
{
188+
NamingStrategy=strategy,
189+
},
190+
};
191+
varformatter=newTestableJsonOutputFormatter(settings);
192+
193+
// Act
194+
awaitformatter.WriteResponseBodyAsync(context,Encoding.UTF8);
195+
196+
// Assert
197+
varbody=context.HttpContext.Response.Body;
198+
199+
Assert.NotNull(body);
200+
body.Position=0;
201+
202+
varcontent=newStreamReader(body,Encoding.UTF8).ReadToEnd();
203+
Assert.Equal(expected,content);
204+
}
205+
206+
[Theory]
207+
[MemberData(nameof(NamingStrategy_DoesNotAffectSerializationData))]
208+
publicasyncTaskNamingStrategy_DoesNotAffectSerialization_WithJsonProperty(NamingStrategystrategy)
209+
{
210+
// Arrange
211+
varuser=newUserWithJsonProperty
212+
{
213+
Name="Joe",
214+
AnotherName="Joe",
215+
ThirdName="Joe",
216+
};
217+
varexpected="{\"ThisIsTheFullName\":\"Joe\",\"another_name\":\"Joe\",\"ThisIsTheThirdName\":\"Joe\"}";
218+
varcontext=GetOutputFormatterContext(user,typeof(UserWithJsonProperty));
219+
220+
varsettings=newJsonSerializerSettings
221+
{
222+
ContractResolver=newDefaultContractResolver
223+
{
224+
NamingStrategy=strategy,
225+
},
226+
};
227+
varformatter=newTestableJsonOutputFormatter(settings);
228+
229+
// Act
230+
awaitformatter.WriteResponseBodyAsync(context,Encoding.UTF8);
231+
232+
// Assert
233+
varbody=context.HttpContext.Response.Body;
234+
235+
Assert.NotNull(body);
236+
body.Position=0;
237+
238+
varcontent=newStreamReader(body,Encoding.UTF8).ReadToEnd();
239+
Assert.Equal(expected,content);
240+
}
241+
242+
[Theory]
243+
[MemberData(nameof(NamingStrategy_DoesNotAffectSerializationData))]
244+
publicasyncTaskNamingStrategy_DoesNotAffectSerialization_WithJsonObject(NamingStrategystrategy)
245+
{
246+
// Arrange
247+
varuser=newUserWithJsonObject
248+
{
249+
age=35,
250+
FullName="John",
251+
};
252+
varexpected="{\"age\":35,\"full_name\":\"John\"}";
253+
varcontext=GetOutputFormatterContext(user,typeof(UserWithJsonProperty));
254+
255+
varsettings=newJsonSerializerSettings
256+
{
257+
ContractResolver=newDefaultContractResolver
258+
{
259+
NamingStrategy=strategy,
260+
},
261+
};
262+
varformatter=newTestableJsonOutputFormatter(settings);
263+
264+
// Act
265+
awaitformatter.WriteResponseBodyAsync(context,Encoding.UTF8);
266+
267+
// Assert
268+
varbody=context.HttpContext.Response.Body;
269+
270+
Assert.NotNull(body);
271+
body.Position=0;
272+
273+
varcontent=newStreamReader(body,Encoding.UTF8).ReadToEnd();
274+
Assert.Equal(expected,content);
275+
}
276+
110277
[Fact]
111278
publicasyncTaskWriteToStreamAsync_RoundTripsJToken()
112279
{
@@ -269,9 +436,30 @@ public TestableJsonOutputFormatter(JsonSerializerSettings serializerSettings)
269436

270437
privatesealedclassUser
271438
{
439+
publicstringFullName{get;set;}
440+
441+
publicintage{get;set;}
442+
}
443+
444+
privateclassUserWithJsonProperty
445+
{
446+
[JsonProperty("ThisIsTheFullName")]
272447
publicstringName{get;set;}
273448

274-
publicintAge{get;set;}
449+
[JsonProperty(NamingStrategyType=typeof(SnakeCaseNamingStrategy))]
450+
publicstringAnotherName{get;set;}
451+
452+
// NamingStrategyType should be ignored with an explicit name.
453+
[JsonProperty("ThisIsTheThirdName",NamingStrategyType=typeof(SnakeCaseNamingStrategy))]
454+
publicstringThirdName{get;set;}
455+
}
456+
457+
[JsonObject(NamingStrategyType=typeof(SnakeCaseNamingStrategy))]
458+
privateclassUserWithJsonObject
459+
{
460+
publicintage{get;set;}
461+
462+
publicstringFullName{get;set;}
275463
}
276464
}
277465
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp