@@ -25,58 +25,58 @@ public class ReadJson : JsonTestsBase
25
25
[ Fact ]
26
26
public void Should_deserialize_enum_when_null_and_default_unless_enum_and_underlying_are_both_null ( )
27
27
{
28
- Deserialize < TestSmartEnum_Class_IntBased , ValueObjectJsonConverterFactory < TestSmartEnum_Class_IntBased , int > > ( "null" ) . Should ( ) . Be ( null ) ;
29
- Deserialize < TestSmartEnum_Class_StringBased , ValueObjectJsonConverterFactory < TestSmartEnum_Class_StringBased , string > > ( "null" ) . Should ( ) . Be ( null ) ;
30
- Deserialize < TestSmartEnum_Struct_IntBased ? , ValueObjectJsonConverterFactory < TestSmartEnum_Struct_IntBased , int > > ( "null" ) . Should ( ) . Be ( null ) ;
31
- Deserialize < TestSmartEnum_Struct_StringBased ? , ValueObjectJsonConverterFactory < TestSmartEnum_Struct_StringBased , string > > ( "null" ) . Should ( ) . Be ( null ) ;
32
- Deserialize < TestSmartEnum_Struct_IntBased , ValueObjectJsonConverterFactory < TestSmartEnum_Struct_IntBased , int > > ( "0" ) . Should ( ) . Be ( default ( TestSmartEnum_Struct_IntBased ) ) ; // default(int) is 0
33
- Deserialize < TestSmartEnum_Struct_StringBased , ValueObjectJsonConverterFactory < TestSmartEnum_Struct_StringBased , string > > ( "null" ) . Should ( ) . Be ( default ( TestSmartEnum_Struct_StringBased ) ) ;
34
-
35
- FluentActions . Invoking ( ( ) => Deserialize < TestSmartEnum_Struct_IntBased , ValueObjectJsonConverterFactory < TestSmartEnum_Struct_IntBased , int > > ( "null" ) ) . Should ( )
28
+ DeserializeWithConverter < TestSmartEnum_Class_IntBased , ValueObjectJsonConverterFactory < TestSmartEnum_Class_IntBased , int > > ( "null" ) . Should ( ) . Be ( null ) ;
29
+ DeserializeWithConverter < TestSmartEnum_Class_StringBased , ValueObjectJsonConverterFactory < TestSmartEnum_Class_StringBased , string > > ( "null" ) . Should ( ) . Be ( null ) ;
30
+ DeserializeWithConverter < TestSmartEnum_Struct_IntBased ? , ValueObjectJsonConverterFactory < TestSmartEnum_Struct_IntBased , int > > ( "null" ) . Should ( ) . Be ( null ) ;
31
+ DeserializeWithConverter < TestSmartEnum_Struct_StringBased ? , ValueObjectJsonConverterFactory < TestSmartEnum_Struct_StringBased , string > > ( "null" ) . Should ( ) . Be ( null ) ;
32
+ DeserializeWithConverter < TestSmartEnum_Struct_IntBased , ValueObjectJsonConverterFactory < TestSmartEnum_Struct_IntBased , int > > ( "0" ) . Should ( ) . Be ( default ( TestSmartEnum_Struct_IntBased ) ) ; // default(int) is 0
33
+ DeserializeWithConverter < TestSmartEnum_Struct_StringBased , ValueObjectJsonConverterFactory < TestSmartEnum_Struct_StringBased , string > > ( "null" ) . Should ( ) . Be ( default ( TestSmartEnum_Struct_StringBased ) ) ;
34
+
35
+ FluentActions . Invoking ( ( ) => DeserializeWithConverter < TestSmartEnum_Struct_IntBased , ValueObjectJsonConverterFactory < TestSmartEnum_Struct_IntBased , int > > ( "null" ) ) . Should ( )
36
36
. Throw < JsonException > ( )
37
37
. WithInnerException < InvalidOperationException > ( ) . WithMessage ( "Cannot get the value of a token type 'Null' as a number." ) ;
38
38
}
39
39
40
40
[ Fact ]
41
41
public void Should_deserialize_keyed_value_object_when_null_and_default_unless_enum_and_underlying_are_both_null ( )
42
42
{
43
- Deserialize < IntBasedReferenceValueObject , ValueObjectJsonConverterFactory < IntBasedReferenceValueObject , int > > ( "null" ) . Should ( ) . Be ( null ) ;
44
- Deserialize < StringBasedReferenceValueObject , ValueObjectJsonConverterFactory < StringBasedReferenceValueObject , string > > ( "null" ) . Should ( ) . Be ( null ) ;
45
- Deserialize < IntBasedStructValueObject ? , ValueObjectJsonConverterFactory < IntBasedStructValueObject , int > > ( "null" ) . Should ( ) . Be ( null ) ;
46
- Deserialize < StringBasedStructValueObject ? , ValueObjectJsonConverterFactory < StringBasedStructValueObject , string > > ( "null" ) . Should ( ) . Be ( null ) ;
47
- Deserialize < IntBasedStructValueObject , ValueObjectJsonConverterFactory < IntBasedStructValueObject , int > > ( "0" ) . Should ( ) . Be ( default ) ; // default(int) is 0
48
- Deserialize < StringBasedStructValueObject , ValueObjectJsonConverterFactory < StringBasedStructValueObject , string > > ( "null" ) . Should ( ) . Be ( default ) ;
43
+ DeserializeWithConverter < IntBasedReferenceValueObject , ValueObjectJsonConverterFactory < IntBasedReferenceValueObject , int > > ( "null" ) . Should ( ) . Be ( null ) ;
44
+ DeserializeWithConverter < StringBasedReferenceValueObject , ValueObjectJsonConverterFactory < StringBasedReferenceValueObject , string > > ( "null" ) . Should ( ) . Be ( null ) ;
45
+ DeserializeWithConverter < IntBasedStructValueObject ? , ValueObjectJsonConverterFactory < IntBasedStructValueObject , int > > ( "null" ) . Should ( ) . Be ( null ) ;
46
+ DeserializeWithConverter < StringBasedStructValueObject ? , ValueObjectJsonConverterFactory < StringBasedStructValueObject , string > > ( "null" ) . Should ( ) . Be ( null ) ;
47
+ DeserializeWithConverter < IntBasedStructValueObject , ValueObjectJsonConverterFactory < IntBasedStructValueObject , int > > ( "0" ) . Should ( ) . Be ( default ) ; // default(int) is 0
48
+ DeserializeWithConverter < StringBasedStructValueObject , ValueObjectJsonConverterFactory < StringBasedStructValueObject , string > > ( "null" ) . Should ( ) . Be ( default ) ;
49
49
50
50
// NullInFactoryMethodsYieldsNull
51
- Deserialize < StringBasedReferenceValueObjectWithNullInFactoryMethodsYieldsNull , ValueObjectJsonConverterFactory < StringBasedReferenceValueObjectWithNullInFactoryMethodsYieldsNull , string > > ( "null" ) . Should ( ) . Be ( null ) ;
52
- FluentActions . Invoking ( ( ) => Deserialize < StringBasedReferenceValueObjectWithNullInFactoryMethodsYieldsNull , ValueObjectJsonConverterFactory < StringBasedReferenceValueObjectWithNullInFactoryMethodsYieldsNull , string > > ( "\" \" " ) )
51
+ DeserializeWithConverter < StringBasedReferenceValueObjectWithNullInFactoryMethodsYieldsNull , ValueObjectJsonConverterFactory < StringBasedReferenceValueObjectWithNullInFactoryMethodsYieldsNull , string > > ( "null" ) . Should ( ) . Be ( null ) ;
52
+ FluentActions . Invoking ( ( ) => DeserializeWithConverter < StringBasedReferenceValueObjectWithNullInFactoryMethodsYieldsNull , ValueObjectJsonConverterFactory < StringBasedReferenceValueObjectWithNullInFactoryMethodsYieldsNull , string > > ( "\" \" " ) )
53
53
. Should ( ) . Throw < JsonException > ( ) . WithMessage ( "Property cannot be empty." ) ;
54
- FluentActions . Invoking ( ( ) => Deserialize < StringBasedReferenceValueObjectWithNullInFactoryMethodsYieldsNull , ValueObjectJsonConverterFactory < StringBasedReferenceValueObjectWithNullInFactoryMethodsYieldsNull , string > > ( "\" \" " ) )
54
+ FluentActions . Invoking ( ( ) => DeserializeWithConverter < StringBasedReferenceValueObjectWithNullInFactoryMethodsYieldsNull , ValueObjectJsonConverterFactory < StringBasedReferenceValueObjectWithNullInFactoryMethodsYieldsNull , string > > ( "\" \" " ) )
55
55
. Should ( ) . Throw < JsonException > ( ) . WithMessage ( "Property cannot be empty." ) ;
56
56
57
57
// EmptyStringInFactoryMethodsYieldsNull
58
- Deserialize < StringBasedReferenceValueObjectWithEmptyStringInFactoryMethodsYieldsNull , ValueObjectJsonConverterFactory < StringBasedReferenceValueObjectWithEmptyStringInFactoryMethodsYieldsNull , string > > ( "null" ) . Should ( ) . Be ( null ) ;
59
- Deserialize < StringBasedReferenceValueObjectWithEmptyStringInFactoryMethodsYieldsNull , ValueObjectJsonConverterFactory < StringBasedReferenceValueObjectWithEmptyStringInFactoryMethodsYieldsNull , string > > ( "\" \" " ) . Should ( ) . Be ( null ) ;
60
- Deserialize < StringBasedReferenceValueObjectWithEmptyStringInFactoryMethodsYieldsNull , ValueObjectJsonConverterFactory < StringBasedReferenceValueObjectWithEmptyStringInFactoryMethodsYieldsNull , string > > ( "\" \" " ) . Should ( ) . Be ( null ) ;
58
+ DeserializeWithConverter < StringBasedReferenceValueObjectWithEmptyStringInFactoryMethodsYieldsNull , ValueObjectJsonConverterFactory < StringBasedReferenceValueObjectWithEmptyStringInFactoryMethodsYieldsNull , string > > ( "null" ) . Should ( ) . Be ( null ) ;
59
+ DeserializeWithConverter < StringBasedReferenceValueObjectWithEmptyStringInFactoryMethodsYieldsNull , ValueObjectJsonConverterFactory < StringBasedReferenceValueObjectWithEmptyStringInFactoryMethodsYieldsNull , string > > ( "\" \" " ) . Should ( ) . Be ( null ) ;
60
+ DeserializeWithConverter < StringBasedReferenceValueObjectWithEmptyStringInFactoryMethodsYieldsNull , ValueObjectJsonConverterFactory < StringBasedReferenceValueObjectWithEmptyStringInFactoryMethodsYieldsNull , string > > ( "\" \" " ) . Should ( ) . Be ( null ) ;
61
61
62
- FluentActions . Invoking ( ( ) => Deserialize < IntBasedStructValueObject , ValueObjectJsonConverterFactory < IntBasedStructValueObject , int > > ( "null" ) ) . Should ( )
62
+ FluentActions . Invoking ( ( ) => DeserializeWithConverter < IntBasedStructValueObject , ValueObjectJsonConverterFactory < IntBasedStructValueObject , int > > ( "null" ) ) . Should ( )
63
63
. Throw < JsonException > ( )
64
64
. WithInnerException < InvalidOperationException > ( ) . WithMessage ( "Cannot get the value of a token type 'Null' as a number." ) ;
65
65
}
66
66
67
67
[ Fact ]
68
68
public void Should_deserialize_value_object_if_null_and_default ( )
69
69
{
70
- Deserialize < TestValueObject_Complex_Class , TestValueObject_Complex_Class . ValueObjectJsonConverterFactory > ( "null" ) . Should ( ) . Be ( null ) ;
71
- Deserialize < TestValueObject_Complex_Struct ? , TestValueObject_Complex_Struct . ValueObjectJsonConverterFactory > ( "null" ) . Should ( ) . Be ( null ) ;
72
- Deserialize < TestValueObject_Complex_Struct , TestValueObject_Complex_Struct . ValueObjectJsonConverterFactory > ( "null" ) . Should ( ) . Be ( default ( TestValueObject_Complex_Struct ) ) ;
70
+ DeserializeWithConverter < TestValueObject_Complex_Class , TestValueObject_Complex_Class . ValueObjectJsonConverterFactory > ( "null" ) . Should ( ) . Be ( null ) ;
71
+ DeserializeWithConverter < TestValueObject_Complex_Struct ? , TestValueObject_Complex_Struct . ValueObjectJsonConverterFactory > ( "null" ) . Should ( ) . Be ( null ) ;
72
+ DeserializeWithConverter < TestValueObject_Complex_Struct , TestValueObject_Complex_Struct . ValueObjectJsonConverterFactory > ( "null" ) . Should ( ) . Be ( default ( TestValueObject_Complex_Struct ) ) ;
73
73
}
74
74
75
75
[ Theory ]
76
76
[ MemberData ( nameof ( DataForStringBasedEnumTest ) ) ]
77
77
public void Should_deserialize_string_based_enum ( TestEnum expectedValue , string json )
78
78
{
79
- var value = Deserialize < TestEnum , ValueObjectJsonConverterFactory < TestEnum , string > > ( json ) ;
79
+ var value = DeserializeWithConverter < TestEnum , ValueObjectJsonConverterFactory < TestEnum , string > > ( json ) ;
80
80
81
81
value . Should ( ) . Be ( expectedValue ) ;
82
82
}
@@ -85,7 +85,7 @@ public void Should_deserialize_string_based_enum(TestEnum expectedValue, string
85
85
[ MemberData ( nameof ( DataForIntBasedEnumTest ) ) ]
86
86
public void Should_deserialize_int_based_enum ( IntegerEnum expectedValue , string json )
87
87
{
88
- var value = Deserialize < IntegerEnum , ValueObjectJsonConverterFactory < IntegerEnum , int > > ( json ) ;
88
+ var value = DeserializeWithConverter < IntegerEnum , ValueObjectJsonConverterFactory < IntegerEnum , int > > ( json ) ;
89
89
90
90
value . Should ( ) . Be ( expectedValue ) ;
91
91
}
@@ -94,7 +94,7 @@ public void Should_deserialize_int_based_enum(IntegerEnum expectedValue, string
94
94
[ MemberData ( nameof ( DataForClassWithStringBasedEnumTest ) ) ]
95
95
public void Should_deserialize_class_containing_string_based_enum ( ClassWithStringBasedEnum expectedValue , string json , bool ignoreNullValues = false )
96
96
{
97
- var value = Deserialize < ClassWithStringBasedEnum , ValueObjectJsonConverterFactory < TestEnum , string > > ( json , ignoreNullValues : ignoreNullValues ) ;
97
+ var value = DeserializeWithConverter < ClassWithStringBasedEnum , ValueObjectJsonConverterFactory < TestEnum , string > > ( json , ignoreNullValues : ignoreNullValues ) ;
98
98
99
99
value . Should ( ) . BeEquivalentTo ( expectedValue ) ;
100
100
}
@@ -103,7 +103,7 @@ public void Should_deserialize_class_containing_string_based_enum(ClassWithStrin
103
103
[ MemberData ( nameof ( DataForClassWithIntBasedEnumTest ) ) ]
104
104
public void Should_deserialize_class_containing_int_based_enum ( ClassWithIntBasedEnum expectedValue , string json , bool ignoreNullValues = false )
105
105
{
106
- var value = Deserialize < ClassWithIntBasedEnum , ValueObjectJsonConverterFactory < IntegerEnum , int > > ( json , ignoreNullValues : ignoreNullValues ) ;
106
+ var value = DeserializeWithConverter < ClassWithIntBasedEnum , ValueObjectJsonConverterFactory < IntegerEnum , int > > ( json , ignoreNullValues : ignoreNullValues ) ;
107
107
108
108
value . Should ( ) . BeEquivalentTo ( expectedValue ) ;
109
109
}
@@ -116,30 +116,46 @@ public void Should_deserialize_value_type_with_multiple_properties(
116
116
JsonNamingPolicy namingPolicy = null ,
117
117
bool propertyNameCaseInsensitive = false )
118
118
{
119
- var value = Deserialize < ValueObjectWithMultipleProperties , ValueObjectWithMultipleProperties . ValueObjectJsonConverterFactory > ( json , namingPolicy , propertyNameCaseInsensitive ) ;
119
+ var value = DeserializeWithConverter < ValueObjectWithMultipleProperties , ValueObjectWithMultipleProperties . ValueObjectJsonConverterFactory > ( json , namingPolicy , propertyNameCaseInsensitive ) ;
120
120
121
121
value . Should ( ) . BeEquivalentTo ( expectedValueObject ) ;
122
122
}
123
123
124
124
[ Fact ]
125
125
public void Should_throw_JsonException_if_enum_parsing_throws_UnknownEnumIdentifierException ( )
126
126
{
127
- FluentActions . Invoking ( ( ) => Deserialize < ValidTestEnum , ValueObjectJsonConverterFactory < ValidTestEnum , string > > ( "\" invalid\" " ) )
127
+ FluentActions . Invoking ( ( ) => DeserializeWithConverter < ValidTestEnum , ValueObjectJsonConverterFactory < ValidTestEnum , string > > ( "\" invalid\" " ) )
128
128
. Should ( ) . Throw < JsonException > ( ) . WithMessage ( "There is no item of type 'ValidTestEnum' with the identifier 'invalid'." ) ;
129
129
}
130
130
131
131
[ Fact ]
132
132
public void Should_throw_JsonException_if_complex_value_object_is_not_a_json_object ( )
133
133
{
134
- FluentActions . Invoking ( ( ) => Deserialize < TestValueObject_Complex_Class , TestValueObject_Complex_Class . ValueObjectJsonConverterFactory > ( "\" invalid\" " ) )
134
+ FluentActions . Invoking ( ( ) => DeserializeWithConverter < TestValueObject_Complex_Class , TestValueObject_Complex_Class . ValueObjectJsonConverterFactory > ( "\" invalid\" " ) )
135
135
. Should ( ) . Throw < JsonException > ( ) . WithMessage ( "Unexpected token \" String\" when trying to deserialize \" TestValueObject_Complex_Class\" . Expected token: \" StartObject\" ." ) ;
136
- FluentActions . Invoking ( ( ) => Deserialize < TestValueObject_Complex_Class , TestValueObject_Complex_Class . ValueObjectJsonConverterFactory > ( "42" ) )
136
+ FluentActions . Invoking ( ( ) => DeserializeWithConverter < TestValueObject_Complex_Class , TestValueObject_Complex_Class . ValueObjectJsonConverterFactory > ( "42" ) )
137
137
. Should ( ) . Throw < JsonException > ( ) . WithMessage ( "Unexpected token \" Number\" when trying to deserialize \" TestValueObject_Complex_Class\" . Expected token: \" StartObject\" ." ) ;
138
- FluentActions . Invoking ( ( ) => Deserialize < TestValueObject_Complex_Class , TestValueObject_Complex_Class . ValueObjectJsonConverterFactory > ( "[]" ) )
138
+ FluentActions . Invoking ( ( ) => DeserializeWithConverter < TestValueObject_Complex_Class , TestValueObject_Complex_Class . ValueObjectJsonConverterFactory > ( "[]" ) )
139
139
. Should ( ) . Throw < JsonException > ( ) . WithMessage ( "Unexpected token \" StartArray\" when trying to deserialize \" TestValueObject_Complex_Class\" . Expected token: \" StartObject\" ." ) ;
140
140
}
141
141
142
- private static T Deserialize < T , TConverterFactory > (
142
+ [ Fact ]
143
+ public void Should_deserialize_using_custom_factory_specified_by_ValueObjectFactoryAttribute ( )
144
+ {
145
+ var value = Deserialize < BoundaryWithFactories , string > ( "\" 1:2\" " ) ;
146
+
147
+ value . Should ( ) . BeEquivalentTo ( BoundaryWithFactories . Create ( 1 , 2 ) ) ;
148
+ }
149
+
150
+ private static T Deserialize < T , TKey > (
151
+ string json ,
152
+ JsonNamingPolicy namingPolicy = null )
153
+ where T : IValueObjectFactory < T , TKey > , IValueObjectConverter < TKey >
154
+ {
155
+ return DeserializeWithConverter < T , ValueObjectJsonConverterFactory > ( json , namingPolicy ) ;
156
+ }
157
+
158
+ private static T DeserializeWithConverter < T , TConverterFactory > (
143
159
string json ,
144
160
JsonNamingPolicy namingPolicy = null ,
145
161
bool propertyNameCaseInsensitive = false ,
0 commit comments