Skip to content

Commit 46e9400

Browse files
committed
Java Reserved Keyword Tests Added
1 parent 9b31b57 commit 46e9400

File tree

6 files changed

+57
-22
lines changed

6 files changed

+57
-22
lines changed

JsonClassGeneratorLib/CodeWriters/JavaCodeWriter.cs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ public string DisplayName
1818
get { return "Java"; }
1919
}
2020

21-
IReadOnlyCollection<string> ICodeBuilder.ReservedKeywords => throw new NotImplementedException();
22-
bool ICodeBuilder.IsReservedKeyword(string word) => throw new NotImplementedException();
21+
private static readonly HashSet<string> _reservedKeywords = new HashSet<string>(comparer: StringComparer.Ordinal) {
22+
"class"
23+
};
24+
25+
IReadOnlyCollection<string> ICodeBuilder.ReservedKeywords => _reservedKeywords;
26+
public bool IsReservedKeyword(string word) => _reservedKeywords.Contains(word ?? string.Empty);
2327

2428
public string GetTypeName(JsonType type, IJsonClassGeneratorConfig config)
2529
{
@@ -74,29 +78,12 @@ public void WriteClassMembers(IJsonClassGeneratorConfig config, StringBuilder sw
7478
{
7579
if (config.UsePascalCase || config.ExamplesInDocumentation) sw.AppendLine();
7680

77-
// if (config.UsePascalCase || config.UseJsonAttributes)
78-
// {
79-
//
80-
// }
81-
//
82-
//
83-
// if (config.UseFields)
84-
// {
85-
// sw.AppendFormat(prefix + "@JsonProperty(\"{0}\"){1}", field.JsonMemberName, Environment.NewLine);
86-
// }
87-
// else
88-
// {
89-
// sw.AppendFormat(prefix + "@JsonProperty" + "(\"{0}\"){1}", field.JsonMemberName,Environment.NewLine);
90-
// sw.AppendFormat(prefix + "public {0} get{1}() {{ \r\t\t return this.{2} \r\t}}", field.Type.GetTypeName(), ChangeFirstChar(field.MemberName), ChangeFirstChar(field.MemberName, false));
91-
// sw.AppendFormat(prefix + "public {0} set{1}({0} {2}) {{ \r\t\t this.{2} = {2} \r\t}}", field.Type.GetTypeName(), ChangeFirstChar(field.MemberName), ChangeFirstChar(field.MemberName, false));
92-
// sw.AppendFormat(prefix + "{0} {1};", field.Type.GetTypeName(), ChangeFirstChar(field.MemberName, false));
93-
// sw.AppendLine();
94-
// }
95-
9681
// Check if property name starts with number
9782
string memberName = field.MemberName;
9883
if (!string.IsNullOrEmpty(field.MemberName) && char.IsDigit(field.MemberName[0])) memberName = "_" + memberName;
9984

85+
if (this.IsReservedKeyword(memberName)) memberName = "my" + memberName;
86+
10087
if (config.UseProperties)
10188
{
10289
sw.AppendFormat(prefix + "@JsonProperty" + "(\"{0}\") {1}", field.JsonMemberName,Environment.NewLine);

TESTS-JSON-to-POJO/CreateTest.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
$TestName = Read-Host -Prompt 'Test Name With Number: THE FORMAT IS [TESTNUMBER]_[TESTNAME], EXAMPLE : 10_TESTNAME.'
1+
set-executionpolicy unrestricted
2+
3+
$TestName = Read-Host -Prompt 'Test Name With Number: THE FORMAT IS [TESTNUMBER]_[TESTNAME], EXAMPLE : 10_TESTNAME.'
24
# TO CHANGE $Dir TO CHANGE $Dir TO CHANGE $Dir TO CHANGE $Dir TO CHANGE $Dir TO CHANGE $Dir TO CHANGE $Dir TO CHANGE $Dir
35

46
$Dir = ($psise.CurrentFile.FullPath -replace "CreateTest.ps1", "")

TESTS-JSON-to-POJO/TESTS-JSON-to-POJO.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<Compile Include="Test_0_BASIC_SCENARIO.cs" />
5555
<Compile Include="Test_2_HANDLE_NUMBERS.cs" />
5656
<Compile Include="Test_3_SETTINGS.cs" />
57+
<Compile Include="Test_4_JAVA_RESERVED_KEYWORDS.cs" />
5758
</ItemGroup>
5859
<ItemGroup>
5960
<None Include="CreateTest.ps1" />
@@ -70,6 +71,8 @@
7071
<Content Include="Test_3_SETTINGS_ADDJSONATTRIBUTE_INPUT.txt" />
7172
<Content Include="Test_3_SETTINGS_USEPASCAL_OUTPUT.txt" />
7273
<Content Include="Test_3_SETTINGS_ADDJSONATTRIBUTE_OUTPUT.txt" />
74+
<Content Include="Test_4_JAVA_RESERVED_KEYWORDS_INPUT.txt" />
75+
<Content Include="Test_4_JAVA_RESERVED_KEYWORDS_OUTPUT.txt" />
7376
</ItemGroup>
7477
<ItemGroup>
7578
<ProjectReference Include="..\JsonClassGeneratorLib\JsonClassGeneratorLib.csproj">
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Text;
7+
using Xamasoft.JsonClassGenerator;
8+
using Xamasoft.JsonClassGenerator.CodeWriters;
9+
10+
namespace TESTS_JSON_to_POJO
11+
{
12+
13+
[TestClass]
14+
public class Test_4_JAVA_RESERVED_KEYWORDS
15+
{
16+
17+
[TestMethod]
18+
public void Run()
19+
{
20+
string path = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_4_JAVA_RESERVED_KEYWORDS_INPUT.txt";
21+
string resultPath = Directory.GetCurrentDirectory().Replace("bin\\Debug", "") + @"Test_4_JAVA_RESERVED_KEYWORDS_OUTPUT.txt";
22+
string input = File.ReadAllText(path);
23+
string errorMessage = string.Empty;
24+
JavaCodeWriter javaCodeWriter = new JavaCodeWriter();
25+
JsonClassGenerator jsonClassGenerator = new JsonClassGenerator();
26+
jsonClassGenerator.CodeWriter = javaCodeWriter;
27+
string returnVal = jsonClassGenerator.GenerateClasses(input, out errorMessage).ToString();
28+
string resultsCompare = File.ReadAllText(resultPath);
29+
Assert.AreEqual(resultsCompare.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\t", ""), returnVal.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\t", ""));
30+
}
31+
}
32+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"class": "user"
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// import com.fasterxml.jackson.databind.ObjectMapper; // version 2.11.1
2+
// import com.fasterxml.jackson.annotation.JsonProperty; // version 2.11.1
3+
/* ObjectMapper om = new ObjectMapper();
4+
Root root = om.readValue(myJsonString), Root.class); */
5+
public class Root{
6+
public String myclass;
7+
}
8+

0 commit comments

Comments
 (0)