Skip to content

trivial addition of NaN #48

@jetmonk

Description

@jetmonk

NaNs are not supported in JSON by default (though in Perl parser apparently). Some JSON files contain them, though.

I found this to be a useful change in parse.lisp

;; export *allow-nan* and 'nan symbols in package.lisp
(defvar *allow-nan* t) 
		    
(defun parse-constant (input)
  (let ((buffer (make-adjustable-string)))
    (loop while (alpha-char-p (peek-char nil input))
          do (vector-push-extend (read-char input) buffer))
    (cond ((string= buffer "true")
	   (if *parse-json-booleans-as-symbols* 'true t))
	  ((string= buffer "false")
	   (if *parse-json-booleans-as-symbols* 'false nil))
	  ((string= buffer "null")
	   (if *parse-json-booleans-as-symbols* 'null  nil))
	  ((and *allow-nan* (string-equal buffer "nan")) ;; allow NaN, nan, etc
	   'nan)
	  (t
	   (error "invalid constant '~A'" buffer)))))

In a followup, I appended updated version that does NaN,nan,inf,infinity,+infinity,-infinity (case insensitive). In parse-number it switches to parse-symbol if it encounters a +/- followed by 'i' or 'I' (for infinity)

Also, the original yason didn't allow a number to have a leading plus sign like {x: +99} because parse% checked only for a leading (#- #\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9). This is correct according to JSON standard, but the new version loosens this restriction to allow parsing of slightly non-compliant JSON.

parse.lisp.txt

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions