@@ -242,9 +242,9 @@ public void WriteClassMembers(StringBuilder sw, JsonType type, string indentMemb
242242 {
243243 string classPropertyName = field . MemberName ;
244244
245- if ( config . UsePascalCase || field . ContainsSpecialChars )
245+ if ( config . UsePascalCase )
246246 classPropertyName = field . MemberName . ToTitleCase ( ) ;
247-
247+
248248 classPropertyName = this . CheckSyntax ( classPropertyName ) ;
249249
250250 string propertyAttribute = GetCSharpJsonAttributeCode ( field ) ;
@@ -406,23 +406,25 @@ private void WriteClassConstructor(StringBuilder sw, JsonType type, string inden
406406
407407 foreach ( JsonFieldInfo field in type . Fields )
408408 {
409- // Writes something like: `[JsonProperty("foobar")] string foobar,`
410-
411409 string ctorParameterName = field . MemberName ;
412- if ( config . UsePascalCase || field . ContainsSpecialChars )
410+ if ( config . UsePascalCase ) {
411+
413412 ctorParameterName = ctorParameterName . ToTitleCase ( ) ;
413+ ctorParameterName = this . GetCSharpCamelCaseName ( ctorParameterName ) ;
414+ }
415+ else if ( field . ContainsSpecialChars )
416+ {
417+ ctorParameterName = this . GetCSharpCamelCaseName ( ctorParameterName ) ;
418+ }
414419
415- ctorParameterName = this . GetCSharpCamelCaseName ( ctorParameterName ) ;
416420
417421 bool isLast = Object . ReferenceEquals ( field , lastField ) ;
418422 string comma = isLast ? "" : "," ;
419423
420- //
421-
422424 sw . Append ( indentBodies ) ;
423425
424426 string attribute = GetCSharpJsonAttributeCode ( field ) ;
425- if ( attribute . Length > 0 )
427+ if ( ! string . IsNullOrEmpty ( attribute ) && config . AttributeLibrary == JsonLibrary . NewtonsoftJson )
426428 {
427429 sw . Append ( attribute ) ;
428430 sw . Append ( ' ' ) ;
@@ -440,14 +442,21 @@ private void WriteClassConstructor(StringBuilder sw, JsonType type, string inden
440442
441443 foreach ( JsonFieldInfo field in type . Fields )
442444 {
443- string classPropertyName = field . MemberName ;
444- if ( config . UsePascalCase || field . ContainsSpecialChars )
445- classPropertyName = classPropertyName . ToTitleCase ( ) ;
445+ string ctorName = field . MemberName ;
446+ string classMemberName = field . MemberName ;
447+ if ( config . UsePascalCase ) {
446448
447- string ctorParameterName = field . MemberName . ToTitleCase ( ) ;
448- ctorParameterName = this . GetCSharpCamelCaseName ( ctorParameterName ) ;
449+ classMemberName = ctorName . ToTitleCase ( ) ;
450+ ctorName = ctorName . ToTitleCase ( ) ;
451+ ctorName = this . GetCSharpCamelCaseName ( ctorName ) ;
452+ }
453+ else if ( field . ContainsSpecialChars && ! config . UsePascalCase )
454+ {
455+ classMemberName = this . GetCSharpCamelCaseName ( classMemberName ) ;
456+ ctorName = this . GetCSharpCamelCaseName ( ctorName ) ;
457+ }
449458
450- sw . AppendFormat ( indentBodies + "this.{0} = {1};{2}" , /*0:*/ classPropertyName , /*1:*/ ctorParameterName , /*2:*/ Environment . NewLine ) ;
459+ sw . AppendFormat ( indentBodies + "this.{0} = {1};{2}" , /*0:*/ classMemberName , /*1:*/ ctorName , /*2:*/ Environment . NewLine ) ;
451460 }
452461
453462 sw . AppendLine ( indentMembers + "}" ) ;
@@ -613,9 +622,12 @@ private string GetCSharpCamelCaseName(string camelCaseFromJson)
613622 /// <summary>Converts an identifier from JSON into a C#-safe PascalCase identifier.</summary>
614623 private string CheckSyntax ( string name )
615624 {
625+ name = name . ReplaceSpecialCharacters ( "" ) ;
626+
616627 // Check if property is a reserved keyword
617628 if ( this . IsReservedKeyword ( name ) ) name = "@" + name ;
618629
630+
619631 // Check if property name starts with number
620632 if ( ! string . IsNullOrEmpty ( name ) && char . IsDigit ( name [ 0 ] ) ) name = "_" + name ;
621633
0 commit comments