-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from crisptrutski/sorted-by-default
Support configurable serialization
- Loading branch information
Showing
10 changed files
with
182 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
(ns matchbox.serialization.keyword | ||
(:require | ||
[matchbox.serialization.plain :as plain] | ||
[matchbox.utils :as utils] | ||
[clojure.walk :as walk])) | ||
|
||
(defn hydrate-kw [x] | ||
(if (and (string? x) (= \: (first x))) (keyword (subs x 1)) x)) | ||
|
||
(defn hydrate-map [x] | ||
(if (map? x) (into (empty x) (map (fn [[k v]] [(keyword k) v]) x)) x)) | ||
|
||
(defn hydrate [v] | ||
#?(:clj (walk/prewalk (comp hydrate-kw hydrate-map plain/hydrate-raw) v) | ||
:cljs (walk/postwalk (comp hydrate-kw hydrate-map) (js->clj v :keywordize-keys true)))) | ||
|
||
(defn kw->str [x] (if (keyword? x) (str x) x)) | ||
|
||
(defn serialize [v] | ||
(->> (walk/stringify-keys v) | ||
(walk/postwalk kw->str) | ||
#?(:cljs clj->js))) | ||
|
||
(defn set-default! [] | ||
(utils/set-date-config! hydrate serialize)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
(ns matchbox.serialization.plain | ||
(:require | ||
[clojure.walk :as walk] | ||
[matchbox.utils :as utils]) | ||
#?(:clj (:import (java.util HashMap ArrayList)))) | ||
|
||
(defn hydrate-raw [x] | ||
#?(:cljs x | ||
:clj | ||
(cond | ||
(instance? HashMap x) (recur (into {} x)) | ||
(instance? ArrayList x) (recur (into [] x)) | ||
:else x))) | ||
|
||
(defn hydrate [v] | ||
#?(:clj (walk/prewalk hydrate-raw v) | ||
:cljs (js->clj v))) | ||
|
||
(defn serialize [v] | ||
#?(:clj (walk/stringify-keys v) | ||
:cljs (clj->js v))) | ||
|
||
(defn set-default! [] | ||
(utils/set-date-config! hydrate serialize)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
(ns matchbox.serialization.sorted | ||
(:require | ||
[clojure.walk :as walk] | ||
[matchbox.serialization.keyword :as keyword] | ||
[matchbox.utils :as utils]) | ||
#?(:clj (:import (java.util HashMap ArrayList)))) | ||
|
||
(defn map->sorted [m] | ||
(if (sorted? m) m (into (sorted-map) (map (juxt (comp keyword key) val) m)))) | ||
|
||
(defn hydrate-shallow [x] | ||
(cond | ||
#?@(:clj [(instance? HashMap x) (recur (map->sorted x)) | ||
(instance? ArrayList x) (recur (into [] x))]) | ||
(map? x) (map->sorted x) | ||
#?@(:cljs [(array? x) (vec x) | ||
(identical? js/Object (type x)) (into (sorted-map) (for [k (js-keys x)] [(keyword k) (aget x k)]))]) | ||
:else (keyword/hydrate-kw x))) | ||
|
||
(defn hydrate [v] (walk/prewalk hydrate-shallow v)) | ||
|
||
(def serialize keyword/serialize) | ||
|
||
(defn set-default! [] | ||
(utils/set-date-config! hydrate serialize)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
(ns matchbox.serialization.stable | ||
(:require | ||
[clojure.walk :as walk] | ||
[matchbox.serialization.keyword :as keyword] | ||
[linked.map] | ||
[matchbox.utils :as utils]) | ||
#?(:clj (:import (java.util HashMap ArrayList)))) | ||
|
||
(defn map->linked [m] | ||
(if (instance? linked.map.LinkedMap m) m (linked.map/->linked-map (map (juxt (comp keyword key) val) m)))) | ||
|
||
(defn hydrate-shallow [x] | ||
(cond | ||
#?@(:clj [(instance? HashMap x) (recur (map->linked x)) | ||
(instance? ArrayList x) (recur (into [] x))]) | ||
(map? x) (map->linked x) | ||
#?@(:cljs [(array? x) (vec x) | ||
(identical? js/Object (type x)) (linked.map/->linked-map (for [k (js-keys x)] [(keyword k) (aget x k)]))]) | ||
:else (keyword/hydrate-kw x))) | ||
|
||
(defn hydrate [v] (walk/prewalk hydrate-shallow v)) | ||
|
||
(def serialize keyword/serialize) | ||
|
||
(defn set-default! [] | ||
(utils/set-date-config! hydrate serialize)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters