Fork me on GitHub
#clojurescript
<
2024-01-10
>
Nik06:01:06

Hi, I'm trying parse and read edn (to/from strings) in cljs. I using defrecord for my objects Out of the box it didn't work. I followed advise given by https://coderwall.com/p/3xqr7q/clojurescript-read-string-and-records but getting same error.

(defrecord Test [a b])
(cljs.reader/register-tag-parser! "Test" map->Test)
(def a (->Test 1 2))
(cljs.reader/read-string (pr-str a))
Error
; 
; Execution error (ExceptionInfo) at (<cljs repl>:1).
; No reader function for tag dev.user.Test.
On a higher level, I'm trying to use js/localStorage for my app's data

thheller07:01:21

I'd strongly recommend using transit, just for speed reasons

thheller07:01:40

but besides that the tag is dev.user.Test not Test, so if you adjust that it should be fine

Nik09:01:40

@U05224H0W Tried both expressions

(cljs.reader/register-tag-parser! "dev.user.Test" map->Test)
(cljs.reader/register-tag-parser! "dev.user.Test" map->Test)
In both cases error is same
; 
; Execution error (ExceptionInfo) at (<cljs repl>:1).
; No reader function for tag dev.user.Test.

Nik09:01:01

@U05224H0W I know a little about transit and my understanding about it was that it helps in communication between apps (eg client - server). This is PWA (offline) so it only needs client side persistent db hence localStorage

Nik09:01:49

Interesting to know about writer customisation. I'll think if it would help me in my data storage design.

thheller09:01:37

transit is a data serialization library, that is what you are doing

1
thheller09:01:00

it has nothing to do with client - server communication, it also is just the serialization format usable for that

thheller09:01:39

so instead of storing a edn string in localstorage you are storing a transit string, still just a string

thheller09:01:18

edn is fine too, just a bit slower

Nik09:01:40

@U05224H0W BTW thanks for the Art of Clourescript blog post. I've been using it as inspiration for my app! Pure cljs without any framework

👍 1
Nik09:01:23

Understood 👍:skin-tone-3: , thanks for the explanation

phill11:01:43

Public Service Announcement: @U05224H0W https://github.com/cognitect/transit-clj says in italics "NOTE: Transit is intended primarily as a wire protocol for transferring data between applications. If storing Transit data durably, readers and writers are expected to use the same version of Transit"

thheller11:01:53

given that in the 5 years since it has been out nothing has changed I think its safe 😉

😄 1
👻 1
👍 1