-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjsonParser.js
More file actions
163 lines (153 loc) · 4.26 KB
/
jsonParser.js
File metadata and controls
163 lines (153 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
exports.parseJSON = valueParser
function valueParser (input) {
input = consumeWhiteSpace(input)
return nullParser(input) ||
boolParser(input) ||
numberParser(input) ||
stringParser(input) ||
arrayParser(input) ||
objectParser(input)
}
function nullParser (input) {
if (input.slice(0, 4) !== 'null') {
return null
}
return [null, input.slice(4)]
}
function boolParser (input) {
let regex = /^(?:true|false)/
let match = input.match(regex)
if (!match) {
return null
}
let bool = match[0] === 'true'
return [bool, input.slice(match[0].length)]
}
function stringParser (input) {
let regex = /^"(?:\\"|.)*?"/
let match = input.match(regex)
if (!match) {
return null
}
let strg = match[0].slice(1, -1)
strg = strg.replace(/\\\\/g, '\\')
strg = strg.replace(/\\\//g, '/')
strg = strg.replace(/\\b/g, '\b')
strg = strg.replace(/\\f/g, '\f')
strg = strg.replace(/\\n/g, '\n')
strg = strg.replace(/\\r/g, '\r')
strg = strg.replace(/\\t/g, '\t')
strg = strg.replace(/\\u([\dABCDEFabcdef]{4})/gu, unicode)
return [strg, input.slice(match[0].length)]
}
function numberParser (input) {
let regex = /^(?:-)?(?:0|\d+)(?:\.\d+)?(?:(?:e|E)(?:\+|-)?\d+)?/
let match = input.match(regex)
if (!match) {
return null
}
let num = Number(match[0])
if (-Infinity === num || num === Infinity) {
throw Error('Invalid JSON : Number out of range')
}
return [num, input.slice(match[0].length)]
}
function arrayParser (arrayString) {
if (!arrayString.startsWith('[')) {
return null
}
let arr = []
arrayString = arrayString.slice(1)
let expectingNextElement = false
while (true) {
arrayString = consumeWhiteSpace(arrayString)
let result = valueParser(arrayString)
if (result) {
arr.push(result[0])
arrayString = result[1]
expectingNextElement = false
} else if (!arrayString.startsWith(']')) {
throw Error('Invalid JSON : Excepted "]"')
}
arrayString = consumeWhiteSpace(arrayString)
if (arrayString.startsWith(',')) {
arrayString = arrayString.slice(1)
expectingNextElement = true
continue
} else if (arrayString.startsWith(']') && !expectingNextElement) {
arrayString = arrayString.slice(1)
break
} else {
throw Error('Invalid JSON')
}
}
return [arr, arrayString]
}
function objectParser (objectString) {
if (!objectString.startsWith('{')) {
return null
}
let obj = {}
let expectingNextPair = false
objectString = objectString.slice(1)
while (true) {
objectString = consumeWhiteSpace(objectString)
let parsedStr = stringParser(objectString)
if (objectString.startsWith('}') && !expectingNextPair) {
objectString = objectString.slice(1)
break
} else if (!expectingNextPair && !parsedStr) {
throw Error('Invalid JSON')
} else if (expectingNextPair && !parsedStr) {
throw Error('Invalid JSON : Expecting next key value pair')
}
let key = parsedStr[0]
objectString = parsedStr[1]
objectString = consumeWhiteSpace(objectString)
if (!objectString.startsWith(':')) {
throw Error('Invalid JSON : Expected ":"')
}
objectString = objectString.slice(1)
objectString = consumeWhiteSpace(objectString)
let value = ''
let parsedValue = valueParser(objectString)
if (parsedValue) {
value = parsedValue[0]
objectString = parsedValue[1]
} else {
throw Error('Invalid JSON : Invalid value')
}
obj[key] = value
expectingNextPair = false
objectString = consumeWhiteSpace(objectString)
if (objectString.startsWith(',')) {
objectString = objectString.slice(1)
expectingNextPair = true
continue
} else if (objectString.startsWith('}')) {
objectString = objectString.slice(1)
break
} else {
throw Error('Invalid JSON')
}
}
return [obj, objectString]
}
function whiteSpaceParser (input) {
let regex = /^\s+/
let match = input.match(regex)
if (!match) {
return null
}
return [match[0], input.replace(regex, '')]
}
function consumeWhiteSpace (input) {
let result = whiteSpaceParser(input)
if (result) {
input = result[1]
}
return input
}
function unicode (match, p1) {
return String.fromCharCode(parseInt(p1, 16))
}