0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-04-14 07:51:35 -05:00

Add lightweight transit serialization abstraction.

This commit is contained in:
Andrey Antukh 2016-03-15 20:49:42 +02:00
parent 69d903f5f8
commit 1c0c0f509c
2 changed files with 49 additions and 0 deletions

View file

@ -9,6 +9,7 @@
[figwheel-sidecar "0.5.0-6" :scope "test"]
;; runtime
[com.cognitect/transit-cljs "0.8.237"]
[rum "0.6.0" :exclusions [sablono]]
[sablono "0.6.2"]
[cljsjs/react "0.14.3-0"]

48
src/uxbox/transit.cljs Normal file
View file

@ -0,0 +1,48 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
(ns uxbox.transit
"A lightweight abstraction for transit serialization."
(:refer-clojure :exclude [do])
(:require [cognitect.transit :as t]
[uxbox.util.data :refer (parse-int)]
[uxbox.util.datetime :as dt]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Read/Write Transit handlers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def ^:private datetime-write-handler
(reify
Object
(tag [_ v] "m")
(rep [_ v] (dt/format v :offset))
(stringRep [this v] (str (dt/format v :offset)))))
(defn- datetime-read-handler
[v]
(dt/datetime (parse-int v)))
(def ^:privare +read-handlers+
{"u" uuid
"m" datetime-read-handler})
(def ^:privare +write-handlers+
{js/moment datetime-write-handler})
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Public Api
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn decode
[data]
(let [r (t/reader :json {:handlers +read-handlers+})]
(t/read r data)))
(defn encode
[data]
(let [w (t/writer :json {:handlers +write-handlers+})]
(t/write w data)))