@@ -15,40 +15,54 @@ public abstract partial class PropertyNameTests : SerializerTests
15
15
public PropertyNameTests ( JsonSerializerWrapper serializerWrapper ) : base ( serializerWrapper ) { }
16
16
17
17
[ Fact ]
18
- public async Task CamelCaseDeserializeNoMatch ( )
18
+ public async Task BuiltInPolicyDeserializeNoMatch ( )
19
19
{
20
- var options = new JsonSerializerOptions ( ) ;
21
- options . PropertyNamingPolicy = JsonNamingPolicy . CamelCase ;
22
-
23
- SimpleTestClass obj = await Serializer . DeserializeWrapper < SimpleTestClass > ( @"{""MyInt16"":1}" , options ) ;
24
-
25
- // This is 0 (default value) because the data does not match the property "MyInt16" that is assuming camel-casing of "myInt16".
26
- Assert . Equal ( 0 , obj . MyInt16 ) ;
20
+ // This is 0 (default value) because the data does not match the property "MyInt16" using the specified policy.
21
+ await DeserializeAndAssert ( JsonNamingPolicy . CamelCase , @"{""MyInt16"":1}" , 0 ) ;
22
+ await DeserializeAndAssert ( JsonNamingPolicy . SnakeCaseLower , @"{""MyInt16"":1}" , 0 ) ;
23
+ await DeserializeAndAssert ( JsonNamingPolicy . SnakeCaseUpper , @"{""MyInt16"":1}" , 0 ) ;
24
+ await DeserializeAndAssert ( JsonNamingPolicy . KebabCaseLower , @"{""MyInt16"":1}" , 0 ) ;
25
+ await DeserializeAndAssert ( JsonNamingPolicy . KebabCaseUpper , @"{""MyInt16"":1}" , 0 ) ;
27
26
}
28
27
29
28
[ Fact ]
30
- public async Task CamelCaseDeserializeMatch ( )
29
+ public async Task BuiltInPolicyDeserializeMatch ( )
31
30
{
32
- var options = new JsonSerializerOptions ( ) ;
33
- options . PropertyNamingPolicy = JsonNamingPolicy . CamelCase ;
31
+ // This is 1 because the data matches the property "MyInt16" using the specified policy.
32
+ await DeserializeAndAssert ( JsonNamingPolicy . CamelCase , @"{""myInt16"":1}" , 1 ) ;
33
+ await DeserializeAndAssert ( JsonNamingPolicy . SnakeCaseLower , @"{""my_int16"":1}" , 1 ) ;
34
+ await DeserializeAndAssert ( JsonNamingPolicy . SnakeCaseUpper , @"{""MY_INT16"":1}" , 1 ) ;
35
+ await DeserializeAndAssert ( JsonNamingPolicy . KebabCaseLower , @"{""my-int16"":1}" , 1 ) ;
36
+ await DeserializeAndAssert ( JsonNamingPolicy . KebabCaseUpper , @"{""MY-INT16"":1}" , 1 ) ;
37
+ }
34
38
35
- SimpleTestClass obj = await Serializer . DeserializeWrapper < SimpleTestClass > ( @"{""myInt16"":1}" , options ) ;
39
+ private async Task DeserializeAndAssert ( JsonNamingPolicy policy , string json , short expected )
40
+ {
41
+ var options = new JsonSerializerOptions { PropertyNamingPolicy = policy } ;
42
+ var obj = await Serializer . DeserializeWrapper < SimpleTestClass > ( json , options ) ;
36
43
37
- // This is 1 because the data matches the property "MyInt16" that is assuming camel-casing of "myInt16".
38
- Assert . Equal ( 1 , obj . MyInt16 ) ;
44
+ Assert . Equal ( expected , obj . MyInt16 ) ;
39
45
}
40
46
41
47
[ Fact ]
42
- public async Task CamelCaseSerialize ( )
48
+ public async Task BuiltInPolicySerialize ( )
43
49
{
44
- var options = new JsonSerializerOptions ( ) ;
45
- options . PropertyNamingPolicy = JsonNamingPolicy . CamelCase ;
50
+ await SerializeAndAssert ( JsonNamingPolicy . CamelCase , @"""myInt16"":0" , @"""myInt32"":0" ) ;
51
+ await SerializeAndAssert ( JsonNamingPolicy . SnakeCaseLower , @"""my_int16"":0" , @"""my_int32"":0" ) ;
52
+ await SerializeAndAssert ( JsonNamingPolicy . SnakeCaseUpper , @"""MY_INT16"":0" , @"""MY_INT32"":0" ) ;
53
+ await SerializeAndAssert ( JsonNamingPolicy . KebabCaseLower , @"""my-int16"":0" , @"""my-int32"":0" ) ;
54
+ await SerializeAndAssert ( JsonNamingPolicy . KebabCaseUpper , @"""MY-INT16"":0" , @"""MY-INT32"":0" ) ;
46
55
47
- SimpleTestClass obj = await Serializer . DeserializeWrapper < SimpleTestClass > ( @"{}" , options ) ;
56
+ async Task SerializeAndAssert ( JsonNamingPolicy policy , string myInt16 , string myInt32 )
57
+ {
58
+ var options = new JsonSerializerOptions { PropertyNamingPolicy = policy } ;
59
+ var obj = await Serializer . DeserializeWrapper < SimpleTestClass > ( @"{}" , options ) ;
48
60
49
- string json = await Serializer . SerializeWrapper ( obj , options ) ;
50
- Assert . Contains ( @"""myInt16"":0" , json ) ;
51
- Assert . Contains ( @"""myInt32"":0" , json ) ;
61
+ string json = await Serializer . SerializeWrapper ( obj , options ) ;
62
+
63
+ Assert . Contains ( myInt16 , json ) ;
64
+ Assert . Contains ( myInt32 , json ) ;
65
+ }
52
66
}
53
67
54
68
[ Fact ]
0 commit comments