This repository was archived by the owner on Dec 24, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +27
-3
lines changed
src/ServiceStack.Text/Common
tests/ServiceStack.Text.Tests Expand file tree Collapse file tree 2 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -32,7 +32,7 @@ private static ParseStringDelegate GetParseFn()
32
32
{
33
33
//Note the generic typeof(T) is faster than using var type = typeof(T)
34
34
if ( typeof ( T ) == typeof ( bool ) )
35
- return value => bool . Parse ( value ) ;
35
+ return value => value . Length == 1 ? value == "1" : bool . Parse ( value ) ;
36
36
if ( typeof ( T ) == typeof ( byte ) )
37
37
return value => byte . Parse ( value ) ;
38
38
if ( typeof ( T ) == typeof ( sbyte ) )
@@ -79,7 +79,7 @@ private static ParseStringDelegate GetParseFn()
79
79
return value => ulong . Parse ( value ) ;
80
80
81
81
if ( typeof ( T ) == typeof ( bool ? ) )
82
- return value => string . IsNullOrEmpty ( value ) ? ( bool ? ) null : bool . Parse ( value ) ;
82
+ return value => string . IsNullOrEmpty ( value ) ? ( bool ? ) null : value . Length == 1 ? value == "1" : bool . Parse ( value ) ;
83
83
if ( typeof ( T ) == typeof ( byte ? ) )
84
84
return value => string . IsNullOrEmpty ( value ) ? ( byte ? ) null : byte . Parse ( value ) ;
85
85
if ( typeof ( T ) == typeof ( sbyte ? ) )
Original file line number Diff line number Diff line change 1
1
using System ;
2
2
using System . Drawing ;
3
+ using System . Runtime . Serialization ;
3
4
using NUnit . Framework ;
4
5
using ServiceStack . Common ;
5
6
@@ -76,5 +77,28 @@ public void Can_serialize_dto_with_enum_flags()
76
77
77
78
Assert . That ( deserialized . Enum , Is . EqualTo ( ExampleEnum . One | ExampleEnum . Four ) ) ;
78
79
}
79
- }
80
+
81
+ [ DataContract ]
82
+ public class Item
83
+ {
84
+ [ DataMember ( Name = "favorite" ) ]
85
+ public bool IsFavorite { get ; set ; }
86
+ }
87
+
88
+ [ Test ]
89
+ public void Can_customize_bool_deserialization ( )
90
+ {
91
+ var dto1 = "{\" favorite\" :1}" . FromJson < Item > ( ) ;
92
+ Assert . That ( dto1 . IsFavorite , Is . True ) ;
93
+
94
+ var dto0 = "{\" favorite\" :0}" . FromJson < Item > ( ) ;
95
+ Assert . That ( dto0 . IsFavorite , Is . False ) ;
96
+
97
+ var dtoTrue = "{\" favorite\" :true}" . FromJson < Item > ( ) ;
98
+ Assert . That ( dtoTrue . IsFavorite , Is . True ) ;
99
+
100
+ var dtoFalse = "{\" favorite\" :0}" . FromJson < Item > ( ) ;
101
+ Assert . That ( dtoFalse . IsFavorite , Is . False ) ;
102
+ }
103
+ }
80
104
}
You can’t perform that action at this time.
0 commit comments