-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.py
80 lines (58 loc) · 2.09 KB
/
reader.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
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
from PyQt5.QtCore import QFile, QJsonDocument, QJsonValue, QJsonParseError, QByteArray
def read_js(js_path):
js = None
stream = QFile(js_path)
if stream.open(QFile.ReadOnly):
js = QByteArray((stream.readAll()))
stream.close()
else:
print(stream.errorString())
return js
def get_type(json_document):
if isinstance(json_document, QJsonValue):
if json_document.isBool():
return 'True' if json_document.isBool() else 'False'
if json_document.isDouble():
return str(json_document.toDouble())
if json_document.isNull():
return ''
if json_document.isString():
return json_document.toString()
if json_document.isArray():
st = ' ['
array = None
if isinstance(json_document, QJsonDocument):
array = json_document.array()
elif isinstance(json_document, QJsonValue):
array = json_document.toArray()
for el in array:
st += get_type(el) + ' '
st = st[:-1]
st += ']'
return st
if json_document.isNull():
return 'null'
if json_document.isObject():
st = ' {\n'
json_document_object = None
if isinstance(json_document, QJsonDocument):
json_document_object = json_document.object()
elif isinstance(json_document, QJsonValue):
json_document_object = json_document.toObject()
for obj in json_document_object:
st += obj + ': ' + get_type(json_document_object[obj]) + '\n'
st = st[:-1]
st += '\n}'
return st
return ''
if __name__ == '__main__':
parse_err = QJsonParseError()
byte_array = read_js('example.json')
json_document = QJsonDocument.fromJson(byte_array, parse_err)
if QJsonParseError.NoError == parse_err.error:
print(parse_err.errorString())
# if json_document.isObject():
# json_object = json_document.object()
# for k in json_object:
# print(k + ': ' + str(json_object[k].toVariant()))
print(get_type(json_document))