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

Automatically decode from pathnames/streams in jget. #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ entry under key is found (like in ~gethash~).
(njson:jget #p"/second-key/0" data)
;; => 1, T

;; Can decode and index files as an alias for `njson:decode'.
(njson:jget #(0 "kind") #p"tests/baker.json")
;; "Listing"

;; Modify the element in place:
(setf (njson:jget #p"/second-key/0" data) 3)
;; Another indexing syntax, for no particular reason:
Expand Down
11 changes: 11 additions & 0 deletions functions.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ CHAR is left unread on STREAM after returning."
(cerror "Return nothing"
'non-indexable :value object)
(values nil nil))
(:method (key (object pathname))
(jget* key (decode-from-file object)))
(:method (key (object stream))
(jget* key (decode-from-stream object)))
(:method ((key string) (object string))
(declare (ignore key))
(cerror "Return nothing"
Expand All @@ -137,6 +141,13 @@ KEY-OR-INDEX can be
- a sequence of integers and strings (to index the nested structures).
- an empty sequence/pathname (to match the whole object).

OBJECT can be
- A hash table, indexed by strings
- An array, indexed by integers
- A stream, which is `decode'd and then indexed
- A pathname, which is `decode'd and then indexed
- Anything else, considered an error (see below)

Return two values: the value under KEY-OR-INDEX and whether this value
was found.

Expand Down
12 changes: 12 additions & 0 deletions tests/tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,15 @@
;; (assert-no-key #(("data" ("parents" _))))
;; (assert-no-key #(("data" ("parents" ("nested" :true)))))
)))

(define-test inline-decode-file/stream ()
(let* ((baker-file (asdf:system-relative-pathname :njson "tests/baker.json"))
(baker-stream (open baker-file))
(baker-decoded (decode baker-file)))
(assert-equal "Henry Baker: Meta-circular semantics for Common Lisp special forms"
(jget #(0 "data" "children" 0 "data" "title") baker-decoded))
(assert-equal "Henry Baker: Meta-circular semantics for Common Lisp special forms"
(jget #(0 "data" "children" 0 "data" "title") baker-file))
(assert-equal "Henry Baker: Meta-circular semantics for Common Lisp special forms"
(jget #(0 "data" "children" 0 "data" "title") baker-stream))
(close baker-stream)))