-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.cljc
44 lines (35 loc) · 1 KB
/
data.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(ns quark.conversion.data
(:require [quark.lang.string :as string]
#?@(:clj [[cheshire.core :as json]
[clojure.edn :as edn]]
:cljs [[cljs.reader :as reader]])))
(def readers {})
(def read-str
#?(:cljs reader/read-string
:clj edn/read-string))
(defn edn-str->edn
[edn-str]
(read-str {:readers readers} edn-str))
(defn str->uuid
[id-str]
(read-str (str "#uuid \"" id-str "\"")))
(def any->str
string/any->str)
(defn any->edn-str
[value]
(with-out-str (pr value)))
(defn edn->json
[data]
#?(:cljs (.stringify js/JSON (clj->js data))
:clj (-> data
string/dash->underscore
json/generate-string)))
#?(:cljs
(defn js->edn [js] (js->clj js :keywordize-keys true)))
(defn json->edn
[json]
(when-not (= json "undefined")
#?(:cljs (try (js->edn (.parse js/JSON json)) (catch js/Error _ nil))
:clj (-> json
string/underscore->dash
(json/parse-string true)))))