Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit 8ee36c9

Browse files
committed
Formats code. Updates phel dependency to 0.2. Fixes failing test.
1 parent 9ea45f5 commit 8ee36c9

File tree

9 files changed

+258
-262
lines changed

9 files changed

+258
-262
lines changed

LICENSE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

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

55
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:
66

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"minimum-stability": "dev",
1414
"require": {
15-
"phel-lang/phel-lang": "dev-master"
15+
"phel-lang/phel-lang": "^0.2"
1616
},
1717
"extra": {
1818
"phel": {

src/json.phel

+36-36
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
(ns mabasic\json\json
2-
(:use \Exception))
2+
(:use \Exception))
33

44
(defn- valid-key? [v]
5-
(or (int? v) (float? v) (symbol? v) (keyword? v) (string? v)))
5+
(or (int? v) (float? v) (symbol? v) (keyword? v) (string? v)))
66

77
(defn- encode-value [x]
8-
(cond
9-
(php/is_iterable x)
10-
(let [arr (php/array)]
11-
(foreach [k v x]
12-
(when-not (valid-key? k)
13-
(throw (php/new Exception "Key can only be an integer, float, symbol, keyword or a string.")))
14-
(php/aset arr (encode-value k) (encode-value v)))
15-
arr)
16-
(symbol? x) (str (php/-> x (getName)))
17-
(keyword? x) (str (php/-> x (getName)))
18-
(float? x) (str x)
19-
true x))
8+
(cond
9+
(php/is_iterable x)
10+
(let [arr (php/array)]
11+
(foreach [k v x]
12+
(when-not (valid-key? k)
13+
(throw (php/new Exception "Key can only be an integer, float, symbol, keyword or a string.")))
14+
(php/aset arr (encode-value k) (encode-value v)))
15+
arr)
16+
(symbol? x) (str (php/-> x (getName)))
17+
(keyword? x) (str (php/-> x (getName)))
18+
(float? x) (str x)
19+
true x))
2020

2121
(defn encode [value & [@{:flags flags :depth depth}]]
22-
(let [flags (or flags 0)
23-
depth (or depth 512)]
24-
(when (php/is_resource value) (throw (php/new Exception "Value can be any type except a resource.")))
25-
(when-not (int? flags) (throw (php/new Exception "Flags must be an integer.")))
26-
(when-not (int? depth) (throw (php/new Exception "Depth must be an integer.")))
27-
(when-not (> depth 0) (throw (php/new Exception "Depth must be greater than zero.")))
28-
(php/json_encode (encode-value value) flags depth)))
22+
(let [flags (or flags 0)
23+
depth (or depth 512)]
24+
(when (php/is_resource value) (throw (php/new Exception "Value can be any type except a resource.")))
25+
(when-not (int? flags) (throw (php/new Exception "Flags must be an integer.")))
26+
(when-not (int? depth) (throw (php/new Exception "Depth must be an integer.")))
27+
(when-not (> depth 0) (throw (php/new Exception "Depth must be greater than zero.")))
28+
(php/json_encode (encode-value value) flags depth)))
2929

3030
(defn- decode-value [x]
31-
(cond
32-
(indexed? x) (for [v :in x] (decode-value v))
33-
(php-array? x)
34-
(let [table @{}]
35-
(foreach [k v x]
36-
(put table (keyword k) (decode-value v)))
37-
table)
38-
true x))
31+
(cond
32+
(indexed? x) (for [v :in x] (decode-value v))
33+
(php-array? x)
34+
(let [table @{}]
35+
(foreach [k v x]
36+
(put table (keyword k) (decode-value v)))
37+
table)
38+
true x))
3939

4040
(defn decode [json & [@{:flags flags :depth depth}]]
41-
(let [flags (or flags 0)
42-
depth (or depth 512)]
43-
(when-not (string? json) (throw (php/new Exception "Json must be a string.")))
44-
(when-not (int? flags) (throw (php/new Exception "Flags must be an integer.")))
45-
(when-not (int? depth) (throw (php/new Exception "Depth must be an integer.")))
46-
(when-not (> depth 0) (throw (php/new Exception "Depth must be greater than zero.")))
47-
(decode-value (php/json_decode json true depth flags))))
41+
(let [flags (or flags 0)
42+
depth (or depth 512)]
43+
(when-not (string? json) (throw (php/new Exception "Json must be a string.")))
44+
(when-not (int? flags) (throw (php/new Exception "Flags must be an integer.")))
45+
(when-not (int? depth) (throw (php/new Exception "Depth must be an integer.")))
46+
(when-not (> depth 0) (throw (php/new Exception "Depth must be greater than zero.")))
47+
(decode-value (php/json_decode json true depth flags))))

tests/decode/flags.phel

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
(ns mabasic\json\tests\decode\flags
2-
(:require mabasic\json\json)
3-
(:require phel\test :refer [deftest is])
4-
(:use \JSON_INVALID_UTF8_IGNORE)
5-
(:use \JSON_INVALID_UTF8_SUBSTITUTE))
2+
(:require mabasic\json\json)
3+
(:require phel\test :refer [deftest is])
4+
(:use \JSON_INVALID_UTF8_IGNORE)
5+
(:use \JSON_INVALID_UTF8_SUBSTITUTE))
66

77
(def sample-data (php/file_get_contents (str __DIR__ "/sample-flags.json")))
88

99
(deftest invalid-flag
10-
(is
11-
(thrown-with-msg?
12-
Exception "Flags must be an integer."
13-
(json/decode sample-data @{:flags "flags"}))
14-
"It tests if flags parameter is an integer."))
10+
(is
11+
(thrown-with-msg?
12+
Exception "Flags must be an integer."
13+
(json/decode sample-data @{:flags "flags"}))
14+
"It tests if flags parameter is an integer."))
1515

1616
(deftest flag
17-
(is
18-
(=
19-
@{:employee @{:name "sonoo 0xC0" :salary 56000 :married true}}
20-
(json/decode sample-data @{:flags JSON_INVALID_UTF8_IGNORE}))
21-
"It tests flags parameter with one flag."))
17+
(is
18+
(=
19+
@{:employee @{:name "sonoo 0xC0" :salary 56000 :married true}}
20+
(json/decode sample-data @{:flags JSON_INVALID_UTF8_IGNORE}))
21+
"It tests flags parameter with one flag."))
2222

2323
(deftest flags
24-
(is
25-
(=
26-
@{:employee @{:name "sonoo 0xC0" :salary 56000 :married true}}
27-
(json/decode sample-data @{:flags (bit-or
28-
JSON_INVALID_UTF8_IGNORE
29-
JSON_INVALID_UTF8_SUBSTITUTE)}))
30-
"It tests flags parameter with two flags."))
24+
(is
25+
(=
26+
@{:employee @{:name "sonoo 0xC0" :salary 56000 :married true}}
27+
(json/decode sample-data @{:flags (bit-or
28+
JSON_INVALID_UTF8_IGNORE
29+
JSON_INVALID_UTF8_SUBSTITUTE)}))
30+
"It tests flags parameter with two flags."))

tests/decode/value.phel

+15-19
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
(ns mabasic\json\tests\decode\value
2-
(:require mabasic\json\json)
3-
(:require phel\test :refer [deftest is]))
2+
(:require mabasic\json\json)
3+
(:require phel\test :refer [deftest is]))
44

55
(def sample-data (php/file_get_contents (str __DIR__ "/sample-value.json")))
66

77
(deftest test-something
8-
(is
9-
(=
10-
@{
11-
:firstName "Rack"
12-
:lastName "Jackon"
13-
:gender "man"
14-
:age 24
15-
:address @{
16-
:streetAddress 126
17-
:city "San Jone"
18-
:state "CA"
19-
:postalCode 394221}
20-
:phoneNumbers @[
21-
@{
22-
:type "home"
23-
:number 7383627627}]}
24-
(json/decode sample-data))))
8+
(is
9+
(=
10+
@{:firstName "Rack"
11+
:lastName "Jackon"
12+
:gender "man"
13+
:age 24
14+
:address @{:streetAddress "126"
15+
:city "San Jone"
16+
:state "CA"
17+
:postalCode "394221"}
18+
:phoneNumbers @[@{:type "home"
19+
:number "7383627627"}]}
20+
(json/decode sample-data))))

tests/encode/depth.phel

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
(ns mabasic\json\tests\encode\depth
2-
(:require mabasic\json\json)
3-
(:require phel\test :refer [deftest is]))
2+
(:require mabasic\json\json)
3+
(:require phel\test :refer [deftest is]))
44

55
(deftest depth
6-
(let [sample-data @[1 [2] [[3]]]]
7-
(is
8-
(=
9-
"[1,[2],[[3]]]"
10-
(json/encode sample-data @{:depth 3}))
11-
"It returns a string containing the JSON representation of the supplied value if depth is equal to or higher than entered depth.")
12-
(is
13-
(=
14-
false
15-
(json/encode sample-data @{:depth 2}))
16-
"It returns false if data depth is higher than entered depth.")))
17-
18-
(deftest invalid-depth
6+
(let [sample-data @[1 [2] [[3]]]]
197
(is
20-
(thrown-with-msg?
21-
Exception "Depth must be an integer."
22-
(json/encode
23-
@{:and "a & b"}
24-
@{:depth "depth"}))
25-
"It tests if depth parameter is an integer.")
8+
(=
9+
"[1,[2],[[3]]]"
10+
(json/encode sample-data @{:depth 3}))
11+
"It returns a string containing the JSON representation of the supplied value if depth is equal to or higher than entered depth.")
2612
(is
27-
(thrown-with-msg?
28-
Exception "Depth must be greater than zero."
29-
(json/encode
30-
@{:and "a & b"}
31-
@{:depth 0}))
32-
"It tests if depth parameter is greater than zero."))
13+
(=
14+
false
15+
(json/encode sample-data @{:depth 2}))
16+
"It returns false if data depth is higher than entered depth.")))
17+
18+
(deftest invalid-depth
19+
(is
20+
(thrown-with-msg?
21+
Exception "Depth must be an integer."
22+
(json/encode
23+
@{:and "a & b"}
24+
@{:depth "depth"}))
25+
"It tests if depth parameter is an integer.")
26+
(is
27+
(thrown-with-msg?
28+
Exception "Depth must be greater than zero."
29+
(json/encode
30+
@{:and "a & b"}
31+
@{:depth 0}))
32+
"It tests if depth parameter is greater than zero."))

tests/encode/flags.phel

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
(ns mabasic\json\tests\encode\flags
2-
(:require mabasic\json\json)
3-
(:require phel\test :refer [deftest is])
4-
(:use \JSON_HEX_TAG)
5-
(:use \JSON_HEX_AMP))
2+
(:require mabasic\json\json)
3+
(:require phel\test :refer [deftest is])
4+
(:use \JSON_HEX_TAG)
5+
(:use \JSON_HEX_AMP))
66

77
(deftest invalid-flag
8-
(is
9-
(thrown-with-msg?
10-
Exception "Flags must be an integer."
11-
(json/encode
12-
@{:and "a & b"}
13-
@{:flags "flags"}))
14-
"It tests if flags parameter is an integer."))
8+
(is
9+
(thrown-with-msg?
10+
Exception "Flags must be an integer."
11+
(json/encode
12+
@{:and "a & b"}
13+
@{:flags "flags"}))
14+
"It tests if flags parameter is an integer."))
1515

1616
(deftest flag
17-
(is
18-
(=
19-
"{\"and\":\"a \u0026 b\"}"
20-
(json/encode
21-
@{:and "a & b"}
22-
@{:flags JSON_HEX_AMP}))
23-
"It tests flags parameter with one flag."))
17+
(is
18+
(=
19+
"{\"and\":\"a \u0026 b\"}"
20+
(json/encode
21+
@{:and "a & b"}
22+
@{:flags JSON_HEX_AMP}))
23+
"It tests flags parameter with one flag."))
2424

2525
(deftest flags
26-
(is
27-
(=
28-
"{\"comparison\":\"a \u003E b\",\"and\":\"a \u0026 b\"}"
29-
(json/encode
30-
@{:comparison "a > b" :and "a & b"}
31-
@{:flags (bit-or JSON_HEX_AMP JSON_HEX_TAG)}))
32-
"It tests flags parameter with two flags."))
26+
(is
27+
(=
28+
"{\"comparison\":\"a \u003E b\",\"and\":\"a \u0026 b\"}"
29+
(json/encode
30+
@{:comparison "a > b" :and "a & b"}
31+
@{:flags (bit-or JSON_HEX_AMP JSON_HEX_TAG)}))
32+
"It tests flags parameter with two flags."))

0 commit comments

Comments
 (0)