-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_parser.py
executable file
·47 lines (40 loc) · 1.6 KB
/
json_parser.py
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
#!/bin/python
# open aircraft.json, which is a json file containing all aircrafts
# loop through each entry and find all of the unique fields
# output the unique fields to a file called fields.txt
# if a field is not always present, tag it with Optional
import json
with open("aircraft.json") as f:
data = json.load(f)
fields = set()
fields_unique = set()
for aircraft in data["aircraft"]:
for field in aircraft:
# if this is not the first aircraft, loop through all of the fields to see if we've not seen them before
if len(fields) > 0:
for f, _ in fields:
if f not in aircraft:
fields_unique.add(f)
# get the type of the field data
field_type = type(aircraft[field])
fields.add((field, str(field_type)))
# sort the fields alphabetically
fields = sorted(fields, key=lambda x: x[0])
with open("fields.txt", "w") as f:
for field, type_of_field in fields:
type_string = type_of_field.split("'")[1]
if type_string == "str":
type_of_field = "String"
elif type_string == "int":
type_of_field = "i32"
elif type_string == "float":
type_of_field = "f32"
elif type_string == "dict":
type_of_field = "Vec<String>"
elif type_string == "list":
type_of_field = "Vec<String>"
if field in fields_unique:
f.write('#[serde(skip_serializing_if = "Option::is_none")]\n')
f.write("pub " + field + ": Option<" + type_of_field + ">,\n")
else:
f.write("pub " + field + ": " + type_of_field + ",\n")