Skip to content

Commit

Permalink
Merge pull request #18 from kilianmh/feat/integer-type
Browse files Browse the repository at this point in the history
Feat: Support integer type
  • Loading branch information
gschjetne authored Feb 4, 2024
2 parents 4080855 + e9a9410 commit 0c4dfed
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Type | Remarks
`:any` | Guesses the way to encode and decode the value
`:string` | Enforces a string value
`:number` | Enforces a number value
`:integer` | Enforces a integer value
`:hash-table` | Enforces a hash table value
`:vector` | Enforces a vector value
`:list` | Enforces a list value
Expand Down
4 changes: 4 additions & 0 deletions src/to-json.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
"Return the number VALUE"
value)

(defmethod to-json-value ((value integer) (json-type (eql :integer)))
"Return the integer VALUE"
value)

(defmethod to-json-value ((value hash-table) (json-type (eql :hash-table)))
"Return the hash-table VALUE"
value)
Expand Down
4 changes: 4 additions & 0 deletions src/to-lisp.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
"Return the number VALUE"
value)

(defmethod to-lisp-value ((value integer) (json-type (eql :integer)))
"Return the number VALUE"
value)

(defmethod to-lisp-value ((value hash-table) (json-type (eql :hash-table)))
"Return the hash-table VALUE"
value)
Expand Down
5 changes: 5 additions & 0 deletions tests/encode-decode.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
(is (= (get-number obj)
(get-number (obj-rt obj))))))

(test integer
(for-all ((obj (gen-object)))
(is (= (get-integer obj)
(get-integer (obj-rt obj))))))

(test hash-table
(for-all ((obj (gen-object)))
(is (equalp (get-hash-table obj)
Expand Down
6 changes: 6 additions & 0 deletions tests/tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
:reader get-number
:json-type :number
:json-key "num")
(integer :initarg :integer
:reader get-integer
:json-type :integer
:json-key "int")
(hash :initarg :hash-table
:reader get-hash-table
:json-type :hash-table
Expand Down Expand Up @@ -119,6 +123,7 @@
(defun gen-object (&key
(string (gen-string))
(number (gen-float))
(integer (gen-integer))
(hash-table (gen-hash-table))
(obj-hash-table
(gen-hash-table
Expand All @@ -134,6 +139,7 @@
(make-instance 'test-class
:string (funcall string)
:number (funcall number)
:integer (funcall integer)
:hash-table (funcall hash-table)
:any-hash-table (funcall hash-table)
:obj-hash-table (funcall obj-hash-table)
Expand Down

0 comments on commit 0c4dfed

Please sign in to comment.