Skip to content

Commit

Permalink
Merge pull request #10 from Dannode36/dev
Browse files Browse the repository at this point in the history
v2.1
  • Loading branch information
Dannode36 authored Oct 17, 2024
2 parents 8b6912b + fc7d13e commit 14daa3d
Show file tree
Hide file tree
Showing 16 changed files with 564 additions and 235 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# json2x
Convert any JSON file into a file of class definitions from a wide range of languages.
Convert JSON into class definitions supporting custom language formats

### Usage
[Getting Started](https://github.com/Dannode36/json2x/wiki/Get-Started)

### Supported Languages
- C++
Expand Down
18 changes: 18 additions & 0 deletions formats/dan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"int32": "int",
"uint32": "uint",
"int64": "int64_t",
"uint64": "uint64_t",
"float32": "float",
"float64": "double",
"bool": "bool",
"string": "string",
"placeholder": "void*",
"var": "{0} {1};",
"array": "std::vector<{}>",
"structStart": "struct {}{{",
"structEnd": "};",
"usingArray": "#include <vector>",
"usingString": "#include <string>",
"extension": "h"
}
18 changes: 18 additions & 0 deletions formats/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"int32": "int",
"uint32": "uint",
"int64": "int64_t",
"uint64": "uint64_t",
"float32": "float",
"float64": "double",
"bool": "bool",
"string": "string",
"placeholder": "void*",
"var": "{0} {1};",
"array": "std::vector<{}>",
"structStart": "struct {} {{",
"structEnd": "};",
"usingArray": "#include <vector>",
"usingString": "#include <string>",
"extension": "h"
}
18 changes: 18 additions & 0 deletions formats/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"int32": "",
"uint32": "",
"int64": "",
"uint64": "",
"float32": "",
"float64": "",
"bool": "",
"string": "",
"placeholder": "",
"var": "",
"array": "",
"structStart": "",
"structEnd": "",
"usingArray": "",
"usingString": "",
"extension": ""
}
14 changes: 13 additions & 1 deletion json2x.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp14</LanguageStandard>
<LanguageStandard_C>Default</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -124,6 +126,8 @@
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<SuppressStartupBanner>true</SuppressStartupBanner>
<LanguageStandard>stdcpp14</LanguageStandard>
<LanguageStandard_C>Default</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -134,15 +138,23 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\LangFormat.cpp" />
<ClCompile Include="src\Options.cpp" />
<ClCompile Include="src\jsonConverters.cpp" />
<ClCompile Include="src\json2x.cpp" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\win32.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\Options.h" />
<ClInclude Include="src\LangFormat.h" />
<ClInclude Include="src\jsonConverters.h" />
<ClInclude Include="src\win32.h" />
</ItemGroup>
<ItemGroup>
<None Include="formats\dan.json" />
<None Include="formats\example.json" />
<None Include="formats\template.json" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
16 changes: 15 additions & 1 deletion json2x.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\json2x.cpp">
<ClCompile Include="src\main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\jsonConverters.cpp">
Expand All @@ -24,6 +24,12 @@
<ClCompile Include="src\win32.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Options.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\LangFormat.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\jsonConverters.h">
Expand All @@ -35,5 +41,13 @@
<ClInclude Include="src\LangFormat.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Options.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="formats\dan.json" />
<None Include="formats\example.json" />
<None Include="formats\template.json" />
</ItemGroup>
</Project>
46 changes: 46 additions & 0 deletions src/LangFormat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <fstream>
#include "LangFormat.h"
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
#include "fmt/format.h"

void LangFormat::parseFormatByName(std::string name)
{
//Load JSON file
std::ifstream formatFile(USER_FORMATS_PATH + name + ".json");

if (!formatFile.is_open()) {
throw std::exception(fmt::format("\"{}\" is not a supported language or could not be found in {}\n", name, USER_FORMATS_PATH).c_str());
}

//Read file contents into a string
std::string json((std::istreambuf_iterator<char>(formatFile)),
std::istreambuf_iterator<char>());

rapidjson::Document doc;
doc.Parse(json.c_str());
if (doc.HasParseError()) {
throw std::exception(
fmt::format("ERROR: User format file: (offset {}}): {}\n",
(unsigned)doc.GetErrorOffset(),
GetParseError_En(doc.GetParseError())).c_str()
);
}

this->int32_t = doc["int32"].GetString();
this->uint32_t = doc["uint32"].GetString();
this->int64_t = doc["int64"].GetString();
this->uint64_t = doc["uint64"].GetString();
this->float_t = doc["float32"].GetString();
this->double_t = doc["float64"].GetString();
this->bool_t = doc["bool"].GetString();
this->string_t = doc["string"].GetString();
this->placeholder_t = doc["placeholder"].GetString();
this->var_format = doc["var"].GetString();
this->array_format = doc["array"].GetString();
this->structS_format = doc["structStart"].GetString();
this->structE_format = doc["structEnd"].GetString();
this->using_array = doc["usingArray"].GetString();
this->using_string = doc["usingString"].GetString();
this->file_extension = doc["extension"].GetString();
}
32 changes: 15 additions & 17 deletions src/LangFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,39 @@
#include <string>
#include <map>

constexpr auto USER_FORMATS_PATH = "formats\\";

struct LangFormat
{
std::string int_t;
std::string int32_t;
std::string uint32_t;
std::string int64_t;
std::string uint64_t;
std::string float_t;
std::string uint_t;
std::string ll_t;
std::string ull_t;
std::string double_t;
std::string string_t;
std::string bool_t;
std::string string_t;
std::string placeholder_t;
std::string var_format;
std::string array_format;
std::string structS_format; //Start of struct declaration
std::string structE_format; //End of struct declaration

std::string using_vector;
std::string using_array;
std::string using_string;

std::wstring file_extension;
std::string file_extension;

LangFormat() = default;

LangFormat(const std::string& int_t, const std::string& float_t, const std::string& uint_t, const std::string& ll_t, const std::string& ull_t, const std::string& double_t, const std::string& string_t, const std::string& bool_t, const std::string& null_t, const std::string& var_format, const std::string& array_format, const std::string& structS_format, const std::string& structE_format, const std::string& using_vector, const std::string& using_string, const std::wstring& file_extension)
: int_t(int_t), float_t(float_t), uint_t(uint_t), ll_t(ll_t), ull_t(ull_t), double_t(double_t), string_t(string_t), bool_t(bool_t), placeholder_t(null_t), var_format(var_format), array_format(array_format), structS_format(structS_format), structE_format(structE_format), using_vector(using_vector), using_string(using_string), file_extension(file_extension)
{
}
void parseFormatByName(std::string name);
};

const std::map<std::string, LangFormat> globalFormats
{
{ "java", { "int", "float", "int", "long", "long", "double", "String", "bool", "Object", "{0} {1};", "ArrayList<{}>", "class {}\n{{\n", "}\n", "", "", L".java" } },
{ "kotlin", { "int", "float", "UInt", "long", "ULong", "double", "String", "bool", "Object", "val {1}: {0};", "List<{}>", "class {}\n{{\n", "}\n", "", "", L".kt" } },
{ "cpp", { "int", "float", "unsigned int", "long long", "unsigned long long", "double", "std::string", "bool", "void*", "{0} {1};", "std::vector<{}>", "struct {} {{\n", "};\n", "#include <vector>\n", "#include <string>\n", L".h" } },
{ "csharp", { "int", "float", "uint", "long", "ulong", "double", "string", "bool", "object", "{0} {1};", "List<{}>", "class {}\n{{\n", "}\n", "using System.Collections.Generic;\n", "", L".cs" } },
{ "rust", { "i32", "f32", "u32", "i64", "u64", "f64", "String", "bool", "", "{1}: {0},", "Vec<{}>", "struct {}\n{{\n", "}\n", "", "", L".rs" } },
{ "go", { "int", "float32", "uint32", "int64", "uint64", "float64", "string", "bool", "interface", "var {0} {1},", "Vec<{}>", "struct {}\n{{\n", "}\n", "", "", L".go" } },
{ "cpp", { "int", "unsigned int", "long long", "unsigned long long", "float", "double", "bool", "std::string", "void*", "{0} {1};", "std::vector<{}>", "struct {} {{", "};", "#include <vector>", "#include <string>", "h" } },
{ "csharp", { "int", "uint", "long", "ulong", "float", "double", "bool", "string", "object", "{0} {1};", "List<{}>", "class {}\n{{", "}", "using System.Collections.Generic;", "", "cs" } },
{ "java", { "int", "int", "long", "long", "float", "double", "bool", "String", "Object", "{0} {1};", "ArrayList<{}>", "class {}\n{{", "}", "", "", "java" } },
{ "kotlin", { "int", "UInt", "long", "ULong", "float", "double", "bool", "String", "Object", "val {1}: {0};", "List<{}>", "class {}\n{{", "}", "", "", "kt" } },
{ "rust", { "i32", "u32", "i64", "u64", "f32", "f64", "bool", "String", "", "{1}: {0},", "Vec<{}>", "struct {}\n{{", "}", "", "", "rs" } },
};
Loading

0 comments on commit 14daa3d

Please sign in to comment.