Skip to content

Commit 7587831

Browse files
committed
Fix Conflicting Tests
1 parent 24a196d commit 7587831

16 files changed

+327
-42
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,5 @@ webpack.config.js
264264

265265
# IntelliJ-based IDE configuration (ie. Rider)
266266
/.idea
267+
268+
packages/*

JsonClassGeneratorLib/CodeWriters/CSharpCodeWriter.cs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

JsonClassGeneratorLib/Extensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public static string RemoveSpecialCharacters(this string str)
2323
return Regex.Replace(str, "[^a-zA-Z0-9]+", "", RegexOptions.Compiled);
2424
}
2525

26+
public static string ReplaceSpecialCharacters(this string str, string replaceWith)
27+
{
28+
return Regex.Replace(str, "[^a-zA-Z0-9_]+", replaceWith, RegexOptions.Compiled);
29+
}
30+
2631
public static string ToTitleCase(this string str)
2732
{
2833
StringBuilder sb = new StringBuilder(str.Length);

JsonClassGeneratorLib/JsonFieldInfo.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ private static bool IsContainsSpecialChars(string text)
4242
for (int i = 0; i < text.Length; i++)
4343
{
4444
char c = text[i];
45+
int number;
46+
if (i == 0 && int.TryParse(c.ToString(), out number))
47+
return true;
48+
4549
if (!char.IsLetterOrDigit(c) && c != '_')
46-
{
4750
return true;
48-
}
4951
}
5052

5153
return false;
@@ -57,4 +59,4 @@ public string GetExamplesText()
5759
}
5860

5961
}
60-
}
62+
}

TESTS-JSON-to-CSHARP/TESTS-JSON-to-CSHARP.csproj

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
<Reference Include="Humanizer, Version=2.14.0.0, Culture=neutral, PublicKeyToken=979442b78dfc278e, processorArchitecture=MSIL">
4444
<HintPath>..\packages\Humanizer.Core.2.14.1\lib\netstandard2.0\Humanizer.dll</HintPath>
4545
</Reference>
46+
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
47+
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.7.0.0\lib\netstandard2.0\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
48+
</Reference>
4649
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
4750
<HintPath>..\packages\MSTest.TestFramework.2.1.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
4851
</Reference>
@@ -53,8 +56,29 @@
5356
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
5457
</Reference>
5558
<Reference Include="System" />
59+
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
60+
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
61+
</Reference>
5662
<Reference Include="System.Core" />
63+
<Reference Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
64+
<HintPath>..\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll</HintPath>
65+
</Reference>
5766
<Reference Include="System.Numerics" />
67+
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
68+
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
69+
</Reference>
70+
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
71+
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
72+
</Reference>
73+
<Reference Include="System.Text.Encodings.Web, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
74+
<HintPath>..\packages\System.Text.Encodings.Web.7.0.0\lib\netstandard2.0\System.Text.Encodings.Web.dll</HintPath>
75+
</Reference>
76+
<Reference Include="System.Text.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
77+
<HintPath>..\packages\System.Text.Json.7.0.0\lib\netstandard2.0\System.Text.Json.dll</HintPath>
78+
</Reference>
79+
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
80+
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
81+
</Reference>
5882
<Reference Include="System.Xml" />
5983
</ItemGroup>
6084
<ItemGroup>
@@ -114,6 +138,9 @@
114138
<Content Include="Test_18_WRONG_NAME_RECURSIVE_BUG78_OUTPUT.txt" />
115139
<Content Include="Test_19_PLURAL_NAMES_INPUT.txt" />
116140
<Content Include="Test_19_PLURAL_NAMES_OUTPUT.txt" />
141+
<Content Include="Test_1_6_SETTINGS_JSONPROPERTYNAME_NETCORE_INPUT2.txt" />
142+
<Content Include="Test_1_6_SETTINGS_JSONPROPERTYNAME_NETCORE_OUTPUT2.txt" />
143+
<Content Include="Test_1_6_SETTINGS_JSONPROPERTYNAME_NETCORE_OUTPUT1.txt" />
117144
<Content Include="Test_20_ROOT_ARRAY_INPUT.txt" />
118145
<Content Include="Test_20_ROOT_ARRAY_OUTPUT.txt" />
119146
<Content Include="Test_21_ADD_TWO_ATTRIBUTELIBRAIRY_INPUT.txt" />
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
2-
public class Root {
3-
public string test;
1+
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
2+
public class Root
3+
{
4+
public string test;
5+
46
[JsonProperty("test-test")]
5-
public string TestTest;
6-
public string @continue;
7-
public string @class;
7+
public string testtest;
8+
public string @continue;
9+
public string @class;
810
}
911

TESTS-JSON-to-CSHARP/Test_1_4_SETTINGS_JSONPROPERTY_OUTPUT.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public string test { get; set; }
66

77
[JsonProperty("test-test")]
8-
public string TestTest { get; set; }
8+
public string testtest { get; set; }
99

1010
[JsonProperty("continue")]
1111
public string @continue { get; set; }

TESTS-JSON-to-CSHARP/Test_1_5_SETTINGS_FIELDS_JSONPROPERTY_OUTPUT.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public string test;
66

77
[JsonProperty("test-test")]
8-
public string TestTest;
8+
public string testtest;
99

1010
[JsonProperty("continue")]
1111
public string @continue;

TESTS-JSON-to-CSHARP/Test_1_6_SETTINGS_JSONPROPERTYNAME_NETCORE.cs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
3-
4+
using System.Text.Json.Serialization;
45
using Microsoft.VisualStudio.TestTools.UnitTesting;
56

67
using Xamasoft.JsonClassGenerator;
@@ -14,7 +15,7 @@ namespace TESTS_JSON_TO_CSHARP
1415
public class Test_1_6_SETTINGS_JSONPROPERTYNAME_NETCORE
1516
{
1617
[TestMethod]
17-
public void Run()
18+
public void JsonPropertyNameNetCore()
1819
{
1920
string path = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_1_6_SETTINGS_JSONPROPERTYNAME_NETCORE_INPUT.txt";
2021
string resultPath = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_1_6_SETTINGS_JSONPROPERTYNAME_NETCORE_OUTPUT.txt";
@@ -33,5 +34,54 @@ public void Run()
3334
string resultsCompare = File.ReadAllText(resultPath);
3435
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
3536
}
37+
38+
/// <summary>
39+
/// Fixes https://github.com/Json2CSharp/Json2CSharpCodeGenerator/issues/52
40+
/// </summary>
41+
[TestMethod]
42+
public void ImmutableClassesAndSystemTextJson()
43+
{
44+
string path = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_1_6_SETTINGS_JSONPROPERTYNAME_NETCORE_INPUT.txt";
45+
string resultPath = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_1_6_SETTINGS_JSONPROPERTYNAME_NETCORE_OUTPUT1.txt";
46+
string input = File.ReadAllText(path);
47+
48+
CSharpCodeWriterConfig csharpCodeWriterConfig = new CSharpCodeWriterConfig();
49+
csharpCodeWriterConfig.AttributeLibrary = JsonLibrary.SystemTextJson;
50+
csharpCodeWriterConfig.OutputType = OutputTypes.ImmutableClass;
51+
csharpCodeWriterConfig.UsePascalCase = true;
52+
csharpCodeWriterConfig.AttributeUsage = JsonPropertyAttributeUsage.Always;
53+
CSharpCodeWriter csharpCodeWriter = new CSharpCodeWriter(csharpCodeWriterConfig);
54+
55+
JsonClassGenerator jsonClassGenerator = new JsonClassGenerator();
56+
jsonClassGenerator.CodeWriter = csharpCodeWriter;
57+
58+
string returnVal = jsonClassGenerator.GenerateClasses(input, out string errorMessage).ToString();
59+
string resultsCompare = File.ReadAllText(resultPath);
60+
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
61+
}
62+
63+
64+
[TestMethod]
65+
public void RegressionCreatedAtUnderscoreIssue()
66+
{
67+
string path = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_1_6_SETTINGS_JSONPROPERTYNAME_NETCORE_INPUT2.txt";
68+
string resultPath = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_1_6_SETTINGS_JSONPROPERTYNAME_NETCORE_OUTPUT2.txt";
69+
string input = File.ReadAllText(path);
70+
71+
CSharpCodeWriterConfig csharpCodeWriterConfig = new CSharpCodeWriterConfig();
72+
csharpCodeWriterConfig.AttributeLibrary = JsonLibrary.SystemTextJson;
73+
csharpCodeWriterConfig.OutputType = OutputTypes.ImmutableClass;
74+
csharpCodeWriterConfig.AttributeUsage = JsonPropertyAttributeUsage.Always;
75+
CSharpCodeWriter csharpCodeWriter = new CSharpCodeWriter(csharpCodeWriterConfig);
76+
77+
JsonClassGenerator jsonClassGenerator = new JsonClassGenerator();
78+
jsonClassGenerator.CodeWriter = csharpCodeWriter;
79+
80+
string returnVal = jsonClassGenerator.GenerateClasses(input, out string errorMessage).ToString();
81+
string resultsCompare = File.ReadAllText(resultPath);
82+
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
83+
}
84+
3685
}
3786
}
87+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"user_id":"user_id_value",
3+
"1user_id":"user_id_value",
4+
"created_at":"2015-06-02 23:33:90",
5+
"updated_at":"2015-06-02 23:33:90"
6+
}

0 commit comments

Comments
 (0)