-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreatejsonclass.py
More file actions
134 lines (107 loc) · 4.26 KB
/
createjsonclass.py
File metadata and controls
134 lines (107 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import json
import os
import argparse
classes = []
existing_classes = []
def __name_exists(param_name):
return param_name in existing_classes
def __get_dict_class_name(param_class_name, param_parent_name=None):
name = param_class_name.title()
name = "{0}ObjectResponse".format(name)
while __name_exists(name):
if __name_exists(name) and param_parent_name:
name = "{0}{1}".format(param_parent_name, name)
else:
name = "Sub{0}".format(name)
existing_classes.append(name)
return name
def __unity_type_map(param_input_type, param_json_key):
param_json_key = param_json_key.title()
input_type = param_input_type
type_map = {
dict: __get_dict_class_name(param_json_key),
int: "int",
str: "string",
float: "float",
bool: "bool",
}
if not isinstance(input_type, list):
return type_map[type(input_type)]
input_type = input_type[0]
if isinstance(input_type, dict):
__create_unity_class(input_type, __get_dict_class_name(param_json_key))
return "{0}[]".format(
__unity_type_map(input_type, param_json_key)
)
def __create_classes_file(param_file_name, param_json_data, param_path):
file_name = "{0}/{1}.cs".format(param_path, param_file_name)
if not os.path.exists(os.path.dirname(file_name)):
try:
os.makedirs(os.path.dirname(file_name))
except Exception as e:
print(e.with_traceback())
with open(file_name, "w+") as class_file:
class_file_content = __create_classes_file_content(param_json_data, param_file_name)
class_file.write(class_file_content)
def __create_unity_class(param_json_data, param_json_key):
response_class = {"name": "{0}".format(__get_dict_class_name(param_json_key)), "attributes": []}
for key in param_json_data:
value = __unity_type_map(param_json_data[key], key)
if isinstance(param_json_data[key], dict):
__create_unity_class(
param_json_data=param_json_data[key],
param_json_key=key,
)
attribute = {"name": key, "value": value}
response_class.get("attributes").append(attribute)
classes.append(response_class)
def __create_classes_file_content(param_json_data, param_file_name):
content = ""
response_class = {"name": param_file_name, "attributes": []}
for key in param_json_data:
value = __unity_type_map(param_json_data[key], key)
if isinstance(param_json_data[key], dict):
__create_unity_class(
param_json_data=param_json_data[key],
param_json_key=key,
)
attribute = {"name": key, "value": value}
response_class.get("attributes").append(attribute)
classes.append(response_class)
content += "using System;\n"
content += "namespace {0}Namespace".format(response_class.get("name"))
content += "{\n"
class_attr = []
for c in classes:
content += "\n [Serializable]\n"
content += " public class {0}ObjectResponse\n".format(c.get("name").title())
content += " {\n"
for a in c.get("attributes"):
class_attr.append(
" public {0} {1};".format(a.get("value"), a.get("name"))
)
content += "\n".join(class_attr)
content += "\n }\n"
class_attr = []
content += "\n}\n"
return content
def create_json_response_classes(
param_input_file_name, param_output_file_name, param_output_file_path
):
with open(param_input_file_name) as json_file:
data = json.load(json_file)
__create_classes_file(
param_file_name=param_output_file_name,
param_json_data=data,
param_path=param_output_file_path,
)
input_parser = argparse.ArgumentParser()
input_parser.add_argument("-f", "--File", help="e.x. -f SomeFile.json", required=True)
input_parser.add_argument("-p", "--Path", help="e.x. -p ~/SomeProject/", required=False, default="./")
input_args = input_parser.parse_args()
if input_args.File:
json_file = input_args.File
if input_args.Path:
output_path = input_args.Path
class_name = json_file.split(".json")[0]
create_json_response_classes(json_file, class_name, output_path)