GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Python2/modules/my_json.py at master · Alessandroo/Python2

Expand file tree

/

my_json.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
136 lines (102 loc) · 3.23 KB
/

my_json.py

Copy path

File metadata and controls

136 lines (102 loc) · 3.23 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
135
136
import json
import re
class NotJsonSerializableException(TypeError):
def __init__(self, obj):
print("Type {0} is not json serializable".format(type(obj)))
def to_json(obj, raise_unknown = False):
result = ''
if isinstance(obj, bool):
if obj:
result += 'true'
else:
result += 'false'
elif isinstance(obj, (int, float)):
result += str(obj)
elif isinstance(obj, (list, tuple)):
result += '['
for item in obj:
result += to_json(item)
result += ', '
result = result[:-2] + ']'
elif isinstance(obj, dict):
result += '{'
for item in obj.items():
result += to_json(item[0]) + ': ' + to_json(item[1])
result += ', '
result = result[:-2] + '}'
elif isinstance(obj, str):
obj = obj.replace('\\', '\\\\')
obj = obj.replace('\n', '\\n')
obj = obj.replace('\"', '\\"')
obj = obj.replace('\t', '\\t')
result += '\"' + obj + '\"'
elif obj is None:
result += 'null'
if raise_unknown is True:
raise NotJsonSerializableException(obj)
return result
def from_json(line):
def parse_list(line):
obj = []
temp = []
line = line.strip()[1:-1]
values = line.split(', ')
for i in range(len(values)):
if i >= len(values):
break
if values[i].startswith('{') or values[i].startswith('['):
symbol = ''
if values[i].startswith('{'):
symbol = '}'
else:
symbol = ']'
d = ''
while not values[i].endswith(symbol):
d += values[i]
d += ', '
values.remove(values[i])
d += values[i]
values.remove(values[i])
temp.append(d)
else:
temp.append(values[i])
for i in temp:
obj.append(from_json(i))
return obj
def parse_dict(line):
obj = {}
line = line.strip()[1: -1]
values = line.split(', ')
for i in values:
temp = i.split(': ')
obj.update({from_json(temp[0]): from_json(temp[1])})
return obj
line = line.strip()
if re.match('\[.*\]', line):
obj = parse_list(line)
elif re.match('\{.*\}', line):
obj = parse_dict(line)
elif re.match('\".*\"', line):
obj = line[1:-1]
elif re.match('-?\d+\.\d+', line):
obj = float(line)
elif re.match('-?\d+', line):
obj = int(line)
elif re.match('null', line):
obj = None
elif re.match('true|false', line):
if line == "true":
obj = True
else:
obj = False
else:
raise TypeError("JSON line is not valid")
return obj
def main():
# print(json.dumps(['\\\tf\\oo\n', {'bar': ('baz', None, 1.0, 2)}]))
# print(to_json(['\\\tf\\oo\n', {'bar': ('baz', None, 1.0, 2)}]))
# print('\n')
# print(from_json("{false: true}"))
print(from_json("[1.123, {2: false, 1: \"amma\"}, 4, null, [1, false, [\"1.23341\", 2]]]"))
if __name__ == "__main__":
main()
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.