Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trivial addition of NaN #48

Open
jetmonk opened this issue Nov 24, 2019 · 4 comments
Open

trivial addition of NaN #48

jetmonk opened this issue Nov 24, 2019 · 4 comments

Comments

@jetmonk
Copy link

jetmonk commented Nov 24, 2019

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

@phmarek
Copy link
Owner

phmarek commented Dec 3, 2019

Would you please create a pull-request here? Thanks a lot!

@jetmonk
Copy link
Author

jetmonk commented Dec 8, 2019

I think I sent a pull request from repository below, but I've never done this before, so have no idea if I did it right.

My modified version is: https://github.com/jetmonk/yason/tree/jetmonk-patch-1

@phmarek
Copy link
Owner

phmarek commented Dec 8, 2019 via email

@jetmonk
Copy link
Author

jetmonk commented Dec 8, 2019

Sorry. 2nd pull request submitted. Hope this one is OK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants