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

Commit 165475c

Browse files
committed
Updates phel to 0.3 and refactors code to support new data structures.
1 parent 8ee36c9 commit 165475c

File tree

9 files changed

+94
-93
lines changed

9 files changed

+94
-93
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This Phel library is a wrapper library around PHP `json_encode` and `json_decode
1010

1111
It converts Phel datastructures and basic types to a format that PHP understands before calling `(php/json_encode)`.
1212

13-
It generates valid Phel datastructures (table, array) from given JSON strings using `(php/json_decode)`.
13+
It generates valid Phel datastructures (map, vector) from given JSON strings using `(php/json_decode)`.
1414

1515
## Installation
1616

@@ -24,23 +24,23 @@ composer require mabasic/phel-json
2424

2525
This Phel library has two public method:
2626

27-
- `(json/encode value @{:depth 512 :flags 0})`
28-
- `(json/decode json @{:depth 512 :flags 0})`
27+
- `(json/encode value {:depth 512 :flags 0})`
28+
- `(json/decode json {:depth 512 :flags 0})`
2929

3030

3131
```phel
3232
(ns your\namespace
3333
(:require mabasic\json\json))
3434
35-
(def result (json/encode @{:name "Phel" :type "lisp"}))
35+
(def result (json/encode {:name "Phel" :type "lisp"}))
3636
3737
# result
3838
# {"name": "Phel", "type": "lisp"}
3939
4040
(println (json/decode result))
4141
4242
# output
43-
# @{:name "Phel" :type "Lisp"}
43+
# {:name "Phel" :type "Lisp"}
4444
```
4545

4646
## Sponsors & Backers

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": "^0.2"
15+
"phel-lang/phel-lang": "^0.3"
1616
},
1717
"extra": {
1818
"phel": {

src/json.phel

+7-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
(float? x) (str x)
1919
true x))
2020

21-
(defn encode [value & [@{:flags flags :depth depth}]]
21+
(defn encode [value & [{:flags flags :depth depth}]]
2222
(let [flags (or flags 0)
2323
depth (or depth 512)]
2424
(when (php/is_resource value) (throw (php/new Exception "Value can be any type except a resource.")))
@@ -29,15 +29,16 @@
2929

3030
(defn- decode-value [x]
3131
(cond
32-
(indexed? x) (for [v :in x] (decode-value v))
32+
(indexed? x)
33+
(for [v :in x] (decode-value v))
3334
(php-array? x)
34-
(let [table @{}]
35+
(let [hashmap (transient {})]
3536
(foreach [k v x]
36-
(put table (keyword k) (decode-value v)))
37-
table)
37+
(put hashmap (keyword k) (decode-value v)))
38+
(persistent hashmap))
3839
true x))
3940

40-
(defn decode [json & [@{:flags flags :depth depth}]]
41+
(defn decode [json & [{:flags flags :depth depth}]]
4142
(let [flags (or flags 0)
4243
depth (or depth 512)]
4344
(when-not (string? json) (throw (php/new Exception "Json must be a string.")))

tests/decode/flags.phel

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@
1010
(is
1111
(thrown-with-msg?
1212
Exception "Flags must be an integer."
13-
(json/decode sample-data @{:flags "flags"}))
13+
(json/decode sample-data {:flags "flags"}))
1414
"It tests if flags parameter is an integer."))
1515

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

2323
(deftest flags
2424
(is
2525
(=
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)}))
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)}))
3030
"It tests flags parameter with two flags."))

tests/decode/value.phel

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
(deftest test-something
88
(is
99
(=
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"}]}
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"}]}
2020
(json/decode sample-data))))

tests/encode/depth.phel

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33
(:require phel\test :refer [deftest is]))
44

55
(deftest depth
6-
(let [sample-data @[1 [2] [[3]]]]
6+
(let [sample-data [1 [2] [[3]]]]
77
(is
88
(=
99
"[1,[2],[[3]]]"
10-
(json/encode sample-data @{:depth 3}))
10+
(json/encode sample-data {:depth 3}))
1111
"It returns a string containing the JSON representation of the supplied value if depth is equal to or higher than entered depth.")
1212
(is
1313
(=
1414
false
15-
(json/encode sample-data @{:depth 2}))
15+
(json/encode sample-data {:depth 2}))
1616
"It returns false if data depth is higher than entered depth.")))
1717

1818
(deftest invalid-depth
1919
(is
2020
(thrown-with-msg?
2121
Exception "Depth must be an integer."
2222
(json/encode
23-
@{:and "a & b"}
24-
@{:depth "depth"}))
23+
{:and "a & b"}
24+
{:depth "depth"}))
2525
"It tests if depth parameter is an integer.")
2626
(is
2727
(thrown-with-msg?
2828
Exception "Depth must be greater than zero."
2929
(json/encode
30-
@{:and "a & b"}
31-
@{:depth 0}))
30+
{:and "a & b"}
31+
{:depth 0}))
3232
"It tests if depth parameter is greater than zero."))

tests/encode/flags.phel

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@
99
(thrown-with-msg?
1010
Exception "Flags must be an integer."
1111
(json/encode
12-
@{:and "a & b"}
13-
@{:flags "flags"}))
12+
{:and "a & b"}
13+
{:flags "flags"}))
1414
"It tests if flags parameter is an integer."))
1515

1616
(deftest flag
1717
(is
1818
(=
1919
"{\"and\":\"a \u0026 b\"}"
2020
(json/encode
21-
@{:and "a & b"}
22-
@{:flags JSON_HEX_AMP}))
21+
{:and "a & b"}
22+
{:flags JSON_HEX_AMP}))
2323
"It tests flags parameter with one flag."))
2424

2525
(deftest flags
2626
(is
2727
(=
2828
"{\"comparison\":\"a \u003E b\",\"and\":\"a \u0026 b\"}"
2929
(json/encode
30-
@{:comparison "a > b" :and "a & b"}
31-
@{:flags (bit-or JSON_HEX_AMP JSON_HEX_TAG)}))
30+
{:comparison "a > b" :and "a & b"}
31+
{:flags (bit-or JSON_HEX_AMP JSON_HEX_TAG)}))
3232
"It tests flags parameter with two flags."))

tests/encode/keys.phel

+14-14
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,49 @@
44

55
(deftest valid-keys
66
(is
7-
(= "{\"string\":\"String\"}" (json/encode @{"string" "String"}))
7+
(= "{\"string\":\"String\"}" (json/encode {"string" "String"}))
88
"It tests if string is a valid key.")
99
(is
10-
(= "{\"1\":\"One\"}" (json/encode @{1 "One"}))
10+
(= "{\"1\":\"One\"}" (json/encode {1 "One"}))
1111
"It tests if integer is a valid key.")
1212
(is
13-
(= "{\"3.14\":\"PI\"}" (json/encode @{3.14 "PI"}))
13+
(= "{\"3.14\":\"PI\"}" (json/encode {3.14 "PI"}))
1414
"It tests if float is a valid key.")
1515
(is
16-
(= "{\"keyword\":\"Keyword\"}" (json/encode @{:keyword "Keyword"}))
16+
(= "{\"keyword\":\"Keyword\"}" (json/encode {:keyword "Keyword"}))
1717
"It tests if keyword is a valid key.")
1818
(is
19-
(= "{\"symbol\":\"Symbol\"}" (json/encode @{'symbol "Symbol"}))
19+
(= "{\"symbol\":\"Symbol\"}" (json/encode {'symbol "Symbol"}))
2020
"It tests if symbol is a valid key."))
2121

2222
(deftest invalid-keys
2323
(is
2424
(thrown-with-msg?
2525
Exception "Key can only be an integer, float, symbol, keyword or a string."
26-
(json/encode @{(set 1 2 3) "Set"}))
26+
(json/encode {(set 1 2 3) "Set"}))
2727
"It tests if set is a valid key.")
2828
(is
2929
(thrown-with-msg?
3030
Exception "Key can only be an integer, float, symbol, keyword or a string."
31-
(json/encode @{[1 2 3] "Tuple"}))
32-
"It tests if tuple is a valid key.")
31+
(json/encode {(list 1 2 3) "List"}))
32+
"It tests if list is a valid key.")
3333
(is
3434
(thrown-with-msg?
3535
Exception "Key can only be an integer, float, symbol, keyword or a string."
36-
(json/encode @{@[1 2 3] "Array"}))
37-
"It tests if array is a valid key.")
36+
(json/encode {[1 2 3] "Vector"}))
37+
"It tests if vector is a valid key.")
3838
(is
3939
(thrown-with-msg?
4040
Exception "Key can only be an integer, float, symbol, keyword or a string."
41-
(json/encode @{@{:hello "World!"} "Table"}))
42-
"It tests if table is a valid key.")
41+
(json/encode {{:hello "World!"} "Hash map"}))
42+
"It tests if hash-map is a valid key.")
4343
(is
4444
(thrown-with-msg?
4545
Exception "Key can only be an integer, float, symbol, keyword or a string."
46-
(json/encode @{(php/new \stdClass) "stdClass"}))
46+
(json/encode {(php/new \stdClass) "stdClass"}))
4747
"It tests if stdClass is a valid key.")
4848
(is
4949
(thrown-with-msg?
5050
Exception "Key can only be an integer, float, symbol, keyword or a string."
51-
(json/encode @{(php/new \DateTime) "DateTime"}))
51+
(json/encode {(php/new \DateTime) "DateTime"}))
5252
"It tests if DateTime is a valid key."))

tests/encode/value.phel

+37-37
Original file line numberDiff line numberDiff line change
@@ -26,86 +26,86 @@
2626
(deftest test-symbol
2727
(is (= "\"symbol\"" (json/encode 'symbol))))
2828

29-
(deftest test-tuple
29+
(deftest test-list
3030
(is (=
3131
"[1,2,3]"
32-
(json/encode [1 2 3])))
32+
(json/encode (list 1 2 3))))
3333
(is (=
3434
"[1,2,[1,2]]"
35-
(json/encode [1 2 [1 2]])))
35+
(json/encode (list 1 2 (list 1 2)))))
3636
(is (=
3737
"[1,2,\"string\"]"
38-
(json/encode [1 2 "string"])))
38+
(json/encode (list 1 2 "string"))))
3939
(is (=
4040
"[1,2,[1,2,3]]"
41-
(json/encode [1 2 (set 1 2 3)])))
41+
(json/encode (list 1 2 (set 1 2 3)))))
4242
(is (=
4343
"[1,2,[1,2,[1,2]]]"
44-
(json/encode [1 2 (set 1 2 (set 1 2))])))
44+
(json/encode (list 1 2 (set 1 2 (set 1 2))))))
4545
(is (=
4646
"[1,2,{\"year\":2020,\"planet\":\"Earth\"}]"
47-
(json/encode [1 2 @{:year 2020
48-
"planet" "Earth"}])))
47+
(json/encode (list 1 2 {:year 2020
48+
"planet" "Earth"}))))
4949
(is (=
50-
"[1,2,{\"year\":2020,\"planet\":\"Earth\",\"tuple\":[\"multi\",\"ple\"]}]"
51-
(json/encode [1 2 @{:year 2020
52-
"planet" "Earth"
53-
"tuple" ["multi" "ple"]}]))))
50+
"[1,2,{\"year\":2020,\"planet\":\"Earth\",\"list\":[\"multi\",\"ple\"]}]"
51+
(json/encode (list 1 2 {:year 2020
52+
"planet" "Earth"
53+
"list" (list "multi" "ple")})))))
5454

55-
(deftest test-array
56-
(is (= "[1,2,3]" (json/encode @[1 2 3])))
57-
(is (= "[1,2,[1,2]]" (json/encode @[1 2 @[1 2]]))))
55+
(deftest test-vector
56+
(is (= "[1,2,3]" (json/encode [1 2 3])))
57+
(is (= "[1,2,[1,2]]" (json/encode [1 2 [1 2]]))))
5858

5959
(deftest test-set
6060
(is (= "[1,2,3]" (json/encode (set 1 2 3))))
6161
(is (= "[1,2,[1,2]]" (json/encode (set 1 2 (set 1 2))))))
6262

6363
(defstruct book [title pages year])
6464

65-
(deftest test-table
65+
(deftest test-hash-map
6666
(is
6767
(=
6868
"{\"first_name\":\"Mario\",\"last_name\":\"Basic\"}"
69-
(json/encode @{"first_name" "Mario"
70-
"last_name" "Basic"})))
69+
(json/encode {"first_name" "Mario"
70+
"last_name" "Basic"})))
7171
(is
7272
(=
7373
"{\"first_name\":\"Mario\",\"last_name\":\"Basic\",\"set\":[1,2]}"
74-
(json/encode @{"first_name" "Mario"
75-
"last_name" "Basic"
76-
"set" (set 1 2)})))
74+
(json/encode {"first_name" "Mario"
75+
"last_name" "Basic"
76+
"set" (set 1 2)})))
7777
(is
7878
(=
7979
"{\"first_name\":\"Mario\",\"last_name\":\"Basic\",\"set\":[1,2,[3,4]]}"
80-
(json/encode @{"first_name" "Mario"
81-
"last_name" "Basic"
82-
"set" (set 1 2 (set 3 4))})))
80+
(json/encode {"first_name" "Mario"
81+
"last_name" "Basic"
82+
"set" (set 1 2 (set 3 4))})))
8383
(is
8484
(=
8585
"{\"first_name\":\"Mario\",\"last_name\":\"Basic\",\"set\":[1,2,[\"three\",4]]}"
86-
(json/encode @{"first_name" "Mario"
87-
"last_name" "Basic"
88-
"set" (set 1 2 (set 'three 4))})))
86+
(json/encode {"first_name" "Mario"
87+
"last_name" "Basic"
88+
"set" (set 1 2 (set 'three 4))})))
8989
(is
9090
(=
9191
"{\"first_name\":[\"Super\",\"mario\"],\"last_name\":\"Basic\"}"
92-
(json/encode @{"first_name" @["Super"
93-
:mario]
94-
"last_name" "Basic"})))
92+
(json/encode {"first_name" ["Super"
93+
:mario]
94+
"last_name" "Basic"})))
9595
(is
9696
(=
9797
"{\"first_name\":[\"Super\",\"mario\"],\"last_name\":\"Basic\",\"3.14\":\"Value of PI.\"}"
98-
(json/encode @{"first_name" @["Super"
99-
:mario]
100-
"last_name" "Basic"
101-
3.14 "Value of PI."})))
98+
(json/encode {"first_name" ["Super"
99+
:mario]
100+
"last_name" "Basic"
101+
3.14 "Value of PI."})))
102102
(let [dictionary (book "Dictionary" 100 2020)]
103103
(is
104104
(=
105105
"{\"first_name\":\"Mario\",\"last_name\":\"Basic\",\"authored\":{\"title\":\"Dictionary\",\"pages\":100,\"year\":2020}}"
106-
(json/encode @{"first_name" "Mario"
107-
"last_name" "Basic"
108-
:authored dictionary})))))
106+
(json/encode {"first_name" "Mario"
107+
"last_name" "Basic"
108+
:authored dictionary})))))
109109

110110
(defstruct person [first_name last_name age])
111111

0 commit comments

Comments
 (0)