Skip to content

Commit

Permalink
Use provided charset to read and write yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
Deraen committed Jan 14, 2019
1 parent 4b2272a commit be68c1d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
14 changes: 8 additions & 6 deletions modules/muuntaja-yaml/src/muuntaja/format/yaml.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@
(let [options-args (mapcat identity options)]
(reify
core/Decode
(decode [_ data _]
(apply yaml/parse-string (slurp data) options-args)))))
(decode [_ data charset]
(apply yaml/parse-string (slurp data :encoding charset) options-args)))))

(defn encoder [options]
(let [options-args (mapcat identity options)]
(reify
core/EncodeToBytes
(encode-to-bytes [_ data _]
(encode-to-bytes [_ data charset]
(.getBytes
^String (apply yaml/generate-string data options-args)))
^String (apply yaml/generate-string data options-args)
^String charset))
core/EncodeToOutputStream
(encode-to-output-stream [_ data _]
(encode-to-output-stream [_ data charset]
(fn [^OutputStream output-stream]
(.write output-stream (.getBytes ^String (apply yaml/generate-string data options-args))))))))
(.write output-stream (.getBytes ^String (apply yaml/generate-string data options-args)
^String charset)))))))

(def format
(core/map->Format
Expand Down
14 changes: 8 additions & 6 deletions test/muuntaja/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,22 @@
(testing "non-binary-formats encoding with charsets"
(let [data {:fée "böz"}
iso-encoded #(slurp (m/encode m % data "ISO-8859-1"))]
(testing "application/json & application/edn use the given charset"
(testing "application/json"
(is (= "{\"f�e\":\"b�z\"}" (iso-encoded "application/json")))
(is (= "{\"f�e\":\"b�z\"}" (iso-encoded "application/json+cheshire")))
(is (= "{\"f�e\":\"b�z\"}" (iso-encoded "application/json+cheshire"))))

(testing "application/edn"
(is (= "{:f�e \"b�z\"}" (iso-encoded "application/edn"))))

(testing "application/x-yaml & application/transit+json use the platform charset"
(testing "application/x-yaml"
(is (= "{f�e: b�z}\n" (iso-encoded "application/x-yaml"))))

(testing "application/transit+json uses the platform charset"
(testing "utf-8"
(is (= "{fée: böz}\n" (iso-encoded "application/x-yaml")))
(is (= "[\"^ \",\"~:fée\",\"böz\"]" (iso-encoded "application/transit+json"))))
(testing "when default charset is ISO-8859-1"
(with-default-charset
"ISO-8859-1"
(testing "application/x-yaml works"
(is (= "{f�e: b�z}\n" (iso-encoded "application/x-yaml"))))
(testing "application/transit IS BROKEN"
(is (not= "[\"^ \",\"~:f�e\",\"b�z\"]" (iso-encoded "application/transit+json")))))))))

Expand Down

0 comments on commit be68c1d

Please sign in to comment.