Added Json2CSharpCodeGenerator.Tests

This commit is contained in:
Mike Phares 2022-08-13 14:15:28 -07:00
parent 2385affccb
commit 29fb70ce06
86 changed files with 61170 additions and 19 deletions

View File

@ -11,14 +11,85 @@ public class CSharpCodeWriter : ICodeBuilder
private const string _NoRenameAttribute = "[Obfuscation(Feature = \"renaming\", Exclude = true)]";
private const string _NoPruneAttribute = "[Obfuscation(Feature = \"trigger\", Exclude = false)]";
private static readonly HashSet<string> _ReservedKeywords = new(comparer: StringComparer.Ordinal) {
"abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", "continue",
"decimal", "default", "delegate", "do", "double", "else", "enum", "event", "explicit", "extern", "false", "finally",
"fixed", "float", "for", "foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock", "long",
"namespace", "new", "null", "object", "operator", "out", "override", "params", "private", "protected", "public",
"readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc", "static", "string", "struct",
"switch", "this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", "using",
"virtual", "void", "volatile", "while"
private static readonly HashSet<string> _ReservedKeywords = new(comparer: StringComparer.Ordinal)
{
"abstract",
"as",
"base",
"bool",
"break",
"byte",
"case",
"catch",
"char",
"checked",
"class",
"const",
"continue",
"decimal",
"default",
"delegate",
"do",
"double",
"else",
"enum",
"event",
"explicit",
"extern",
"false",
"finally",
"fixed",
"float",
"for",
"foreach",
"goto",
"if",
"implicit",
"in",
"int",
"interface",
"internal",
"is",
"lock",
"long",
"namespace",
"new",
"null",
"object",
"operator",
"out",
"override",
"params",
"private",
"protected",
"public",
"readonly",
"ref",
"return",
"sbyte",
"sealed",
"short",
"sizeof",
"stackalloc",
"static",
"string",
"struct",
"switch",
"this",
"throw",
"true",
"try",
"typeof",
"uint",
"ulong",
"unchecked",
"unsafe",
"ushort",
"using",
"virtual",
"void",
"volatile",
"while"
};
public bool IsReservedKeyword(string word) => _ReservedKeywords.Contains(word ?? string.Empty);
@ -500,7 +571,7 @@ public class CSharpCodeWriter : ICodeBuilder
string ctorParameterName = GetCSharpCamelCaseName(field.MemberName);
string classPropertyName = GetCSharpPascalCaseName(field.MemberName);
if (config.MutableClasses.ReadOnlyCollectionProperties || !string.IsNullOrEmpty(classPropertyName))
if (!config.UseThisKeyWord)
_ = sw.AppendFormat(indentBodies + "{0} = {1};{2}", /*0:*/ classPropertyName, /*1:*/ ctorParameterName, /*2:*/ Environment.NewLine);
else
_ = sw.AppendFormat(indentBodies + "this.{0} = {1};{2}", /*0:*/ classPropertyName, /*1:*/ ctorParameterName, /*2:*/ Environment.NewLine);

View File

@ -8,17 +8,79 @@ public class JavaCodeWriter : ICodeBuilder
public string DisplayName => "Java";
private static readonly HashSet<string> _ReservedKeywords = new(comparer: StringComparer.Ordinal) {
private static readonly HashSet<string> _ReservedKeywords = new(comparer: StringComparer.Ordinal)
{
// https://en.wikipedia.org/wiki/List_of_Java_keywords
// `Array.from( document.querySelectorAll('dl > dt > code') ).map( e => '"' + e.textContent + '"' ).sort().join( ", " )`
"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "const", "continue",
"default", "do", "double", "else", "enum", "extends", "false", "final", "finally", "float", "for", "goto", "goto",
"if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "non-sealed", "null",
"open", "opens", "package", "permits", "private", "protected", "provides", "public", "record", "return",
"sealed", "short", "static", "strictfp", "super", "switch", "synchronized",
"this", "throw", "throws", "to", "transient", "transitive", "true", "try",
"uses", "var", "void", "volatile", "while", "with", "yield"
"abstract",
"assert",
"boolean",
"break",
"byte",
"case",
"catch",
"char",
"class",
"const",
"const",
"continue",
"default",
"do",
"double",
"else",
"enum",
"extends",
"false",
"final",
"finally",
"float",
"for",
"goto",
"goto",
"if",
"implements",
"import",
"instanceof",
"int",
"interface",
"long",
"native",
"new",
"non-sealed",
"null",
"open",
"opens",
"package",
"permits",
"private",
"protected",
"provides",
"public",
"record",
"return",
"sealed",
"short",
"static",
"strictfp",
"super",
"switch",
"synchronized",
"this",
"throw",
"throws",
"to",
"transient",
"transitive",
"true",
"try",
"uses",
"var",
"void",
"volatile",
"while",
"with",
"yield"
};
IReadOnlyCollection<string> ICodeBuilder.ReservedKeywords => _ReservedKeywords;

View File

@ -40,7 +40,8 @@ public interface IJsonClassGeneratorConfig
/// <summary>When <see langword="true"/>, then <see cref="System.Reflection.ObfuscationAttribute"/> will be applied to generated types.</summary>
bool ApplyObfuscationAttributes { get; set; }
bool SingleFile { get; set; }
// bool SingleFile { get; set; }
bool UseThisKeyWord { get; set; }
ICodeBuilder CodeWriter { get; set; }
bool AlwaysUseNullableValues { get; set; }
bool ExamplesInDocumentation { get; set; }

View File

@ -24,6 +24,7 @@ public class JsonClassGenerator : IJsonClassGeneratorConfig
public bool InternalVisibility { get; set; }
public bool NoHelperClass { get; set; }
public string MainClass { get; set; }
public bool UseThisKeyWord { get; set; }
public bool UsePascalCase { get; set; }
public bool UseNestedClasses { get; set; }
public bool ApplyObfuscationAttributes { get; set; }

View File

@ -0,0 +1,193 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
<LangVersion>10.0</LangVersion>
<Nullable>disable</Nullable>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<VSTestLogger>trx</VSTestLogger>
<VSTestResultsDirectory>../../../../05_TestResults/TestResults</VSTestResultsDirectory>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Json2CSharpCodeGenerator.Lib\Json2CSharpCodeGenerator.Lib.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="Test_0_DIAGNOSTICS_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_0_DIAGNOSTICS_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_10_SETTINGS_IMMUTABLE_CLASSES_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_10_SETTINGS_IMMUTABLE_CLASSES_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_11_NoListSetter_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_11_NoListSetter_INPUT1.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_11_NoListSetter_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_11_NoListSetter_OUTPUT1.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_17_RECORD_TYPES_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_17_RECORD_TYPES_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_17_RECORD_TYPES_OUTPUT_NEWTONSOFT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_17_RECORD_TYPES_OUTPUT_SYSTEMTEXTJSON.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_18_WRONG_NAME_RECURSIVE_BUG78_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_18_WRONG_NAME_RECURSIVE_BUG78_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_19_PLURAL_NAMES_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_19_PLURAL_NAMES_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_20_ROOT_ARRAY_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_20_ROOT_ARRAY_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_5_BASIC_SCENARIO_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_5_BASIC_SCENARIO_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_1_2_SETTINGS_PASCAL_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_1_2_SETTINGS_PASCAL_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_1_3_SETTINGS_FIELDS_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_1_3_SETTINGS_FIELDS_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_1_4_SETTINGS_JSONPROPERTY_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_1_4_SETTINGS_JSONPROPERTY_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_1_5_SETTINGS_FIELDS_JSONPROPERTY_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_1_5_SETTINGS_FIELDS_JSONPROPERTY_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_1_6_SETTINGS_JSONPROPERTYNAME_NETCORE_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_1_6_SETTINGS_JSONPROPERTYNAME_NETCORE_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_1_SETTINGS_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_1_SETTINGS_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_2_CHECK_RESERVED_KEYWORDS_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_2_CHECK_RESERVED_KEYWORDS_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_3_ReplaceSpecialCharacters_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_3_ReplaceSpecialCharacters_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_4_BracketError_INPUT_1.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_4_BracketError_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_4_BracketError_OUTPUT_1.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_4_BracketError_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_6_DictionaryTest_INPUT1.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_6_DictionaryTest_INPUT2.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_6_DictionaryTest_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_6_DictionaryTest_OUTPUT1.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_6_DictionaryTest_OUTPUT2.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_6_DictionaryTest_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_7_DuplictedClasses_INPUT1.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_7_DuplictedClasses_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_7_DuplictedClasses_OUTPUT1.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_7_DuplictedClasses_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_8_LargeArrayOfObjects_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_8_LargeArrayOfObjects_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_9_HANDLE_NUMBERS_INPUT1.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_9_HANDLE_NUMBERS_INPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_9_HANDLE_NUMBERS_OUTPUT1.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Test_9_HANDLE_NUMBERS_OUTPUT.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,34 @@
using System.Text.RegularExpressions;
namespace Json2CSharpCodeGenerator.Tests;
public static class TestUtility
{
private static readonly Regex _SpaceRegex = new("( +)", RegexOptions.Compiled);
public static string NormalizeOutput(this string testData)
{
string testDataCleaned = testData
.Replace("\r", "")
.Replace("\n", " ")
// .Replace(" ", " ")
.Replace("\t", " ")
#if DEBUG
.Replace(",", ",\r\n") // Parameters/arguments
.Replace("{", "\r\n{\r\n")
.Replace("}", "\r\n}\r\n")
.Replace(";", ";\r\n")
.Replace("\r\n\r\n", "\r\n")
.Replace("\r\n\r\n", "\r\n")
#endif
;
testDataCleaned = _SpaceRegex.Replace(input: testDataCleaned, m => " "); // i.e.: replace runs of 2-or-more space characters (0x20) with a single space character.
testDataCleaned = testDataCleaned.Trim();
return "\r\n" + testDataCleaned + "\r\n"; // The outer line-breaks are added because VS's Test Explorer's Actual vs. Expected comparison is harder to read without them.
}
}

View File

@ -0,0 +1,39 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_0_DIAGNOSTICS
{
[TestMethod]
public void Run()
{
// Assert.Inconclusive(message: "This test is not yet implemented.");
string path = Path.Combine(AppContext.BaseDirectory, "Test_0_DIAGNOSTICS_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_0_DIAGNOSTICS_OUTPUT.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter,
AttributeLibrary = JsonLibrary.SystemTextJson
};
string returnVal = jsonClassGenerator.GenerateClasses(input, errorMessage: out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
string expected = resultsCompare.NormalizeOutput();
string actual = returnVal.NormalizeOutput();
if (expected != actual)
{
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "Test_0_DIAGNOSTICS_OUTPUT.actual.txt"), returnVal);
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "Test_0_DIAGNOSTICS_OUTPUT.expected.txt"), resultsCompare);
}
Assert.AreEqual(expected, actual);
}
}

View File

@ -0,0 +1,197 @@
 [{
"id": 197149482,
"node_id": "MDEwOlJlcG9zaXRvcnkxOTcxNDk0ODI=",
"name": "FlexSouko",
"full_name": "e-wit/FlexSouko",
"private": true,
"owner": {
"login": "e-wit",
"id": 52725066,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjUyNzI1MDY2",
"avatar_url": "https://avatars3.githubusercontent.com/u/52725066?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/e-wit",
"html_url": "https://github.com/e-wit",
"followers_url": "https://api.github.com/users/e-wit/followers",
"following_url": "https://api.github.com/users/e-wit/following{/other_user}",
"gists_url": "https://api.github.com/users/e-wit/gists{/gist_id}",
"starred_url": "https://api.github.com/users/e-wit/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/e-wit/subscriptions",
"organizations_url": "https://api.github.com/users/e-wit/orgs",
"repos_url": "https://api.github.com/users/e-wit/repos",
"events_url": "https://api.github.com/users/e-wit/events{/privacy}",
"received_events_url": "https://api.github.com/users/e-wit/received_events",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/e-wit/FlexSouko",
"description": "フレックス様向け倉庫システム",
"fork": false,
"url": "https://api.github.com/repos/e-wit/FlexSouko",
"forks_url": "https://api.github.com/repos/e-wit/FlexSouko/forks",
"keys_url": "https://api.github.com/repos/e-wit/FlexSouko/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/e-wit/FlexSouko/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/e-wit/FlexSouko/teams",
"hooks_url": "https://api.github.com/repos/e-wit/FlexSouko/hooks",
"issue_events_url": "https://api.github.com/repos/e-wit/FlexSouko/issues/events{/number}",
"events_url": "https://api.github.com/repos/e-wit/FlexSouko/events",
"assignees_url": "https://api.github.com/repos/e-wit/FlexSouko/assignees{/user}",
"branches_url": "https://api.github.com/repos/e-wit/FlexSouko/branches{/branch}",
"tags_url": "https://api.github.com/repos/e-wit/FlexSouko/tags",
"blobs_url": "https://api.github.com/repos/e-wit/FlexSouko/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/e-wit/FlexSouko/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/e-wit/FlexSouko/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/e-wit/FlexSouko/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/e-wit/FlexSouko/statuses/{sha}",
"languages_url": "https://api.github.com/repos/e-wit/FlexSouko/languages",
"stargazers_url": "https://api.github.com/repos/e-wit/FlexSouko/stargazers",
"contributors_url": "https://api.github.com/repos/e-wit/FlexSouko/contributors",
"subscribers_url": "https://api.github.com/repos/e-wit/FlexSouko/subscribers",
"subscription_url": "https://api.github.com/repos/e-wit/FlexSouko/subscription",
"commits_url": "https://api.github.com/repos/e-wit/FlexSouko/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/e-wit/FlexSouko/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/e-wit/FlexSouko/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/e-wit/FlexSouko/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/e-wit/FlexSouko/contents/{+path}",
"compare_url": "https://api.github.com/repos/e-wit/FlexSouko/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/e-wit/FlexSouko/merges",
"archive_url": "https://api.github.com/repos/e-wit/FlexSouko/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/e-wit/FlexSouko/downloads",
"issues_url": "https://api.github.com/repos/e-wit/FlexSouko/issues{/number}",
"pulls_url": "https://api.github.com/repos/e-wit/FlexSouko/pulls{/number}",
"milestones_url": "https://api.github.com/repos/e-wit/FlexSouko/milestones{/number}",
"notifications_url": "https://api.github.com/repos/e-wit/FlexSouko/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/e-wit/FlexSouko/labels{/name}",
"releases_url": "https://api.github.com/repos/e-wit/FlexSouko/releases{/id}",
"deployments_url": "https://api.github.com/repos/e-wit/FlexSouko/deployments",
"created_at": "2019-07-16T08:07:21Z",
"updated_at": "2020-01-31T01:38:07Z",
"pushed_at": "2019-07-17T01:00:19Z",
"git_url": "git://github.com/e-wit/FlexSouko.git",
"ssh_url": "git@github.com:e-wit/FlexSouko.git",
"clone_url": "https://github.com/e-wit/FlexSouko.git",
"svn_url": "https://github.com/e-wit/FlexSouko",
"homepage": null,
"size": 8999,
"stargazers_count": 0,
"watchers_count": 0,
"language": "Visual Basic",
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"forks_count": 0,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 0,
"license": null,
"forks": 0,
"open_issues": 0,
"watchers": 0,
"default_branch": "master",
"permissions": {
"admin": true,
"push": true,
"pull": true
}
},{
"id": 197149482,
"node_id": "MDEwOlJlcG9zaXRvcnkxOTcxNDk0ODI=",
"name": "FlexSouko",
"full_name": "e-wit/FlexSouko",
"private": true,
"owner": {
"login": "e-wit",
"id": 52725066,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjUyNzI1MDY2",
"avatar_url": "https://avatars3.githubusercontent.com/u/52725066?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/e-wit",
"html_url": "https://github.com/e-wit",
"followers_url": "https://api.github.com/users/e-wit/followers",
"following_url": "https://api.github.com/users/e-wit/following{/other_user}",
"gists_url": "https://api.github.com/users/e-wit/gists{/gist_id}",
"starred_url": "https://api.github.com/users/e-wit/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/e-wit/subscriptions",
"organizations_url": "https://api.github.com/users/e-wit/orgs",
"repos_url": "https://api.github.com/users/e-wit/repos",
"events_url": "https://api.github.com/users/e-wit/events{/privacy}",
"received_events_url": "https://api.github.com/users/e-wit/received_events",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/e-wit/FlexSouko",
"description": "フレックス様向け倉庫システム",
"fork": false,
"url": "https://api.github.com/repos/e-wit/FlexSouko",
"forks_url": "https://api.github.com/repos/e-wit/FlexSouko/forks",
"keys_url": "https://api.github.com/repos/e-wit/FlexSouko/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/e-wit/FlexSouko/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/e-wit/FlexSouko/teams",
"hooks_url": "https://api.github.com/repos/e-wit/FlexSouko/hooks",
"issue_events_url": "https://api.github.com/repos/e-wit/FlexSouko/issues/events{/number}",
"events_url": "https://api.github.com/repos/e-wit/FlexSouko/events",
"assignees_url": "https://api.github.com/repos/e-wit/FlexSouko/assignees{/user}",
"branches_url": "https://api.github.com/repos/e-wit/FlexSouko/branches{/branch}",
"tags_url": "https://api.github.com/repos/e-wit/FlexSouko/tags",
"blobs_url": "https://api.github.com/repos/e-wit/FlexSouko/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/e-wit/FlexSouko/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/e-wit/FlexSouko/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/e-wit/FlexSouko/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/e-wit/FlexSouko/statuses/{sha}",
"languages_url": "https://api.github.com/repos/e-wit/FlexSouko/languages",
"stargazers_url": "https://api.github.com/repos/e-wit/FlexSouko/stargazers",
"contributors_url": "https://api.github.com/repos/e-wit/FlexSouko/contributors",
"subscribers_url": "https://api.github.com/repos/e-wit/FlexSouko/subscribers",
"subscription_url": "https://api.github.com/repos/e-wit/FlexSouko/subscription",
"commits_url": "https://api.github.com/repos/e-wit/FlexSouko/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/e-wit/FlexSouko/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/e-wit/FlexSouko/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/e-wit/FlexSouko/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/e-wit/FlexSouko/contents/{+path}",
"compare_url": "https://api.github.com/repos/e-wit/FlexSouko/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/e-wit/FlexSouko/merges",
"archive_url": "https://api.github.com/repos/e-wit/FlexSouko/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/e-wit/FlexSouko/downloads",
"issues_url": "https://api.github.com/repos/e-wit/FlexSouko/issues{/number}",
"pulls_url": "https://api.github.com/repos/e-wit/FlexSouko/pulls{/number}",
"milestones_url": "https://api.github.com/repos/e-wit/FlexSouko/milestones{/number}",
"notifications_url": "https://api.github.com/repos/e-wit/FlexSouko/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/e-wit/FlexSouko/labels{/name}",
"releases_url": "https://api.github.com/repos/e-wit/FlexSouko/releases{/id}",
"deployments_url": "https://api.github.com/repos/e-wit/FlexSouko/deployments",
"created_at": "2019-07-16T08:07:21Z",
"updated_at": "2020-01-31T01:38:07Z",
"pushed_at": "2019-07-17T01:00:19Z",
"git_url": "git://github.com/e-wit/FlexSouko.git",
"ssh_url": "git@github.com:e-wit/FlexSouko.git",
"clone_url": "https://github.com/e-wit/FlexSouko.git",
"svn_url": "https://github.com/e-wit/FlexSouko",
"homepage": null,
"size": 8999,
"stargazers_count": 0,
"watchers_count": 0,
"language": "Visual Basic",
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"forks_count": 0,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 0,
"license": null,
"forks": 0,
"open_issues": 0,
"watchers": 0,
"default_branch": "master",
"permissions": {
"admin": true,
"push": true,
"pull": true
}
}]

View File

@ -0,0 +1,108 @@
// Root myDeserializedClass = JsonSerializer.Deserialize<List<Root>>(myJsonResponse);
public class Owner
{
public string login { get; set; }
public int id { get; set; }
public string node_id { get; set; }
public string avatar_url { get; set; }
public string gravatar_id { get; set; }
public string url { get; set; }
public string html_url { get; set; }
public string followers_url { get; set; }
public string following_url { get; set; }
public string gists_url { get; set; }
public string starred_url { get; set; }
public string subscriptions_url { get; set; }
public string organizations_url { get; set; }
public string repos_url { get; set; }
public string events_url { get; set; }
public string received_events_url { get; set; }
public string type { get; set; }
public bool site_admin { get; set; }
}
public class Permissions
{
public bool admin { get; set; }
public bool push { get; set; }
public bool pull { get; set; }
}
public class Root
{
public int id { get; set; }
public string node_id { get; set; }
public string name { get; set; }
public string full_name { get; set; }
public bool @private { get; set; }
public Owner owner { get; set; }
public string html_url { get; set; }
public string description { get; set; }
public bool fork { get; set; }
public string url { get; set; }
public string forks_url { get; set; }
public string keys_url { get; set; }
public string collaborators_url { get; set; }
public string teams_url { get; set; }
public string hooks_url { get; set; }
public string issue_events_url { get; set; }
public string events_url { get; set; }
public string assignees_url { get; set; }
public string branches_url { get; set; }
public string tags_url { get; set; }
public string blobs_url { get; set; }
public string git_tags_url { get; set; }
public string git_refs_url { get; set; }
public string trees_url { get; set; }
public string statuses_url { get; set; }
public string languages_url { get; set; }
public string stargazers_url { get; set; }
public string contributors_url { get; set; }
public string subscribers_url { get; set; }
public string subscription_url { get; set; }
public string commits_url { get; set; }
public string git_commits_url { get; set; }
public string comments_url { get; set; }
public string issue_comment_url { get; set; }
public string contents_url { get; set; }
public string compare_url { get; set; }
public string merges_url { get; set; }
public string archive_url { get; set; }
public string downloads_url { get; set; }
public string issues_url { get; set; }
public string pulls_url { get; set; }
public string milestones_url { get; set; }
public string notifications_url { get; set; }
public string labels_url { get; set; }
public string releases_url { get; set; }
public string deployments_url { get; set; }
public DateTime created_at { get; set; }
public DateTime updated_at { get; set; }
public DateTime pushed_at { get; set; }
public string git_url { get; set; }
public string ssh_url { get; set; }
public string clone_url { get; set; }
public string svn_url { get; set; }
public object homepage { get; set; }
public int size { get; set; }
public int stargazers_count { get; set; }
public int watchers_count { get; set; }
public string language { get; set; }
public bool has_issues { get; set; }
public bool has_projects { get; set; }
public bool has_downloads { get; set; }
public bool has_wiki { get; set; }
public bool has_pages { get; set; }
public int forks_count { get; set; }
public object mirror_url { get; set; }
public bool archived { get; set; }
public bool disabled { get; set; }
public int open_issues_count { get; set; }
public object license { get; set; }
public int forks { get; set; }
public int open_issues { get; set; }
public int watchers { get; set; }
public string default_branch { get; set; }
public Permissions permissions { get; set; }
}

View File

@ -0,0 +1,39 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_10_SETTINGS_IMMUTABLE_CLASSES
{
[TestMethod]
public void Run()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_10_SETTINGS_IMMUTABLE_CLASSES_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_10_SETTINGS_IMMUTABLE_CLASSES_OUTPUT.txt");
string input = File.ReadAllText(path);
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = new CSharpCodeWriter(),
OutputType = OutputTypes.ImmutableClass,
UseThisKeyWord = true,
AttributeLibrary = JsonLibrary.NewtonsoftJson,
AttributeUsage = JsonPropertyAttributeUsage.Always,
UsePascalCase = true,
};
string returnVal = jsonClassGenerator.GenerateClasses(input, errorMessage: out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
string expected = resultsCompare.NormalizeOutput();
string actual = returnVal.NormalizeOutput();
if (expected != actual)
{
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "Test_10_SETTINGS_IMMUTABLE_CLASSES_OUTPUT.actual.txt"), returnVal);
File.WriteAllText(Path.Combine(AppContext.BaseDirectory, "Test_10_SETTINGS_IMMUTABLE_CLASSES_OUTPUT.expected.txt"), resultsCompare);
}
Assert.AreEqual(expected, actual);
}
}

View File

@ -0,0 +1,30 @@
{
"Class1":{
"id":4,
"user_id":"user_id_value",
"awesomeobject": {"SomeProps1" : 1, "SomeProps2" : "test"},
"created_at":"2015-06-02 23:33:90",
"updated_at":"2015-06-02 23:33:90",
"users":[
{
"id":"6",
"name":"Test Child 1",
"created_at":"2015-06-02 23:33:90",
"updated_at":"2015-06-02 23:33:90",
"email":"test@gmail.com"
},
{
"id":"6",
"name":"Test Child 1",
"created_at":"2015-06-02 23:33:90",
"updated_at":"2015-06-02 23:33:90",
"email":"test@gmail.com",
"testanadditionalfield":"tet"
}
],
},
"Class2" : {
"SomePropertyOfClass2" : "SomeValueOfClass2"
}
}

View File

@ -0,0 +1,134 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Awesomeobject
{
[JsonConstructor]
public Awesomeobject(
[JsonProperty("SomeProps1")] int someProps1,
[JsonProperty("SomeProps2")] string someProps2
)
{
this.SomeProps1 = someProps1;
this.SomeProps2 = someProps2;
}
[JsonProperty("SomeProps1")]
public int SomeProps1 { get; }
[JsonProperty("SomeProps2")]
public string SomeProps2 { get; }
}
public class Class1
{
[JsonConstructor]
public Class1(
[JsonProperty("id")] int id,
[JsonProperty("user_id")] string userId,
[JsonProperty("awesomeobject")] Awesomeobject awesomeobject,
[JsonProperty("created_at")] string createdAt,
[JsonProperty("updated_at")] string updatedAt,
[JsonProperty("users")] List<User> users
)
{
this.Id = id;
this.UserId = userId;
this.Awesomeobject = awesomeobject;
this.CreatedAt = createdAt;
this.UpdatedAt = updatedAt;
this.Users = users;
}
[JsonProperty("id")]
public int Id { get; }
[JsonProperty("user_id")]
public string UserId { get; }
[JsonProperty("awesomeobject")]
public Awesomeobject Awesomeobject { get; }
[JsonProperty("created_at")]
public string CreatedAt { get; }
[JsonProperty("updated_at")]
public string UpdatedAt { get; }
[JsonProperty("users")]
public IReadOnlyList<User> Users { get; }
}
public class Class2
{
[JsonConstructor]
public Class2(
[JsonProperty("SomePropertyOfClass2")] string somePropertyOfClass2
)
{
this.SomePropertyOfClass2 = somePropertyOfClass2;
}
[JsonProperty("SomePropertyOfClass2")]
public string SomePropertyOfClass2 { get; }
}
public class Root
{
[JsonConstructor]
public Root(
[JsonProperty("Class1")] Class1 class1,
[JsonProperty("Class2")] Class2 class2
)
{
this.Class1 = class1;
this.Class2 = class2;
}
[JsonProperty("Class1")]
public Class1 Class1 { get; }
[JsonProperty("Class2")]
public Class2 Class2 { get; }
}
public class User
{
[JsonConstructor]
public User(
[JsonProperty("id")] string id,
[JsonProperty("name")] string name,
[JsonProperty("created_at")] string createdAt,
[JsonProperty("updated_at")] string updatedAt,
[JsonProperty("email")] string email,
[JsonProperty("testanadditionalfield")] string testanadditionalfield
)
{
this.Id = id;
this.Name = name;
this.CreatedAt = createdAt;
this.UpdatedAt = updatedAt;
this.Email = email;
this.Testanadditionalfield = testanadditionalfield;
}
[JsonProperty("id")]
public string Id { get; }
[JsonProperty("name")]
public string Name { get; }
[JsonProperty("created_at")]
public string CreatedAt { get; }
[JsonProperty("updated_at")]
public string UpdatedAt { get; }
[JsonProperty("email")]
public string Email { get; }
[JsonProperty("testanadditionalfield")]
public string Testanadditionalfield { get; }
}

View File

@ -0,0 +1,65 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_11_NoListSetter
{
[TestMethod]
public void Run()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_11_NoListSetter_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_11_NoListSetter_OUTPUT.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
MutableClasses =
{
ReadOnlyCollectionProperties = true
},
AttributeLibrary = JsonLibrary.SystemTextJson,
AttributeUsage = JsonPropertyAttributeUsage.Always,
UsePascalCase = true
};
jsonClassGenerator.CodeWriter = csharpCodeWriter;
string returnVal = jsonClassGenerator.GenerateClasses(input, out string errorMessage).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(expected: resultsCompare.NormalizeOutput(), actual: returnVal.NormalizeOutput());
Assert.AreEqual(expected: string.Empty, errorMessage);
}
[TestMethod]
public void Run2()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_11_NoListSetter_INPUT1.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_11_NoListSetter_OUTPUT1.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
MutableClasses =
{
ReadOnlyCollectionProperties = true
},
CollectionType = OutputCollectionType.Array,
AttributeLibrary = JsonLibrary.SystemTextJson,
AttributeUsage = JsonPropertyAttributeUsage.Always,
UsePascalCase = true
};
jsonClassGenerator.CodeWriter = csharpCodeWriter;
string returnVal = jsonClassGenerator.GenerateClasses(input, out string errorMessage).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(expected: resultsCompare.NormalizeOutput(), actual: returnVal.NormalizeOutput());
Assert.AreEqual(string.Empty, errorMessage);
}
}

View File

@ -0,0 +1,13 @@
{
"Id": 123456123456789,
"Data": [
{
"Name": "Car",
"Year": 2000
},
{
"Name": "Motorbike",
"Year": 2010
}
]
}

View File

@ -0,0 +1,13 @@
{
"Id": 123456123456789,
"Data": [
{
"Name": "Car",
"Year": 2000
},
{
"Name": "Motorbike",
"Year": 2010
}
]
}

View File

@ -0,0 +1,18 @@
// Root myDeserializedClass = JsonSerializer.Deserialize<Root>(myJsonResponse);
public class Datum
{
[JsonPropertyName("Name")]
public string Name { get; set; }
[JsonPropertyName("Year")]
public int Year { get; set; }
}
public class Root
{
[JsonPropertyName("Id")]
public long Id { get; set; }
[JsonPropertyName("Data")]
public List<Datum> Data { get; } = new List<Datum>();
}

View File

@ -0,0 +1,18 @@
// Root myDeserializedClass = JsonSerializer.Deserialize<Root>(myJsonResponse);
public class Datum
{
[JsonPropertyName("Name")]
public string Name { get; set; }
[JsonPropertyName("Year")]
public int Year { get; set; }
}
public class Root
{
[JsonPropertyName("Id")]
public long Id { get; set; }
[JsonPropertyName("Data")]
public Datum[] Data { get; set; }
}

View File

@ -0,0 +1,58 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_17_RECORD_TYPES
{
[TestMethod]
public void Run() => DoTest(null);
[TestMethod]
public void RunNewtonsoft() => DoTest(false, "_NEWTONSOFT");
[TestMethod]
public void RunSystemTextJson() => DoTest(true, "_SYSTEMTEXTJSON");
private static void DoTest(bool? useSystemTextJson, string outputFileSuffix = "")
{
string inputPath = Path.Combine(AppContext.BaseDirectory, "Test_17_RECORD_TYPES_INPUT.txt");
string outputPath = Path.Combine(AppContext.BaseDirectory, $"Test_17_RECORD_TYPES_OUTPUT{outputFileSuffix}.txt");
string input = File.ReadAllText(inputPath);
string output = File.ReadAllText(outputPath);
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = new CSharpCodeWriter(),
OutputType = OutputTypes.ImmutableRecord
};
if (useSystemTextJson.HasValue)
{
jsonClassGenerator.AttributeUsage = JsonPropertyAttributeUsage.Always;
if (useSystemTextJson.Value)
{
jsonClassGenerator.AttributeLibrary = JsonLibrary.SystemTextJson;
}
else
{
jsonClassGenerator.AttributeLibrary = JsonLibrary.NewtonsoftJson;
}
}
else
{
jsonClassGenerator.AttributeUsage = JsonPropertyAttributeUsage.OnlyWhenNecessary;
}
string actual = jsonClassGenerator.GenerateClasses(input, out _).ToString();
// Console.WriteLine("Actual:\n" + actual);
string outputFixed = output.NormalizeOutput();
string actualFixed = actual.NormalizeOutput();
Assert.AreEqual(expected: outputFixed, actual: actualFixed);
}
}

View File

@ -0,0 +1,13 @@
{
"key": "my_key",
"values": [
{
"id": 123,
"real": true,
},
{
"id": 456,
"real": false
}
]
}

View File

@ -0,0 +1,12 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public record Root(
string key,
IReadOnlyList<Value> values
);
public record Value(
int id,
bool real
);

View File

@ -0,0 +1,11 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public record Root(
[property: JsonProperty("key")] string Key,
[property: JsonProperty("values")] IReadOnlyList<Value> Values
);
public record Value(
[property: JsonProperty("id")] int Id,
[property: JsonProperty("real")] bool Real
);

View File

@ -0,0 +1,11 @@
// Root myDeserializedClass = JsonSerializer.Deserialize<Root>(myJsonResponse);
public record Root(
[property: JsonPropertyName("key")] string Key,
[property: JsonPropertyName("values")] IReadOnlyList<Value> Values
);
public record Value(
[property: JsonPropertyName("id")] int Id,
[property: JsonPropertyName("real")] bool Real
);

View File

@ -0,0 +1,27 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_18_WRONG_NAME_RECURSIVE_BUG78
{
[TestMethod]
public void Run()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_18_WRONG_NAME_RECURSIVE_BUG78_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_18_WRONG_NAME_RECURSIVE_BUG78_OUTPUT.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\t", ""), returnVal.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\t", ""));
}
}

View File

@ -0,0 +1,22 @@
{
"number": 1,
"elements": [{
"name": "node1",
"attrs": {
"id": "1"
},
"elements": [{
"name": "node2",
"attrs": {
"id": "2"
},
"elements": [{
"name": "node3",
"attrs": {
"id": "3"
}
}]
}
]
}]
}

View File

@ -0,0 +1,19 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Attrs
{
public string id { get; set; }
}
public class Element
{
public string name { get; set; }
public Attrs attrs { get; set; }
public List<Element> elements { get; set; }
}
public class Root
{
public int number { get; set; }
public List<Element> elements { get; set; }
}

View File

@ -0,0 +1,25 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_19_PLURAL_NAMES
{
[TestMethod]
public void Run()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_19_PLURAL_NAMES_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_19_PLURAL_NAMES_OUTPUT.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\t", ""), returnVal.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\t", ""));
}
}

View File

@ -0,0 +1,10 @@
{
"timeWindowDetails": {
"openCloses": [
{
"startTimeOfDay": "",
"endTimeOfDay": ""
}
]
}
}

View File

@ -0,0 +1,17 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class OpenClose
{
public string startTimeOfDay { get; set; }
public string endTimeOfDay { get; set; }
}
public class Root
{
public TimeWindowDetails timeWindowDetails { get; set; }
}
public class TimeWindowDetails
{
public List<OpenClose> openCloses { get; set; }
}

View File

@ -0,0 +1,28 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_1_2_SETTINGS_PASCAL
{
[TestMethod]
public void Run()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_1_2_SETTINGS_PASCAL_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_1_2_SETTINGS_PASCAL_OUTPUT.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter,
UsePascalCase = true
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(expected: resultsCompare.NormalizeOutput(), actual: returnVal.NormalizeOutput());
}
}

View File

@ -0,0 +1,9 @@
{
"test": "asdfasjdfajjfasldf",
"test-test" : "test",
"continue" : "test",
"class" : "test"
}

View File

@ -0,0 +1,9 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Root {
public string Test { get; set; }
[JsonProperty("test-test")]
public string TestTest { get; set; }
public string Continue { get; set; }
public string Class { get; set; }
}

View File

@ -0,0 +1,28 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_1_3_SETTINGS_FIELDS
{
[TestMethod]
public void Run()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_1_3_SETTINGS_FIELDS_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_1_3_SETTINGS_FIELDS_OUTPUT.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter
};
jsonClassGenerator.MutableClasses.Members = OutputMembers.AsPublicFields;
string returnVal = jsonClassGenerator.GenerateClasses(input, out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
}
}

View File

@ -0,0 +1,9 @@
{
"test": "asdfasjdfajjfasldf",
"test-test" : "test",
"continue" : "test",
"class" : "test"
}

View File

@ -0,0 +1,9 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Root {
public string test;
[JsonProperty("test-test")]
public string TestTest;
public string @continue;
public string @class;
}

View File

@ -0,0 +1,30 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_1_4_SETTINGS_JSONPROPERTY
{
[TestMethod]
public void Run()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_1_4_SETTINGS_JSONPROPERTY_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_1_4_SETTINGS_JSONPROPERTY_OUTPUT.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter,
AttributeLibrary = JsonLibrary.NewtonsoftJson,
AttributeUsage = JsonPropertyAttributeUsage.Always
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(expected: resultsCompare.NormalizeOutput(), actual: returnVal.NormalizeOutput());
}
}

View File

@ -0,0 +1,9 @@
{
"test": "asdfasjdfajjfasldf",
"test-test" : "test",
"continue" : "test",
"class" : "test"
}

View File

@ -0,0 +1,15 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Root {
[JsonProperty("test")]
public string Test { get; set; }
[JsonProperty("test-test")]
public string TestTest { get; set; }
[JsonProperty("continue")]
public string Continue { get; set; }
[JsonProperty("class")]
public string Class { get; set; }
}

View File

@ -0,0 +1,33 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_1_5_SETTINGS_FIELDS_JSONPROPERTY
{
[TestMethod]
public void Run()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_1_5_SETTINGS_FIELDS_JSONPROPERTY_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_1_5_SETTINGS_FIELDS_JSONPROPERTY_OUTPUT.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter,
OutputType = OutputTypes.MutableClass
};
jsonClassGenerator.MutableClasses.Members = OutputMembers.AsPublicFields;
jsonClassGenerator.AttributeLibrary = JsonLibrary.NewtonsoftJson;
jsonClassGenerator.AttributeUsage = JsonPropertyAttributeUsage.Always;
string returnVal = jsonClassGenerator.GenerateClasses(input, out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
}
}

View File

@ -0,0 +1,9 @@
{
"test": "asdfasjdfajjfasldf",
"test-test" : "test",
"continue" : "test",
"class" : "test"
}

View File

@ -0,0 +1,15 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Root {
[JsonProperty("test")]
public string Test;
[JsonProperty("test-test")]
public string TestTest;
[JsonProperty("continue")]
public string Continue;
[JsonProperty("class")]
public string Class;
}

View File

@ -0,0 +1,29 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_1_6_SETTINGS_JSONPROPERTYNAME_NETCORE
{
[TestMethod]
public void Run()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_1_6_SETTINGS_JSONPROPERTYNAME_NETCORE_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_1_6_SETTINGS_JSONPROPERTYNAME_NETCORE_OUTPUT.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter,
AttributeLibrary = JsonLibrary.SystemTextJson,
AttributeUsage = JsonPropertyAttributeUsage.Always
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
}
}

View File

@ -0,0 +1,30 @@
{
"Class1":{
"id":4,
"user_id":"user_id_value",
"awesomeobject": {"SomeProps1" : 1, "SomeProps2" : "test"},
"created_at":"2015-06-02 23:33:90",
"updated_at":"2015-06-02 23:33:90",
"users":[
{
"id":"6",
"name":"Test Child 1",
"created_at":"2015-06-02 23:33:90",
"updated_at":"2015-06-02 23:33:90",
"email":"test@gmail.com"
},
{
"id":"6",
"name":"Test Child 1",
"created_at":"2015-06-02 23:33:90",
"updated_at":"2015-06-02 23:33:90",
"email":"test@gmail.com",
"testanadditionalfield":"tet"
}
],
},
"Class2" : {
"SomePropertyOfClass2" : "SomeValueOfClass2"
}
}

View File

@ -0,0 +1,64 @@
// Root myDeserializedClass = JsonSerializer.Deserialize<Root>(myJsonResponse);
public class Awesomeobject {
[JsonPropertyName("SomeProps1")]
public int SomeProps1 { get; set; }
[JsonPropertyName("SomeProps2")]
public string SomeProps2 { get; set; }
}
public class Class1 {
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("user_id")]
public string UserId { get; set; }
[JsonPropertyName("awesomeobject")]
public Awesomeobject Awesomeobject { get; set; }
[JsonPropertyName("created_at")]
public string CreatedAt { get; set; }
[JsonPropertyName("updated_at")]
public string UpdatedAt { get; set; }
[JsonPropertyName("users")]
public List<User> Users { get; set; }
}
public class Class2 {
[JsonPropertyName("SomePropertyOfClass2")]
public string SomePropertyOfClass2 { get; set; }
}
public class Root {
[JsonPropertyName("Class1")]
public Class1 Class1 { get; set; }
[JsonPropertyName("Class2")]
public Class2 Class2 { get; set; }
}
public class User {
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("created_at")]
public string CreatedAt { get; set; }
[JsonPropertyName("updated_at")]
public string UpdatedAt { get; set; }
[JsonPropertyName("email")]
public string Email { get; set; }
[JsonPropertyName("testanadditionalfield")]
public string Testanadditionalfield { get; set; }
}

View File

@ -0,0 +1,33 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_1_SETTINGS
{
[TestMethod]
public void Run()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_1_SETTINGS_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_1_SETTINGS_OUTPUT.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter,
UsePascalCase = true,
AttributeLibrary = JsonLibrary.NewtonsoftJson,
AttributeUsage = JsonPropertyAttributeUsage.Always
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
}
}

View File

@ -0,0 +1,9 @@
{
"test": "asdfasjdfajjfasldf",
"test-test" : "test",
"continue" : "test",
"class" : "test"
}

View File

@ -0,0 +1,15 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Root {
[JsonProperty("test")]
public string Test { get; set; }
[JsonProperty("test-test")]
public string TestTest { get; set; }
[JsonProperty("continue")]
public string Continue { get; set; }
[JsonProperty("class")]
public string Class { get; set; }
}

View File

@ -0,0 +1,25 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_20_ROOT_ARRAY
{
[TestMethod]
public void Run()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_20_ROOT_ARRAY_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_20_ROOT_ARRAY_OUTPUT.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\t", ""), returnVal.Replace(Environment.NewLine, "").Replace(" ", "").Replace("\t", ""));
}
}

View File

@ -0,0 +1,10 @@
[
{
"ID":"{7D7891AF-69E0-4542-BE8A-0B1FE073B211}",
"Description":""
},
{
"ID":"{7D7891AF-69E0-4542-BE8A-0B1FE073B211}",
"Description":""
}
]

View File

@ -0,0 +1,6 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<List<Root>>(myJsonResponse);
public class Root
{
public string ID { get; set; }
public string Description { get; set; }
}

View File

@ -0,0 +1,27 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_2_CHECK_RESERVED_KEYWORDS
{
[TestMethod]
public void Run()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_2_CHECK_RESERVED_KEYWORDS_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_2_CHECK_RESERVED_KEYWORDS_OUTPUT.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
}
}

View File

@ -0,0 +1,79 @@
{
"abstract": -1,
"as": -1,
"base": -1,
"bool": -1,
"break": -1,
"byte": -1,
"case": -1,
"catch": -1,
"char": -1,
"checked": -1,
"class": -1,
"const": -1,
"continue": -1,
"decimal": -1,
"default": -1,
"delegate": -1,
"do": -1,
"double": -1,
"else": -1,
"enum": -1,
"event": -1,
"explicit": -1,
"extern": -1,
"false": -1,
"finally": -1,
"fixed": -1,
"float": -1,
"for": -1,
"foreach": -1,
"goto": -1,
"if": -1,
"implicit": -1,
"in": -1,
"int": -1,
"interface": -1,
"internal": -1,
"is": -1,
"lock": -1,
"long": -1,
"namespace": -1,
"new": -1,
"null": -1,
"object": -1,
"operator": -1,
"out": -1,
"override": -1,
"params": -1,
"private": -1,
"protected": -1,
"public": -1,
"readonly": -1,
"ref": -1,
"return": -1,
"sbyte": -1,
"sealed": -1,
"short": -1,
"sizeof": -1,
"stackalloc": -1,
"static": -1,
"string": -1,
"struct": -1,
"switch": -1,
"this": -1,
"throw": -1,
"true": -1,
"try": -1,
"typeof": -1,
"uint": -1,
"ulong": -1,
"unchecked": -1,
"unsafe": -1,
"ushort": -1,
"using": -1,
"virtual": -1,
"void": -1,
"volatile": -1,
"while": -1
}

View File

@ -0,0 +1,81 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Root {
public int @abstract { get; set; }
public int @as { get; set; }
public int @base { get; set; }
public int @bool { get; set; }
public int @break { get; set; }
public int @byte { get; set; }
public int @case { get; set; }
public int @catch { get; set; }
public int @char { get; set; }
public int @checked { get; set; }
public int @class { get; set; }
public int @const { get; set; }
public int @continue { get; set; }
public int @decimal { get; set; }
public int @default { get; set; }
public int @delegate { get; set; }
public int @do { get; set; }
public int @double { get; set; }
public int @else { get; set; }
public int @enum { get; set; }
public int @event { get; set; }
public int @explicit { get; set; }
public int @extern { get; set; }
public int @false { get; set; }
public int @finally { get; set; }
public int @fixed { get; set; }
public int @float { get; set; }
public int @for { get; set; }
public int @foreach { get; set; }
public int @goto { get; set; }
public int @if { get; set; }
public int @implicit { get; set; }
public int @in { get; set; }
public int @int { get; set; }
public int @interface { get; set; }
public int @internal { get; set; }
public int @is { get; set; }
public int @lock { get; set; }
public int @long { get; set; }
public int @namespace { get; set; }
public int @new { get; set; }
public int @null { get; set; }
public int @object { get; set; }
public int @operator { get; set; }
public int @out { get; set; }
public int @override { get; set; }
public int @params { get; set; }
public int @private { get; set; }
public int @protected { get; set; }
public int @public { get; set; }
public int @readonly { get; set; }
public int @ref { get; set; }
public int @return { get; set; }
public int @sbyte { get; set; }
public int @sealed { get; set; }
public int @short { get; set; }
public int @sizeof { get; set; }
public int @stackalloc { get; set; }
public int @static { get; set; }
public int @string { get; set; }
public int @struct { get; set; }
public int @switch { get; set; }
public int @this { get; set; }
public int @throw { get; set; }
public int @true { get; set; }
public int @try { get; set; }
public int @typeof { get; set; }
public int @uint { get; set; }
public int @ulong { get; set; }
public int @unchecked { get; set; }
public int @unsafe { get; set; }
public int @ushort { get; set; }
public int @using { get; set; }
public int @virtual { get; set; }
public int @void { get; set; }
public int @volatile { get; set; }
public int @while { get; set; }
}

View File

@ -0,0 +1,28 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_3_ReplaceSpecialCharacters
{
[TestMethod]
public void Run()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_3_ReplaceSpecialCharacters_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_3_ReplaceSpecialCharacters_OUTPUT.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter,
AttributeLibrary = JsonLibrary.NewtonsoftJson
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
}
}

View File

@ -0,0 +1 @@
{"Test-tes+-t": 123}

View File

@ -0,0 +1,6 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Root {
[JsonProperty("Test-tes+-t")]
public int TestTesT { get; set; }
}

View File

@ -0,0 +1,46 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_4_BracketError
{
[TestMethod]
public void Run_1()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_4_BracketError_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_4_BracketError_OUTPUT.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
}
[TestMethod]
public void Run_2()
{
string path1 = Path.Combine(AppContext.BaseDirectory, "Test_4_BracketError_INPUT_1.txt");
string resultPath1 = Path.Combine(AppContext.BaseDirectory, "Test_4_BracketError_OUTPUT_1.txt");
string input1 = File.ReadAllText(path1);
CSharpCodeWriter csharpCodeWriter1 = new();
JsonClassGenerator jsonClassGenerator1 = new()
{
CodeWriter = csharpCodeWriter1
};
string returnVal1 = jsonClassGenerator1.GenerateClasses(input1, out _).ToString();
string resultsCompare1 = File.ReadAllText(resultPath1);
Assert.AreEqual(resultsCompare1.NormalizeOutput(), returnVal1.NormalizeOutput());
}
}

View File

@ -0,0 +1,17 @@
{
"test":[
{
"Mbo":[
[
160
]
]
},
{
"Mbo":[
]
}
]
}

View File

@ -0,0 +1,17 @@
{
"test":[
{
"Mbo":[
]
},
{
"Mbo":[
[
160
]
]
}
]
}

View File

@ -0,0 +1,11 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Root {
public List<Test> test { get; set; }
}
public class Test {
public List<List<int>> Mbo { get; set; }
}

View File

@ -0,0 +1,11 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Root {
public List<Test> test { get; set; }
}
public class Test {
public List<List<int>> Mbo { get; set; }
}

View File

@ -0,0 +1,23 @@
using Json2CSharpCodeGenerator.Lib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_5_BASIC_SCENARIO
{
[TestMethod]
public void Run()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_5_BASIC_SCENARIO_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_5_BASIC_SCENARIO_OUTPUT.txt");
string input = File.ReadAllText(path);
JsonClassGenerator jsonClassGenerator = new();
string returnVal = jsonClassGenerator.GenerateClasses(input, out string _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
}
}

View File

@ -0,0 +1,30 @@
{
"Class1":{
"id":4,
"user_id":"user_id_value",
"awesomeobject": {"SomeProps1" : 1, "SomeProps2" : "test"},
"created_at":"2015-06-02 23:33:90",
"updated_at":"2015-06-02 23:33:90",
"users":[
{
"id":"6",
"name":"Test Child 1",
"created_at":"2015-06-02 23:33:90",
"updated_at":"2015-06-02 23:33:90",
"email":"test@gmail.com"
},
{
"id":"6",
"name":"Test Child 1",
"created_at":"2015-06-02 23:33:90",
"updated_at":"2015-06-02 23:33:90",
"email":"test@gmail.com",
"testanadditionalfield":"tet"
}
],
},
"Class2" : {
"SomePropertyOfClass2" : "SomeValueOfClass2"
}
}

View File

@ -0,0 +1,36 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Awesomeobject {
public int SomeProps1 { get; set; }
public string SomeProps2 { get; set; }
}
public class Class1 {
public int id { get; set; }
public string user_id { get; set; }
public Awesomeobject awesomeobject { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public List<User> users { get; set; }
}
public class Class2 {
public string SomePropertyOfClass2 { get; set; }
}
public class Root {
public Class1 Class1 { get; set; }
public Class2 Class2 { get; set; }
}
public class User {
public string id { get; set; }
public string name { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public string email { get; set; }
public string testanadditionalfield { get; set; }
}

View File

@ -0,0 +1,51 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_6_DictionaryTest
{
[TestMethod]
public void Run()
{
Assert.Inconclusive(message: "This test is not yet implemented.");
return;
string path = Path.Combine(AppContext.BaseDirectory, "Test_6_DictionaryTest_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_6_DictionaryTest_OUTPUT.txt");
string input = File.ReadAllText(path);
string errorMessage = string.Empty;
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out errorMessage).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
}
[TestMethod]
public void Run2()
{
Assert.Inconclusive(message: "This test is not yet implemented.");
return;
string path = Path.Combine(AppContext.BaseDirectory, "Test_6_DictionaryTest_INPUT2.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_6_DictionaryTest_OUTPUT2.txt");
string input = File.ReadAllText(path);
string errorMessage = string.Empty;
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out errorMessage).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
}
}

View File

@ -0,0 +1,26 @@
{
"TestData" : {
"28-09-2019 08:21:24 PM" : {
"sdata" : {
"testobj1" : "0",
"testobj2" : "57"
},
"testusername" : "testname"
},
"28-09-2019 08:21:25 PM" : {
"sdata" : {
"testobj1" : "0",
"testobj2" : "57"
},
"testusername" : "testname"
}
}
}

View File

@ -0,0 +1,13 @@
{
"m" : [ null, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 58.66117 ],
"w" : [ 0.001, 0.001, 0.001, 58.66117, 0.001, 0.001, 0.001 ],
"y" : {
"2020" : 58.66117,
"2021" : 0.001,
"2022" : 0.001,
"2023" : 0.001,
"2024" : 0.001,
"2025" : 0.001,
"2026" : 0.001
}
}

View File

@ -0,0 +1,36 @@
{
"2103":{
"position":"OT",
"first_name":"Cody",
"player_id":"2103",
"last_name":"Booth",
"team":null,
},
"6250":{
"position":"DT",
"first_name":"Eurndraus",
"player_id":"6250",
"last_name":"Bryant",
"team":null,
}
}
or
{"Test" : {
"2103":{
"position":"OT",
"first_name":"Cody",
"player_id":"2103",
"last_name":"Booth",
"team":null
},
"6250":{
"position":"DT",
"first_name":"Eurndraus",
"player_id":"6250",
"last_name":"Bryant",
"team":null
}
}
}

View File

@ -0,0 +1 @@
dfasdfadfasdf

View File

@ -0,0 +1 @@
dfasdfadfasdf

View File

@ -0,0 +1 @@
dfasdfadfasdf

View File

@ -0,0 +1,48 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_7_DuplicatedClasses
{
[TestMethod]
public void Run_0()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_7_DuplictedClasses_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_7_DuplictedClasses_OUTPUT.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
}
[TestMethod]
public void Run_1()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_7_DuplictedClasses_INPUT1.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_7_DuplictedClasses_OUTPUT1.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter,
UsePascalCase = true,
AttributeLibrary = JsonLibrary.NewtonsoftJson,
AttributeUsage = JsonPropertyAttributeUsage.Always
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
}
}

View File

@ -0,0 +1,26 @@
{
"testobject" : [
{
"test" : 1,
"teststring" : "sdfasdf",
"teststringbla" : {
"testinginner" : 12,
"testingdd" : 12
}
}
]
,
"testparent": {
"testobject" : [
{
"test" : 1,
"teststring" : "sdfasdf"
}
]
}
}

View File

@ -0,0 +1,26 @@
{
"testobject" : [
{
"test" : 1,
"teststring" : "sdfasdf",
"teststringbla" : {
"testinginner" : 12,
"testingdd" : 12
}
}
]
,
"testparent": {
"testobject" : [
{
"test" : 1,
"teststring" : "sdfasdf"
}
]
}
}

View File

@ -0,0 +1,25 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Root {
public List<Testobject> testobject { get; set; }
public Testparent testparent { get; set; }
}
public class Testobject {
public int test { get; set; }
public string teststring { get; set; }
public Teststringbla teststringbla { get; set; }
}
public class Testparent {
public List<Testobject> testobject { get; set; }
}
public class Teststringbla {
public int testinginner { get; set; }
public int testingdd { get; set; }
}

View File

@ -0,0 +1,38 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Root {
[JsonProperty("testobject")]
public List<Testobject> Testobject { get; set; }
[JsonProperty("testparent")]
public Testparent Testparent { get; set; }
}
public class Testobject {
[JsonProperty("test")]
public int Test { get; set; }
[JsonProperty("teststring")]
public string Teststring { get; set; }
[JsonProperty("teststringbla")]
public Teststringbla Teststringbla { get; set; }
}
public class Testparent {
[JsonProperty("testobject")]
public List<Testobject> Testobject { get; set; }
}
public class Teststringbla {
[JsonProperty("testinginner")]
public int Testinginner { get; set; }
[JsonProperty("testingdd")]
public int Testingdd { get; set; }
}

View File

@ -0,0 +1,40 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_8_LargeArrayOfObjects
{
[TestMethod]
public void Run()
{
Assert.Inconclusive(message: "This test is not yet implemented.");
return;
string path = Path.Combine(AppContext.BaseDirectory, "Test_8_LargeArrayOfObjects_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_8_LargeArrayOfObjects_OUTPUT.txt");
string input = File.ReadAllText(path);
string errorMessage = string.Empty;
CSharpCodeWriter csharpCodeWriter = new();
Stopwatch watch = new();
watch.Start();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out errorMessage).ToString();
watch.Stop();
long seconds = watch.ElapsedMilliseconds / 1000;
Assert.IsTrue(false);
string resultsCompare = File.ReadAllText(resultPath);
//Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,12 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Root {
public string test { get; set; }
public string test2 { get; set; }
public string test3 { get; set; }
public string test4 { get; set; }
public string test5 { get; set; }
public string test6 { get; set; }
public string test7 { get; set; }
public string test8 { get; set; }
}

View File

@ -0,0 +1,50 @@
using Json2CSharpCodeGenerator.Lib;
using Json2CSharpCodeGenerator.Lib.CodeWriters;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Json2CSharpCodeGenerator.Tests;
[TestClass]
public class Test_9_HANDLE_NUMBERS
{
[TestMethod]
public void Run()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_9_HANDLE_NUMBERS_INPUT.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_9_HANDLE_NUMBERS_OUTPUT.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
}
[TestMethod]
public void Run2()
{
string path = Path.Combine(AppContext.BaseDirectory, "Test_9_HANDLE_NUMBERS_INPUT1.txt");
string resultPath = Path.Combine(AppContext.BaseDirectory, "Test_9_HANDLE_NUMBERS_OUTPUT1.txt");
string input = File.ReadAllText(path);
CSharpCodeWriter csharpCodeWriter = new();
JsonClassGenerator jsonClassGenerator = new()
{
CodeWriter = csharpCodeWriter,
UsePascalCase = true,
AttributeLibrary = JsonLibrary.NewtonsoftJson,
AttributeUsage = JsonPropertyAttributeUsage.Always
};
string returnVal = jsonClassGenerator.GenerateClasses(input, out _).ToString();
string resultsCompare = File.ReadAllText(resultPath);
Assert.AreEqual(resultsCompare.NormalizeOutput(), returnVal.NormalizeOutput());
}
}

View File

@ -0,0 +1,9 @@
{
"1_TEST" : {
"_10_star": 7,
"2_star": 6,
"3_star": 10,
"4_star": 19,
"5_star": 151
}
}

View File

@ -0,0 +1,10 @@
{
"test_test" : {
"1test_test_test" : 123
,
"test_test_test" : 123
},
"12test_test" : {
}
}

View File

@ -0,0 +1,13 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class _1TEST {
public int _10_star { get; set; }
public int _2_star { get; set; }
public int _3_star { get; set; }
public int _4_star { get; set; }
public int _5_star { get; set; }
}
public class Root {
public _1TEST _1_TEST { get; set; }
}

View File

@ -0,0 +1,21 @@
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class _12testTest {
}
public class Root {
[JsonProperty("test_test")]
public TestTest TestTest { get; set; }
[JsonProperty("12test_test")]
public _12testTest _12testTest { get; set; }
}
public class TestTest {
[JsonProperty("1test_test_test")]
public int _1testTestTest { get; set; }
[JsonProperty("test_test_test")]
public int TestTestTest { get; set; }
}

View File

@ -397,7 +397,7 @@ public partial class MainForm : Form
private void ConfigureGenerator(IJsonClassGeneratorConfig config)
{
config.UsePascalCase = optsPascalCase.Checked;
config.UseThisKeyWord = false;
//
if (optAttribJP.Checked)

View File

@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Json2CSharpCodeGenerator.Li
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Json2CSharpCodeGenerator.WinForms", "Json2CSharpCodeGenerator.WinForms\Json2CSharpCodeGenerator.WinForms.csproj", "{0B91EC07-6FAA-4A51-BECD-8EA156B04976}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Json2CSharpCodeGenerator.Tests", "Json2CSharpCodeGenerator.Tests\Json2CSharpCodeGenerator.Tests.csproj", "{81FCF430-5425-48A6-9BE7-745937D78D97}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -24,5 +26,9 @@ Global
{0B91EC07-6FAA-4A51-BECD-8EA156B04976}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0B91EC07-6FAA-4A51-BECD-8EA156B04976}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0B91EC07-6FAA-4A51-BECD-8EA156B04976}.Release|Any CPU.Build.0 = Release|Any CPU
{81FCF430-5425-48A6-9BE7-745937D78D97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{81FCF430-5425-48A6-9BE7-745937D78D97}.Debug|Any CPU.Build.0 = Debug|Any CPU
{81FCF430-5425-48A6-9BE7-745937D78D97}.Release|Any CPU.ActiveCfg = Release|Any CPU
{81FCF430-5425-48A6-9BE7-745937D78D97}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,57 @@
#Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned -Confirm:$false -Force;
$TestName = Read-Host -Prompt 'Test Name With Number: THE FORMAT IS [TESTNUMBER]_[TESTNAME], EXAMPLE : 10_TESTNAME.'
$Dir = ($psise.CurrentFile.FullPath -replace "CreateTest.ps1", "")
$fullPath = $Dir + "Test_" + $TestName + ".cs"
$fullPathInput = $Dir + "Test_" + $TestName + "_INPUT"+ ".txt"
$fullPathOutput = $Dir + "Test_" + $TestName + "_OUTPUT"+".txt"
New-Item -Path $fullPath -ItemType File
New-Item -Path $fullPathInput -ItemType File
New-Item -Path $fullPathOutput -ItemType File
$testFileContentStart = "using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Xamasoft.JsonClassGenerator;
using Xamasoft.JsonClassGenerator.CodeWriters;
namespace Json2CSharpCodeGenerator.Tests
{
`t[TestClass]";
$testFileClassName = "`r`n`tpublic class Test_{0}" -f $TestName;
$testFileContentStart1 =
"`r`n`t{
`t`t[TestMethod]
`t`tpublic void Run()
`t`t{";
$content1 = "`r`n`t`t`tstring path = Directory.GetCurrentDirectory().Replace(""bin\\Debug"", """") + @`"Test_{0}_INPUT.txt`";" -f $TestName;
$content2 = "`r`n`t`t`tstring resultPath = Directory.GetCurrentDirectory().Replace(""bin\\Debug"", """") + @`"Test_{0}_OUTPUT.txt`";" -f $TestName;
$testFileContentMiddle =
"`r`n`t`t`tstring input = File.ReadAllText(path);
`t`t`tstring errorMessage = string.Empty;
`t`t`tCSharpCodeWriter csharpCodeWriter = new CSharpCodeWriter();
`t`t`tJsonClassGenerator jsonClassGenerator = new JsonClassGenerator();
`t`t`tjsonClassGenerator.CodeWriter = csharpCodeWriter;
`t`t`tstring returnVal = jsonClassGenerator.GenerateClasses(input, out errorMessage).ToString();
`t`t`tstring resultsCompare = File.ReadAllText(resultPath);";
$testFileAssertion = "`r`n`t`t`tAssert.AreEqual(resultsCompare.Replace(Environment.NewLine, `"`").Replace(`" `", `"`").Replace(`"\t`", `"`"), returnVal.Replace(Environment.NewLine, `"`").Replace(`" `", `"`").Replace(`"\t`", `"`"));";
$testFileContentEnd = "
`t`t}
`t}
}";
$tesformattedString = $testFileContentStart + $testFileClassName + $testFileContentStart1 + $content1 + $content2 + $testFileContentMiddle + $testFileAssertion +$testFileContentEnd
Set-Content -Path $fullPath -Value $tesformattedString
Set-Content -Path $fullPathOutput -Value "dfasdfadfasdf" # DO NOT REMOVE : IF THIS IS EMPTY THE TEST WILL SUCCEED, WE WANT TO FAIL INITIALLY
#Set-ExecutionPolicy Restricted -Confirm:$false -Force;