js->clj keywords

3,298 views
Skip to first unread message

Graham MacDonald

unread,
Sep 8, 2013, 4:24:28 PM9/8/13
to clojur...@googlegroups.com
Hi

I'm trying to parse a json object, but my object keys always seem to be strings. Looking at the docstring for js->clj, it seems that setting :keywordize true should convert all string field names to keywords, but either I misunderstand things (quite/very possible), or there's another problem (no!).

A simple example, running the latest clojurescript:
(-> {:abc 123 "def" 456} (clj->js) (js->clj {:keywordize true}))

I would have expected this to return a map with the keys being the keywords :abc and :def, but in this case they're both strings.

So what am I doing wrong/misunderstanding?

Thanks,
Graham

da...@dsargeant.com

unread,
Sep 8, 2013, 5:39:26 PM9/8/13
to clojur...@googlegroups.com
The option is keywordize-keys.

Graham MacDonald

unread,
Sep 8, 2013, 6:36:17 PM9/8/13
to clojur...@googlegroups.com
On Sunday, 8 September 2013 22:39:26 UTC+1, da...@dsargeant.com wrote:
> The option is keywordize-keys.

Good point - thanks! Interestingly, I see that this now works:
(-> {:abc 123 "def" 456} (clj->js) (js->clj :keywordize-keys true))
...thought this doesn't:
(-> {:abc 123 "def" 456} (clj->js) (js->clj {:keywordize-keys true}))
...even though the map style of the second is the same as that used in the single argument version of js->clj in the Clojurescript source code.

Wei Hsu

unread,
Sep 15, 2013, 7:30:51 AM9/15/13
to clojur...@googlegroups.com
I'm curious as well why that's the case.

Nicolás Berger

unread,
Jan 7, 2016, 12:10:22 PM1/7/16
to ClojureScript
Pretty old thread, but it turns out there's a bug in the 1-arity version of js->clj: it passes a map instead of keyword arguments to set the default of :keywordize-keys false. It works fine, because keywordize-keys ends as nil by default, which is falsey of course... But when you (or me, like I did a few minutes ago :) ) try to do the same but passing {:keywordize-keys true} you find that it doesn't work as you expect.

Just sent a patch to fix this: http://dev.clojure.org/jira/browse/CLJS-1540

Varga Radu

unread,
Feb 26, 2017, 12:07:52 PM2/26/17
to ClojureScript
On Thursday, 7 January 2016 18:10:22 UTC+1, Nicolás Berger wrote:
> Pretty old thread, but it turns out there's a bug in the 1-arity version of js->clj: it passes a map instead of keyword arguments to set the default of :keywordize-keys false. It works fine, because keywordize-keys ends as nil by default, which is falsey of course... But when you (or me, like I did a few minutes ago :) ) try to do the same but passing {:keywordize-keys true} you find that it doesn't work as you expect.
>
> Just sent a patch to fix this: http://dev.clojure.org/jira/browse/CLJS-1540

The option ":keywordize-keys" still doesn't work for me.
Usage: (js->clj response :keywordize-keys true))
I still get the stringed-key array-map.
Any ideas why?

Daniel Compton

unread,
Feb 26, 2017, 2:21:05 PM2/26/17
to ClojureScript
What version of CLJS are you using? The patch was introduced in CLJS 1.9.183. Running on Planck this seems to work:

cljs.user=> (js->clj (clj->js {"a" 1}) :keywordize-keys true)
{:a 1}

I have a vague memory that some of the types in the DOM API behave like JS objects, but aren’t actually JS primitives (strings, arrays), so js->clj doesn’t work as expected on them. From memory ClientRect (or something like it) was one of these. That could possibly be the issue?

--
Note that posts from new members are moderated - please be patient with your first post.
---
You received this message because you are subscribed to the Google Groups "ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojurescrip...@googlegroups.com.
To post to this group, send email to clojur...@googlegroups.com.
Visit this group at https://groups.google.com/group/clojurescript.
--

Daniel
Reply all
Reply to author
Forward
0 new messages