@@ -9,6 +9,16 @@ namespace Xamasoft.JsonClassGenerator.CodeWriters
99{
1010 public class CSharpCodeWriter : ICodeWriter
1111 {
12+ private static HashSet < string > _csharpKeywords = new HashSet < string >
13+ {
14+ "abstract" , "as" , "base" , "bool" , "break" , "byte" , "case" , "catch" , "char" , "checked" , "class" , "const" , "continue" , "decimal" , "default" ,
15+ "delegate" , "do" , "double" , "else" , "enum" , "event" , "explicit" , "extern" , "false" , "finally" , "fixed" , "float" , "for" , "foreach" , "goto" ,
16+ "if" , "implicit" , "in" , "int" , "interface" , "internal" , "is" , "lock" , "long" , "namespace" , "new" , "null" , "object" , "operator" , "out" ,
17+ "override" , "params" , "private" , "protected" , "public" , "readonly" , "ref" , "return" , "sbyte" , "sealed" , "short" , "sizeof" , "stackalloc" ,
18+ "static" , "string" , "struct" , "switch" , "this" , "throw" , "true" , "try" , "typeof" , "uint" , "ulong" , "unchecked" , "unsafe" , "ushort" , "using" ,
19+ "virtual" , "void" , "volatile" , "while"
20+ } ;
21+
1222 public string FileExtension
1323 {
1424 get { return ".cs" ; }
@@ -46,7 +56,7 @@ public string GetTypeName(JsonType type, IJsonClassGeneratorConfig config)
4656 case JsonTypeEnum . NullableSomething : return "object" ;
4757 case JsonTypeEnum . Object : return type . AssignedName ;
4858 case JsonTypeEnum . String : return "string" ;
49- default : throw new System . NotSupportedException ( "Unsupported json type" ) ;
59+ default : throw new NotSupportedException ( "Unsupported json type" ) ;
5060 }
5161 }
5262
@@ -193,15 +203,14 @@ private void WriteClassMembers(IJsonClassGeneratorConfig config, TextWriter sw,
193203 sw . WriteLine ( prefix + "/// </summary>" ) ;
194204 }
195205
196- if ( config . UsePascalCase )
206+ if ( true || config . UsePascalCase )
197207 {
198-
199208 sw . WriteLine ( prefix + "[JsonProperty(\" {0}\" )]" , field . JsonMemberName ) ;
200209 }
201210
202211 if ( config . UseProperties )
203212 {
204- sw . WriteLine ( prefix + "public {0} {1} {{ get; set; }}" , field . Type . GetTypeName ( ) , field . MemberName ) ;
213+ sw . WriteLine ( prefix + "public {0} {1}{2} {{ get; set; }}" , field . Type . GetTypeName ( ) , ( ShouldPrefix ( field . MemberName ) || field . MemberName == type . AssignedName ) ? "_" : "" , field . MemberName ) ;
205214 }
206215 else
207216 {
@@ -212,7 +221,16 @@ private void WriteClassMembers(IJsonClassGeneratorConfig config, TextWriter sw,
212221 }
213222
214223
224+ private bool ShouldPrefix ( string fieldName )
225+ {
226+ if ( ! Char . IsLetter ( fieldName . ToCharArray ( ) [ 0 ] ) )
227+ return true ;
215228
229+ if ( _csharpKeywords . Contains ( fieldName ) )
230+ return true ;
231+
232+ return false ;
233+ }
216234
217235
218236
@@ -234,7 +252,7 @@ private void WriteClassWithPropertiesExplicitDeserialization(TextWriter sw, Json
234252 string variable = null ;
235253 if ( field . Type . MustCache )
236254 {
237- variable = "_" + char . ToLower ( field . MemberName [ 0 ] ) + field . MemberName . Substring ( 1 ) ;
255+ variable = "_" + Char . ToLower ( field . MemberName [ 0 ] ) + field . MemberName . Substring ( 1 ) ;
238256 sw . WriteLine ( prefix + "[System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]" ) ;
239257 sw . WriteLine ( prefix + "private {0} {1};" , field . Type . GetTypeName ( ) , variable ) ;
240258 }
@@ -295,6 +313,5 @@ private void WriteClassWithFieldsExplicitDeserialization(TextWriter sw, JsonType
295313 }
296314 }
297315 #endregion
298-
299316 }
300317}
0 commit comments