jsown
jsown copied to clipboard
Cannot parse bare numbers
* (jsown:parse "12345")
debugger invoked on a SB-KERNEL:BOUNDING-INDICES-BAD-ERROR in thread
#<THREAD "main thread" RUNNING {1002A9CB83}>:
The bounding indices 0 and 9416 are bad for a sequence of length 5.
See also:
The ANSI Standard, Glossary entry for "bounding index designator"
The ANSI Standard, writeup for Issue SUBSEQ-OUT-OF-BOUNDS:IS-AN-ERROR
Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.
restarts (invokable by number or by possibly-abbreviated name):
0: [ABORT] Exit debugger, returning to top level.
(SB-INT:SEQUENCE-BOUNDING-INDICES-BAD-ERROR "12345" 0 9416)
0]
Hi Jasom,
JSOWN currently only parses top-level objects. I can't remember if a lot would need to be changed in order to make parsing of top-level primitives possible. Is it a real-world use-case?
Wow, that was fast! This is not a real-world use-case, I was benchmarking my tnetstring library against jsown (had previously been using cl-json), and this is one of the artificial tests.
Also, top-level arrays seems to work, and I can't think of a time in which I was parsing json and it wasn't either an object or an array.
I don't mind looking at it and seeing if it'd be easy to support though. It's an oversight and probably should become supported some day.
And I've just ran into the same issue - and it's real case scenario here. I am converting user-provided field to JSON and it fails when user provides number.
I will have to dive deeper into the code to support this. The function that reads the number has a POST condition requiring the string to be longer than only the number. I suppose this design decision came from http://json.org/ where it states that JSON is built on two structures, object and array. I might have misinterpreted that.
In the meantime, you can easily work around the issue by converting your user input to an object, and reading from that object:
(defun parse-jsonlike (str)
(jsown:val (jsown:parse (format nil "{ \"v\": ~A }" str)) "v"))