Skip to content

Commit

Permalink
Formats code. Updates phel dependency to 0.2. Fixes failing test.
Browse files Browse the repository at this point in the history
  • Loading branch information
mabasic committed Mar 18, 2021
1 parent 9ea45f5 commit 8ee36c9
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 262 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2020 Mario Bašić
Copyright (c) 2020-2021 Mario Bašić

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"minimum-stability": "dev",
"require": {
"phel-lang/phel-lang": "dev-master"
"phel-lang/phel-lang": "^0.2"
},
"extra": {
"phel": {
Expand Down
72 changes: 36 additions & 36 deletions src/json.phel
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))))
42 changes: 21 additions & 21 deletions tests/decode/flags.phel
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."))
34 changes: 15 additions & 19 deletions tests/decode/value.phel
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))))
54 changes: 27 additions & 27 deletions tests/encode/depth.phel
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."))
50 changes: 25 additions & 25 deletions tests/encode/flags.phel
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."))
Loading

0 comments on commit 8ee36c9

Please sign in to comment.