|
325 | 325 | (defn invalid-array-exception [] |
326 | 326 | (Exception. "JSON error (invalid array)")) |
327 | 327 |
|
| 328 | +(defn- eof-array-exception [] |
| 329 | + (EOFException. "JSON error (EOF in array)")) |
| 330 | + |
328 | 331 | (defn- read-array* [^InternalPBR stream options] |
329 | 332 | ;; Handles all array values after the first. |
330 | 333 | (loop [result (transient [])] |
331 | 334 | (let [r (conj! result (-read stream true nil options))] |
332 | 335 | (codepoint-case (int (next-token stream)) |
333 | 336 | \] (persistent! r) |
334 | 337 | \, (recur r) |
| 338 | + -1 (throw (eof-array-exception)) |
335 | 339 | (throw (invalid-array-exception)))))) |
336 | 340 |
|
337 | 341 | (defn- read-array [^InternalPBR stream options] |
|
342 | 346 | (codepoint-case c |
343 | 347 | \] [] |
344 | 348 | \, (throw (invalid-array-exception)) |
| 349 | + -1 (throw (eof-array-exception)) |
345 | 350 | (do (.unreadChar stream c) |
346 | 351 | (read-array* stream options))))) |
347 | 352 |
|
| 353 | +(defn- object-colon-exception [] |
| 354 | + (Exception. "JSON error (missing `:` in object)")) |
| 355 | + |
| 356 | +(defn- eof-object-exception [] |
| 357 | + (EOFException. "JSON error (EOF in object)")) |
| 358 | + |
| 359 | +(defn- invalid-key-exception [c] |
| 360 | + (if (= c -1) |
| 361 | + (throw (eof-object-exception)) |
| 362 | + (throw (Exception. (str "JSON error (non-string key in object), found `" (char c) "`, expected `\"`"))))) |
| 363 | + |
| 364 | +(comment |
| 365 | + (compile 'clojure.data.json) |
| 366 | + ) |
| 367 | + |
348 | 368 | (defn- read-key [^InternalPBR stream] |
349 | 369 | (let [c (int (next-token stream))] |
350 | 370 | (if (= c (codepoint \")) |
351 | 371 | (let [key (read-quoted-string stream)] |
352 | 372 | (if (= (codepoint \:) (int (next-token stream))) |
353 | 373 | key |
354 | | - (throw (Exception. "JSON error (missing `:` in object)")))) |
| 374 | + (throw (object-colon-exception)))) |
355 | 375 | (if (= c (codepoint \})) |
356 | 376 | nil |
357 | | - (throw (Exception. (str "JSON error (non-string key in object), found `" (char c) "`, expected `\"`"))))))) |
| 377 | + (invalid-key-exception c))))) |
358 | 378 |
|
359 | 379 | (defn- read-object [^InternalPBR stream options] |
360 | 380 | ;; Expects to be called with the head of the stream AFTER the |
|
374 | 394 | (codepoint-case (int (next-token stream)) |
375 | 395 | \, (recur r) |
376 | 396 | \} (persistent! r) |
| 397 | + -1 (throw (eof-object-exception)) |
377 | 398 | (throw (Exception. "JSON error (missing entry in object)")))) |
378 | 399 | (let [r (persistent! result)] |
379 | 400 | (if (empty? r) |
|
0 commit comments