This repository was archived by the owner on Dec 24, 2022. It is now read-only.
File tree Expand file tree Collapse file tree
src/ServiceStack.Text/Common
tests/ServiceStack.Text.Tests Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -30,9 +30,7 @@ private ParseStringDelegate GetCoreParseFn<T>()
3030 return value => JsConfig < T > . ParseFn ( Serializer , value ) ;
3131
3232 if ( type . IsEnum ( ) )
33- {
34- return x => Enum . Parse ( type , Serializer . UnescapeSafeString ( x ) , true ) ;
35- }
33+ return x => ParseUtils . TryParseEnum ( type , Serializer . UnescapeSafeString ( x ) ) ;
3634
3735 if ( type == typeof ( string ) )
3836 return Serializer . UnescapeString ;
@@ -44,9 +42,6 @@ private ParseStringDelegate GetCoreParseFn<T>()
4442 if ( specialParseFn != null )
4543 return specialParseFn ;
4644
47- if ( type . IsEnum ( ) )
48- return x => Enum . Parse ( type , x , true ) ;
49-
5045 if ( type . IsArray )
5146 {
5247 return DeserializeArray < T , TSerializer > . Parse ;
Original file line number Diff line number Diff line change @@ -60,6 +60,14 @@ public static Type ParseType(string assemblyQualifiedName)
6060 {
6161 return AssemblyUtils . FindType ( assemblyQualifiedName . FromCsvField ( ) ) ;
6262 }
63+
64+ public static object TryParseEnum ( Type enumType , string str )
65+ {
66+ if ( JsConfig . EmitLowercaseUnderscoreNames )
67+ str = str . Replace ( "_" , "" ) ;
68+
69+ return Enum . Parse ( enumType , str , ignoreCase : true ) ;
70+ }
6371 }
6472
6573}
Original file line number Diff line number Diff line change @@ -134,6 +134,27 @@ public void Can_use_enum_as_key_in_map()
134134 var map = json . FromJson < Dictionary < AnEnum , int > > ( ) ;
135135 Assert . That ( map [ AnEnum . This ] , Is . EqualTo ( 1 ) ) ;
136136 }
137+
138+ public enum EnumStyles
139+ {
140+ None = 0 ,
141+ Word ,
142+ DoubleWord ,
143+ lowerWord ,
144+ Underscore_Words ,
145+ }
146+
147+ [ Test ]
148+ public void Can_serialize_different_enum_styles ( )
149+ {
150+ Assert . That ( "Word" . FromJson < EnumStyles > ( ) , Is . EqualTo ( EnumStyles . Word ) ) ;
151+ Assert . That ( "DoubleWord" . FromJson < EnumStyles > ( ) , Is . EqualTo ( EnumStyles . DoubleWord ) ) ;
152+
153+ using ( JsConfig . With ( emitLowercaseUnderscoreNames : true ) )
154+ {
155+ Assert . That ( "Double_Word" . FromJson < EnumStyles > ( ) , Is . EqualTo ( EnumStyles . DoubleWord ) ) ;
156+ }
157+ }
137158 }
138159}
139160
You can’t perform that action at this time.
0 commit comments