-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Formats code. Updates phel dependency to 0.2. Fixes failing test.
- Loading branch information
Showing
9 changed files
with
258 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,47 @@ | ||
(ns mabasic\json\json | ||
(:use \Exception)) | ||
(:use \Exception)) | ||
|
||
(defn- valid-key? [v] | ||
(or (int? v) (float? v) (symbol? v) (keyword? v) (string? v))) | ||
(or (int? v) (float? v) (symbol? v) (keyword? v) (string? v))) | ||
|
||
(defn- encode-value [x] | ||
(cond | ||
(php/is_iterable x) | ||
(let [arr (php/array)] | ||
(foreach [k v x] | ||
(when-not (valid-key? k) | ||
(throw (php/new Exception "Key can only be an integer, float, symbol, keyword or a string."))) | ||
(php/aset arr (encode-value k) (encode-value v))) | ||
arr) | ||
(symbol? x) (str (php/-> x (getName))) | ||
(keyword? x) (str (php/-> x (getName))) | ||
(float? x) (str x) | ||
true x)) | ||
(cond | ||
(php/is_iterable x) | ||
(let [arr (php/array)] | ||
(foreach [k v x] | ||
(when-not (valid-key? k) | ||
(throw (php/new Exception "Key can only be an integer, float, symbol, keyword or a string."))) | ||
(php/aset arr (encode-value k) (encode-value v))) | ||
arr) | ||
(symbol? x) (str (php/-> x (getName))) | ||
(keyword? x) (str (php/-> x (getName))) | ||
(float? x) (str x) | ||
true x)) | ||
|
||
(defn encode [value & [@{:flags flags :depth depth}]] | ||
(let [flags (or flags 0) | ||
depth (or depth 512)] | ||
(when (php/is_resource value) (throw (php/new Exception "Value can be any type except a resource."))) | ||
(when-not (int? flags) (throw (php/new Exception "Flags must be an integer."))) | ||
(when-not (int? depth) (throw (php/new Exception "Depth must be an integer."))) | ||
(when-not (> depth 0) (throw (php/new Exception "Depth must be greater than zero."))) | ||
(php/json_encode (encode-value value) flags depth))) | ||
(let [flags (or flags 0) | ||
depth (or depth 512)] | ||
(when (php/is_resource value) (throw (php/new Exception "Value can be any type except a resource."))) | ||
(when-not (int? flags) (throw (php/new Exception "Flags must be an integer."))) | ||
(when-not (int? depth) (throw (php/new Exception "Depth must be an integer."))) | ||
(when-not (> depth 0) (throw (php/new Exception "Depth must be greater than zero."))) | ||
(php/json_encode (encode-value value) flags depth))) | ||
|
||
(defn- decode-value [x] | ||
(cond | ||
(indexed? x) (for [v :in x] (decode-value v)) | ||
(php-array? x) | ||
(let [table @{}] | ||
(foreach [k v x] | ||
(put table (keyword k) (decode-value v))) | ||
table) | ||
true x)) | ||
(cond | ||
(indexed? x) (for [v :in x] (decode-value v)) | ||
(php-array? x) | ||
(let [table @{}] | ||
(foreach [k v x] | ||
(put table (keyword k) (decode-value v))) | ||
table) | ||
true x)) | ||
|
||
(defn decode [json & [@{:flags flags :depth depth}]] | ||
(let [flags (or flags 0) | ||
depth (or depth 512)] | ||
(when-not (string? json) (throw (php/new Exception "Json must be a string."))) | ||
(when-not (int? flags) (throw (php/new Exception "Flags must be an integer."))) | ||
(when-not (int? depth) (throw (php/new Exception "Depth must be an integer."))) | ||
(when-not (> depth 0) (throw (php/new Exception "Depth must be greater than zero."))) | ||
(decode-value (php/json_decode json true depth flags)))) | ||
(let [flags (or flags 0) | ||
depth (or depth 512)] | ||
(when-not (string? json) (throw (php/new Exception "Json must be a string."))) | ||
(when-not (int? flags) (throw (php/new Exception "Flags must be an integer."))) | ||
(when-not (int? depth) (throw (php/new Exception "Depth must be an integer."))) | ||
(when-not (> depth 0) (throw (php/new Exception "Depth must be greater than zero."))) | ||
(decode-value (php/json_decode json true depth flags)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,30 @@ | ||
(ns mabasic\json\tests\decode\flags | ||
(:require mabasic\json\json) | ||
(:require phel\test :refer [deftest is]) | ||
(:use \JSON_INVALID_UTF8_IGNORE) | ||
(:use \JSON_INVALID_UTF8_SUBSTITUTE)) | ||
(:require mabasic\json\json) | ||
(:require phel\test :refer [deftest is]) | ||
(:use \JSON_INVALID_UTF8_IGNORE) | ||
(:use \JSON_INVALID_UTF8_SUBSTITUTE)) | ||
|
||
(def sample-data (php/file_get_contents (str __DIR__ "/sample-flags.json"))) | ||
|
||
(deftest invalid-flag | ||
(is | ||
(thrown-with-msg? | ||
Exception "Flags must be an integer." | ||
(json/decode sample-data @{:flags "flags"})) | ||
"It tests if flags parameter is an integer.")) | ||
(is | ||
(thrown-with-msg? | ||
Exception "Flags must be an integer." | ||
(json/decode sample-data @{:flags "flags"})) | ||
"It tests if flags parameter is an integer.")) | ||
|
||
(deftest flag | ||
(is | ||
(= | ||
@{:employee @{:name "sonoo 0xC0" :salary 56000 :married true}} | ||
(json/decode sample-data @{:flags JSON_INVALID_UTF8_IGNORE})) | ||
"It tests flags parameter with one flag.")) | ||
(is | ||
(= | ||
@{:employee @{:name "sonoo 0xC0" :salary 56000 :married true}} | ||
(json/decode sample-data @{:flags JSON_INVALID_UTF8_IGNORE})) | ||
"It tests flags parameter with one flag.")) | ||
|
||
(deftest flags | ||
(is | ||
(= | ||
@{:employee @{:name "sonoo 0xC0" :salary 56000 :married true}} | ||
(json/decode sample-data @{:flags (bit-or | ||
JSON_INVALID_UTF8_IGNORE | ||
JSON_INVALID_UTF8_SUBSTITUTE)})) | ||
"It tests flags parameter with two flags.")) | ||
(is | ||
(= | ||
@{:employee @{:name "sonoo 0xC0" :salary 56000 :married true}} | ||
(json/decode sample-data @{:flags (bit-or | ||
JSON_INVALID_UTF8_IGNORE | ||
JSON_INVALID_UTF8_SUBSTITUTE)})) | ||
"It tests flags parameter with two flags.")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,20 @@ | ||
(ns mabasic\json\tests\decode\value | ||
(:require mabasic\json\json) | ||
(:require phel\test :refer [deftest is])) | ||
(:require mabasic\json\json) | ||
(:require phel\test :refer [deftest is])) | ||
|
||
(def sample-data (php/file_get_contents (str __DIR__ "/sample-value.json"))) | ||
|
||
(deftest test-something | ||
(is | ||
(= | ||
@{ | ||
:firstName "Rack" | ||
:lastName "Jackon" | ||
:gender "man" | ||
:age 24 | ||
:address @{ | ||
:streetAddress 126 | ||
:city "San Jone" | ||
:state "CA" | ||
:postalCode 394221} | ||
:phoneNumbers @[ | ||
@{ | ||
:type "home" | ||
:number 7383627627}]} | ||
(json/decode sample-data)))) | ||
(is | ||
(= | ||
@{:firstName "Rack" | ||
:lastName "Jackon" | ||
:gender "man" | ||
:age 24 | ||
:address @{:streetAddress "126" | ||
:city "San Jone" | ||
:state "CA" | ||
:postalCode "394221"} | ||
:phoneNumbers @[@{:type "home" | ||
:number "7383627627"}]} | ||
(json/decode sample-data)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
(ns mabasic\json\tests\encode\depth | ||
(:require mabasic\json\json) | ||
(:require phel\test :refer [deftest is])) | ||
(:require mabasic\json\json) | ||
(:require phel\test :refer [deftest is])) | ||
|
||
(deftest depth | ||
(let [sample-data @[1 [2] [[3]]]] | ||
(is | ||
(= | ||
"[1,[2],[[3]]]" | ||
(json/encode sample-data @{:depth 3})) | ||
"It returns a string containing the JSON representation of the supplied value if depth is equal to or higher than entered depth.") | ||
(is | ||
(= | ||
false | ||
(json/encode sample-data @{:depth 2})) | ||
"It returns false if data depth is higher than entered depth."))) | ||
|
||
(deftest invalid-depth | ||
(let [sample-data @[1 [2] [[3]]]] | ||
(is | ||
(thrown-with-msg? | ||
Exception "Depth must be an integer." | ||
(json/encode | ||
@{:and "a & b"} | ||
@{:depth "depth"})) | ||
"It tests if depth parameter is an integer.") | ||
(= | ||
"[1,[2],[[3]]]" | ||
(json/encode sample-data @{:depth 3})) | ||
"It returns a string containing the JSON representation of the supplied value if depth is equal to or higher than entered depth.") | ||
(is | ||
(thrown-with-msg? | ||
Exception "Depth must be greater than zero." | ||
(json/encode | ||
@{:and "a & b"} | ||
@{:depth 0})) | ||
"It tests if depth parameter is greater than zero.")) | ||
(= | ||
false | ||
(json/encode sample-data @{:depth 2})) | ||
"It returns false if data depth is higher than entered depth."))) | ||
|
||
(deftest invalid-depth | ||
(is | ||
(thrown-with-msg? | ||
Exception "Depth must be an integer." | ||
(json/encode | ||
@{:and "a & b"} | ||
@{:depth "depth"})) | ||
"It tests if depth parameter is an integer.") | ||
(is | ||
(thrown-with-msg? | ||
Exception "Depth must be greater than zero." | ||
(json/encode | ||
@{:and "a & b"} | ||
@{:depth 0})) | ||
"It tests if depth parameter is greater than zero.")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
(ns mabasic\json\tests\encode\flags | ||
(:require mabasic\json\json) | ||
(:require phel\test :refer [deftest is]) | ||
(:use \JSON_HEX_TAG) | ||
(:use \JSON_HEX_AMP)) | ||
(:require mabasic\json\json) | ||
(:require phel\test :refer [deftest is]) | ||
(:use \JSON_HEX_TAG) | ||
(:use \JSON_HEX_AMP)) | ||
|
||
(deftest invalid-flag | ||
(is | ||
(thrown-with-msg? | ||
Exception "Flags must be an integer." | ||
(json/encode | ||
@{:and "a & b"} | ||
@{:flags "flags"})) | ||
"It tests if flags parameter is an integer.")) | ||
(is | ||
(thrown-with-msg? | ||
Exception "Flags must be an integer." | ||
(json/encode | ||
@{:and "a & b"} | ||
@{:flags "flags"})) | ||
"It tests if flags parameter is an integer.")) | ||
|
||
(deftest flag | ||
(is | ||
(= | ||
"{\"and\":\"a \u0026 b\"}" | ||
(json/encode | ||
@{:and "a & b"} | ||
@{:flags JSON_HEX_AMP})) | ||
"It tests flags parameter with one flag.")) | ||
(is | ||
(= | ||
"{\"and\":\"a \u0026 b\"}" | ||
(json/encode | ||
@{:and "a & b"} | ||
@{:flags JSON_HEX_AMP})) | ||
"It tests flags parameter with one flag.")) | ||
|
||
(deftest flags | ||
(is | ||
(= | ||
"{\"comparison\":\"a \u003E b\",\"and\":\"a \u0026 b\"}" | ||
(json/encode | ||
@{:comparison "a > b" :and "a & b"} | ||
@{:flags (bit-or JSON_HEX_AMP JSON_HEX_TAG)})) | ||
"It tests flags parameter with two flags.")) | ||
(is | ||
(= | ||
"{\"comparison\":\"a \u003E b\",\"and\":\"a \u0026 b\"}" | ||
(json/encode | ||
@{:comparison "a > b" :and "a & b"} | ||
@{:flags (bit-or JSON_HEX_AMP JSON_HEX_TAG)})) | ||
"It tests flags parameter with two flags.")) |
Oops, something went wrong.