1+ using System ;
2+ using System . Runtime . Serialization ;
3+ using NUnit . Framework ;
4+
5+ namespace ServiceStack . Text . Tests . JsonTests
6+ {
7+ public class OnDeserializationErrorTests
8+ {
9+ [ Test ]
10+ public void Invokes_callback_on_protected_setter ( )
11+ {
12+ string json = @"{""idBadProt"":""value"", ""idGood"":""2"" }" ;
13+
14+ AssertThatInvalidJsonInvokesExpectedCallback < TestDto > ( json , "idBadProt" , "value" , typeof ( int ) , "Input string was not in a correct format." ) ;
15+ }
16+
17+ [ Test ]
18+ public void Invokes_callback_on_incorrect_type ( )
19+ {
20+ string json = @"{""idBad"":""abc"", ""idGood"":""2"" }" ;
21+ AssertThatInvalidJsonInvokesExpectedCallback < TestDto > ( json , "idBad" , "abc" , typeof ( int ) , "Input string was not in a correct format." ) ;
22+ }
23+
24+ [ Test ]
25+ public void Invokes_callback_on_incorrect_type_with_data_set ( )
26+ {
27+ string json = @"{""idBad"":""abc"", ""idGood"":""2"" }" ;
28+ AssertThatInvalidJsonInvokesExpectedCallback < TestDto > ( json , "idBad" , "abc" , typeof ( int ) , "Input string was not in a correct format." ) ;
29+ }
30+
31+ [ Test ]
32+ public void Invokes_callback_on_value_out_of_range ( )
33+ {
34+ string json = @"{""idBad"":""4700000007"", ""idGood"":""2"" }" ;
35+ AssertThatInvalidJsonInvokesExpectedCallback < TestDto > ( json , "idBad" , "4700000007" , typeof ( int ) , "Value was either too large or too small for an Int32." ) ;
36+ }
37+
38+ [ Test ]
39+ public void Does_not_invoke_callback_on_valid_data ( )
40+ {
41+ JsConfig . Reset ( ) ;
42+ JsConfig . OnDeserializationErrorCallback = ( o , s , s1 , arg3 , arg4 ) => Assert . Fail ( "For valida data this should not be invoked" ) ;
43+
44+ var json = @"{""idBad"":""2"", ""idGood"":""2"" }" ;
45+ JsonSerializer . DeserializeFromString ( json , typeof ( TestDto ) ) ;
46+ }
47+
48+ [ Test ]
49+ public void TestReset ( )
50+ {
51+ JsConfig . Reset ( ) ;
52+ Assert . IsNull ( JsConfig . OnDeserializationErrorCallback ) ;
53+ JsConfig . OnDeserializationErrorCallback = ( o , s , s1 , arg3 , arg4 ) => { } ;
54+ Assert . IsNotNull ( JsConfig . OnDeserializationErrorCallback ) ;
55+ JsConfig . Reset ( ) ;
56+ Assert . IsNull ( JsConfig . OnDeserializationErrorCallback ) ;
57+ }
58+
59+ [ Test ]
60+ public void Invokes_callback_deserialization_of_array_with_missing_comma ( )
61+ {
62+ string json = @"{""Values"": [ { ""Val"": ""a""} { ""Val"": ""b""}] }" ;
63+
64+ AssertThatInvalidJsonInvokesExpectedCallback < TestDtoWithArray > ( json , "Values" , @"[ { ""Val"": ""a""} { ""Val"": ""b""}]" , typeof ( TestChildDto [ ] ) , "Type definitions should start with a '{', expecting serialized type 'TestChildDto', got string starting with: Val" ) ;
65+ }
66+
67+ [ Test ]
68+ public void Does_not_invoke_callback_on_valid_data_with_array ( )
69+ {
70+ JsConfig . Reset ( ) ;
71+ JsConfig . OnDeserializationErrorCallback = ( o , s , s1 , arg3 , arg4 ) => Assert . Fail ( "For valida data this should not be invoked" ) ;
72+
73+ var json = @"{""Values"": [ { ""Val"": ""a""}, { ""Val"": ""b""}] }" ;
74+ JsonSerializer . DeserializeFromString ( json , typeof ( TestDtoWithArray ) ) ;
75+ }
76+
77+ [ DataContract ]
78+ class TestDtoWithArray
79+ {
80+ [ DataMember ]
81+ public TestChildDto [ ] Values { get ; set ; }
82+ }
83+
84+ [ DataContract ]
85+ class TestChildDto
86+ {
87+ [ DataMember ]
88+ public string Val { get ; set ; }
89+ }
90+
91+ [ DataContract ]
92+ class TestDto
93+ {
94+ [ DataMember ( Name = "idBadProt" ) ]
95+ public int protId { get ; protected set ; }
96+ [ DataMember ( Name = "idGood" ) ]
97+ public int IdGood { get ; set ; }
98+ [ DataMember ( Name = "idBad" ) ]
99+ public int IdBad { get ; set ; }
100+ }
101+
102+ private static void AssertThatInvalidJsonInvokesExpectedCallback < T > ( string json , string expectedProperty , string expectedValue , Type expectedType , string expectedExceptionMessage )
103+ {
104+ string property = null , value = null ;
105+ Type type = null ;
106+ Exception ex = null ;
107+ bool callbackInvoked = false ;
108+ object deserialized = null ;
109+
110+ JsConfig . Reset ( ) ;
111+ JsConfig . OnDeserializationErrorCallback = ( o , s , s1 , arg3 , arg4 ) =>
112+ {
113+ deserialized = o ;
114+ property = s ;
115+ value = s1 ;
116+ type = arg3 ;
117+ ex = arg4 ;
118+ callbackInvoked = true ;
119+ } ;
120+
121+
122+ JsonSerializer . DeserializeFromString ( json , typeof ( T ) ) ;
123+
124+ Assert . IsTrue ( callbackInvoked , "Callback should be invoked" ) ;
125+ Assert . AreEqual ( expectedProperty , property ) ;
126+ Assert . AreEqual ( expectedValue , value ) ;
127+ Assert . AreEqual ( expectedType , type ) ;
128+ Assert . AreEqual ( expectedExceptionMessage , ex . Message ) ;
129+ Assert . IsNotNull ( deserialized ) ;
130+ Assert . IsInstanceOf < T > ( deserialized ) ;
131+ }
132+ }
133+ }
0 commit comments