UNPKG

4.44 kBPlain TextView Raw
1const rxParseJson = /position\s(\d+)$/
2
3export function parseJson(s: string, pos: number): unknown {
4 let endPos: number | undefined
5 parseJson.message = undefined
6 let matches: RegExpExecArray | null
7 if (pos) s = s.slice(pos)
8 try {
9 parseJson.position = pos + s.length
10 return JSON.parse(s)
11 } catch (e) {
12 matches = rxParseJson.exec((e as Error).message)
13 if (!matches) {
14 parseJson.message = "unexpected end"
15 return undefined
16 }
17 endPos = +matches[1]
18 const c = s[endPos]
19 s = s.slice(0, endPos)
20 parseJson.position = pos + endPos
21 try {
22 return JSON.parse(s)
23 } catch (e1) {
24 parseJson.message = `unexpected token ${c}`
25 return undefined
26 }
27 }
28}
29
30parseJson.message = undefined as string | undefined
31parseJson.position = 0 as number
32parseJson.code = 'require("ajv/dist/runtime/parseJson").parseJson'
33
34export function parseJsonNumber(s: string, pos: number, maxDigits?: number): number | undefined {
35 let numStr = ""
36 let c: string
37 parseJsonNumber.message = undefined
38 if (s[pos] === "-") {
39 numStr += "-"
40 pos++
41 }
42 if (s[pos] === "0") {
43 numStr += "0"
44 pos++
45 } else {
46 if (!parseDigits(maxDigits)) {
47 errorMessage()
48 return undefined
49 }
50 }
51 if (maxDigits) {
52 parseJsonNumber.position = pos
53 return +numStr
54 }
55 if (s[pos] === ".") {
56 numStr += "."
57 pos++
58 if (!parseDigits()) {
59 errorMessage()
60 return undefined
61 }
62 }
63 if (((c = s[pos]), c === "e" || c === "E")) {
64 numStr += "e"
65 pos++
66 if (((c = s[pos]), c === "+" || c === "-")) {
67 numStr += c
68 pos++
69 }
70 if (!parseDigits()) {
71 errorMessage()
72 return undefined
73 }
74 }
75 parseJsonNumber.position = pos
76 return +numStr
77
78 function parseDigits(maxLen?: number): boolean {
79 let digit = false
80 while (((c = s[pos]), c >= "0" && c <= "9" && (maxLen === undefined || maxLen-- > 0))) {
81 digit = true
82 numStr += c
83 pos++
84 }
85 return digit
86 }
87
88 function errorMessage(): void {
89 parseJsonNumber.position = pos
90 parseJsonNumber.message = pos < s.length ? `unexpected token ${s[pos]}` : "unexpected end"
91 }
92}
93
94parseJsonNumber.message = undefined as string | undefined
95parseJsonNumber.position = 0 as number
96parseJsonNumber.code = 'require("ajv/dist/runtime/parseJson").parseJsonNumber'
97
98const escapedChars: {[X in string]?: string} = {
99 b: "\b",
100 f: "\f",
101 n: "\n",
102 r: "\r",
103 t: "\t",
104 '"': '"',
105 "/": "/",
106 "\\": "\\",
107}
108
109const CODE_A: number = "a".charCodeAt(0)
110const CODE_0: number = "0".charCodeAt(0)
111
112export function parseJsonString(s: string, pos: number): string | undefined {
113 let str = ""
114 let c: string | undefined
115 parseJsonString.message = undefined
116 // eslint-disable-next-line no-constant-condition, @typescript-eslint/no-unnecessary-condition
117 while (true) {
118 c = s[pos++]
119 if (c === '"') break
120 if (c === "\\") {
121 c = s[pos]
122 if (c in escapedChars) {
123 str += escapedChars[c]
124 pos++
125 } else if (c === "u") {
126 pos++
127 let count = 4
128 let code = 0
129 while (count--) {
130 code <<= 4
131 c = s[pos]
132 if (c === undefined) {
133 errorMessage("unexpected end")
134 return undefined
135 }
136 c = c.toLowerCase()
137 if (c >= "a" && c <= "f") {
138 code += c.charCodeAt(0) - CODE_A + 10
139 } else if (c >= "0" && c <= "9") {
140 code += c.charCodeAt(0) - CODE_0
141 } else {
142 errorMessage(`unexpected token ${c}`)
143 return undefined
144 }
145 pos++
146 }
147 str += String.fromCharCode(code)
148 } else {
149 errorMessage(`unexpected token ${c}`)
150 return undefined
151 }
152 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
153 } else if (c === undefined) {
154 errorMessage("unexpected end")
155 return undefined
156 } else {
157 if (c.charCodeAt(0) >= 0x20) {
158 str += c
159 } else {
160 errorMessage(`unexpected token ${c}`)
161 return undefined
162 }
163 }
164 }
165 parseJsonString.position = pos
166 return str
167
168 function errorMessage(msg: string): void {
169 parseJsonString.position = pos
170 parseJsonString.message = msg
171 }
172}
173
174parseJsonString.message = undefined as string | undefined
175parseJsonString.position = 0 as number
176parseJsonString.code = 'require("ajv/dist/runtime/parseJson").parseJsonString'