jsown icon indicating copy to clipboard operation
jsown copied to clipboard

Cannot parse bare numbers

Open jasom opened this issue 9 years ago • 6 comments

* (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]                                                          

jasom avatar Jan 25 '16 20:01 jasom

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?

madnificent avatar Jan 25 '16 20:01 madnificent

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.

jasom avatar Jan 25 '16 20:01 jasom

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.

jasom avatar Jan 25 '16 20:01 jasom

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.

madnificent avatar Jan 25 '16 20:01 madnificent

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.

otwieracz avatar Mar 15 '17 14:03 otwieracz

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"))

madnificent avatar Mar 20 '17 00:03 madnificent