Ticket #21 (closed defect: fixed)

Opened 9 years ago

Last modified 6 years ago

can't parse non-natural numbers

Reported by: dherman Owned by: dherman
Priority: major Milestone:
Component: dherman/json.plt Keywords:
Cc: Version:
Racket Version:


Rob Hunter wrote:

Hi Dave,

I'm using your JSON package on PLaneT, and it's way cool. I think it
might not be quite to spec though. For example,

(require (only-in (planet "json.ss" ("dherman" "json.plt" 1 1)) (read read-json)))
(read-json (open-input-string "{ \"somekey\": 4 }"))

#hasheq((somekey . 4))

;; so far so good...

(read-json (open-input-string "{ \"somekey\": 4.1 }"))

read: expected: digits, got: .

I briefly looked at the spec linked from your help documents, and
numbers in JSON don't have to be integers, it seems. Is this a bug,
or am I missing something? thanks!


Change History

Changed 9 years ago by dherman

  • summary changed from can't parse non-integer numbers to can't parse non-natural numbers

Changed 9 years ago by dherman

Rob Hunter wrote:

I just noticed another similar issue:

(read-json (open-input-string "{ \"somekey\": -4 }"))

read: expected: digits, got: -


Changed 9 years ago by dherman

Rob Hunter wrote:

I took a stab at fixing it. The negative sign problem I think is just
a simple bug--when you detect (with peek) a negative sign, you need to
make sure to do a read-char to pop it off the port stack. As far as
the decimal point stuff, I'm not super-pleased with my solution cause
of the set!. Anyway, the patch below does seem to fix the issues I
was having.


< (define (read/digits port)
> > (define (decimal-point? ch)
> >   (char=? ch #\.))
> >
> > (define (read/digits port #:allow-decimal-point (dec-ok #f))
<                                                      (or (eof-object? ch)
<                                                          (not
(char-numeric? ch))))))])
<                   digit)])
> >                                                      (if (and dec-ok (decimal-point? ch))
> >                                                          (begin (set! dec-ok #f) #f)
> >                                                          (or (eof-object? ch)
> >                                                              (not
> >                                                               (char-numeric? ch)))))))])
> >                           digit)])
<   (let* ([sign (if (eq? (peek-char port) #\-) '(#\-) '())]
<          [digits (read/digits port)]
<          [frac (if (eq? (peek-char port) #\.) (read/digits port) '())]
> >   (let* ([sign (if (eq? (peek-char port) #\-) (list (read-char port)) '())]
> >          [digits (read/digits port #:allow-decimal-point #t)]
> >          [frac (if (decimal-point? (peek-char port)) (read/digits port) '())]

Changed 6 years ago by dherman

  • status changed from new to closed
  • resolution set to fixed

Fixed and updated.


Note: See TracTickets for help on using tickets.