How to keep keywords in arrays when converting to json using js->clj?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
My actual behavior is
(js->clj (clj->js [:a :b :c]) :keywordize-keys true)
=> ["a" "b" "c"]
Desired behavior
[:a :b :c]
clojurescript clojurescript-javascript-interop
add a comment |
My actual behavior is
(js->clj (clj->js [:a :b :c]) :keywordize-keys true)
=> ["a" "b" "c"]
Desired behavior
[:a :b :c]
clojurescript clojurescript-javascript-interop
add a comment |
My actual behavior is
(js->clj (clj->js [:a :b :c]) :keywordize-keys true)
=> ["a" "b" "c"]
Desired behavior
[:a :b :c]
clojurescript clojurescript-javascript-interop
My actual behavior is
(js->clj (clj->js [:a :b :c]) :keywordize-keys true)
=> ["a" "b" "c"]
Desired behavior
[:a :b :c]
clojurescript clojurescript-javascript-interop
clojurescript clojurescript-javascript-interop
asked Nov 29 '18 at 2:55
Jp_Jp_
1,65441625
1,65441625
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I don't use ClojureScript, but it should be noted that :keywordize-keys
isn't doing anything likely because vectors are keyed by index. The elements of the vector are the values, not the indices.
You could do something like
(->> [:a :b :c]
(clj->js)
(js->clj)
(mapv keyword))
; Should print [:a :b :c]
Of course, this gets a little more complicated if the structure is nested, but it's the same general idea.
Since JSON doesn't recognize the concept of a "keyword" though, there's no simple way of converting between the two formats and maintaining what is a String and what is a Keyword. If you really need to differentiate, you could use Clojure's EDN format instead of JSON. This would only work though if you aren't doing excessive JavaScript interop. Any data exchanged with a plain JS library will involve the conflation of Keywords and Strings unless the library understands EDN formatting, or you do something unfortunate like attaching some kind of metadata to the object indicating what is a keyword and what isn't.
You could also just do away with the idea of keywords altogether and use Strings for everything internally. That would suck, but at least it would make the interop easier.
Yes.. my structure is very nested, but I'm realising my problem is more general. When I convert to json I loose the information which values were keywords, so there isn't a way I can tell even in a map if I have a keyword that is not the key of the map.
– Jp_
Nov 29 '18 at 3:15
@Jp_ I don't see how it could be handled any differently though. JSON doesn't include any concept of keywords, so that information is necessarily lost when converting a Clojure map to a JSON representation. You could try dealing with Clojure's EDN instead of JSON, unless you need to interop with JS.
– Carcigenicate
Nov 29 '18 at 3:18
I need to interop with JS, what I'm doing is dumping a re-frame db into firebase realtime database. I thought would be a simple operation, but I'm not seeing a way out of it. Maybe keeping the information of what values are keywords somehow when using clj->js.
– Jp_
Nov 29 '18 at 3:29
2
@Jp_ Or just assume all converted map keys are strings. Keywords are nice to have, but they aren't necessary. If you're dealing with records or maps that for whatever reason are using keywords, you can easily create a function to convert the map into a JSON friendly format by using thename
function.
– Carcigenicate
Nov 29 '18 at 3:34
This might work, I'll let you know when I try.
– Jp_
Nov 29 '18 at 3:36
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531174%2fhow-to-keep-keywords-in-arrays-when-converting-to-json-using-js-clj%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't use ClojureScript, but it should be noted that :keywordize-keys
isn't doing anything likely because vectors are keyed by index. The elements of the vector are the values, not the indices.
You could do something like
(->> [:a :b :c]
(clj->js)
(js->clj)
(mapv keyword))
; Should print [:a :b :c]
Of course, this gets a little more complicated if the structure is nested, but it's the same general idea.
Since JSON doesn't recognize the concept of a "keyword" though, there's no simple way of converting between the two formats and maintaining what is a String and what is a Keyword. If you really need to differentiate, you could use Clojure's EDN format instead of JSON. This would only work though if you aren't doing excessive JavaScript interop. Any data exchanged with a plain JS library will involve the conflation of Keywords and Strings unless the library understands EDN formatting, or you do something unfortunate like attaching some kind of metadata to the object indicating what is a keyword and what isn't.
You could also just do away with the idea of keywords altogether and use Strings for everything internally. That would suck, but at least it would make the interop easier.
Yes.. my structure is very nested, but I'm realising my problem is more general. When I convert to json I loose the information which values were keywords, so there isn't a way I can tell even in a map if I have a keyword that is not the key of the map.
– Jp_
Nov 29 '18 at 3:15
@Jp_ I don't see how it could be handled any differently though. JSON doesn't include any concept of keywords, so that information is necessarily lost when converting a Clojure map to a JSON representation. You could try dealing with Clojure's EDN instead of JSON, unless you need to interop with JS.
– Carcigenicate
Nov 29 '18 at 3:18
I need to interop with JS, what I'm doing is dumping a re-frame db into firebase realtime database. I thought would be a simple operation, but I'm not seeing a way out of it. Maybe keeping the information of what values are keywords somehow when using clj->js.
– Jp_
Nov 29 '18 at 3:29
2
@Jp_ Or just assume all converted map keys are strings. Keywords are nice to have, but they aren't necessary. If you're dealing with records or maps that for whatever reason are using keywords, you can easily create a function to convert the map into a JSON friendly format by using thename
function.
– Carcigenicate
Nov 29 '18 at 3:34
This might work, I'll let you know when I try.
– Jp_
Nov 29 '18 at 3:36
|
show 1 more comment
I don't use ClojureScript, but it should be noted that :keywordize-keys
isn't doing anything likely because vectors are keyed by index. The elements of the vector are the values, not the indices.
You could do something like
(->> [:a :b :c]
(clj->js)
(js->clj)
(mapv keyword))
; Should print [:a :b :c]
Of course, this gets a little more complicated if the structure is nested, but it's the same general idea.
Since JSON doesn't recognize the concept of a "keyword" though, there's no simple way of converting between the two formats and maintaining what is a String and what is a Keyword. If you really need to differentiate, you could use Clojure's EDN format instead of JSON. This would only work though if you aren't doing excessive JavaScript interop. Any data exchanged with a plain JS library will involve the conflation of Keywords and Strings unless the library understands EDN formatting, or you do something unfortunate like attaching some kind of metadata to the object indicating what is a keyword and what isn't.
You could also just do away with the idea of keywords altogether and use Strings for everything internally. That would suck, but at least it would make the interop easier.
Yes.. my structure is very nested, but I'm realising my problem is more general. When I convert to json I loose the information which values were keywords, so there isn't a way I can tell even in a map if I have a keyword that is not the key of the map.
– Jp_
Nov 29 '18 at 3:15
@Jp_ I don't see how it could be handled any differently though. JSON doesn't include any concept of keywords, so that information is necessarily lost when converting a Clojure map to a JSON representation. You could try dealing with Clojure's EDN instead of JSON, unless you need to interop with JS.
– Carcigenicate
Nov 29 '18 at 3:18
I need to interop with JS, what I'm doing is dumping a re-frame db into firebase realtime database. I thought would be a simple operation, but I'm not seeing a way out of it. Maybe keeping the information of what values are keywords somehow when using clj->js.
– Jp_
Nov 29 '18 at 3:29
2
@Jp_ Or just assume all converted map keys are strings. Keywords are nice to have, but they aren't necessary. If you're dealing with records or maps that for whatever reason are using keywords, you can easily create a function to convert the map into a JSON friendly format by using thename
function.
– Carcigenicate
Nov 29 '18 at 3:34
This might work, I'll let you know when I try.
– Jp_
Nov 29 '18 at 3:36
|
show 1 more comment
I don't use ClojureScript, but it should be noted that :keywordize-keys
isn't doing anything likely because vectors are keyed by index. The elements of the vector are the values, not the indices.
You could do something like
(->> [:a :b :c]
(clj->js)
(js->clj)
(mapv keyword))
; Should print [:a :b :c]
Of course, this gets a little more complicated if the structure is nested, but it's the same general idea.
Since JSON doesn't recognize the concept of a "keyword" though, there's no simple way of converting between the two formats and maintaining what is a String and what is a Keyword. If you really need to differentiate, you could use Clojure's EDN format instead of JSON. This would only work though if you aren't doing excessive JavaScript interop. Any data exchanged with a plain JS library will involve the conflation of Keywords and Strings unless the library understands EDN formatting, or you do something unfortunate like attaching some kind of metadata to the object indicating what is a keyword and what isn't.
You could also just do away with the idea of keywords altogether and use Strings for everything internally. That would suck, but at least it would make the interop easier.
I don't use ClojureScript, but it should be noted that :keywordize-keys
isn't doing anything likely because vectors are keyed by index. The elements of the vector are the values, not the indices.
You could do something like
(->> [:a :b :c]
(clj->js)
(js->clj)
(mapv keyword))
; Should print [:a :b :c]
Of course, this gets a little more complicated if the structure is nested, but it's the same general idea.
Since JSON doesn't recognize the concept of a "keyword" though, there's no simple way of converting between the two formats and maintaining what is a String and what is a Keyword. If you really need to differentiate, you could use Clojure's EDN format instead of JSON. This would only work though if you aren't doing excessive JavaScript interop. Any data exchanged with a plain JS library will involve the conflation of Keywords and Strings unless the library understands EDN formatting, or you do something unfortunate like attaching some kind of metadata to the object indicating what is a keyword and what isn't.
You could also just do away with the idea of keywords altogether and use Strings for everything internally. That would suck, but at least it would make the interop easier.
edited Nov 29 '18 at 3:43
answered Nov 29 '18 at 3:11
CarcigenicateCarcigenicate
18.5k53262
18.5k53262
Yes.. my structure is very nested, but I'm realising my problem is more general. When I convert to json I loose the information which values were keywords, so there isn't a way I can tell even in a map if I have a keyword that is not the key of the map.
– Jp_
Nov 29 '18 at 3:15
@Jp_ I don't see how it could be handled any differently though. JSON doesn't include any concept of keywords, so that information is necessarily lost when converting a Clojure map to a JSON representation. You could try dealing with Clojure's EDN instead of JSON, unless you need to interop with JS.
– Carcigenicate
Nov 29 '18 at 3:18
I need to interop with JS, what I'm doing is dumping a re-frame db into firebase realtime database. I thought would be a simple operation, but I'm not seeing a way out of it. Maybe keeping the information of what values are keywords somehow when using clj->js.
– Jp_
Nov 29 '18 at 3:29
2
@Jp_ Or just assume all converted map keys are strings. Keywords are nice to have, but they aren't necessary. If you're dealing with records or maps that for whatever reason are using keywords, you can easily create a function to convert the map into a JSON friendly format by using thename
function.
– Carcigenicate
Nov 29 '18 at 3:34
This might work, I'll let you know when I try.
– Jp_
Nov 29 '18 at 3:36
|
show 1 more comment
Yes.. my structure is very nested, but I'm realising my problem is more general. When I convert to json I loose the information which values were keywords, so there isn't a way I can tell even in a map if I have a keyword that is not the key of the map.
– Jp_
Nov 29 '18 at 3:15
@Jp_ I don't see how it could be handled any differently though. JSON doesn't include any concept of keywords, so that information is necessarily lost when converting a Clojure map to a JSON representation. You could try dealing with Clojure's EDN instead of JSON, unless you need to interop with JS.
– Carcigenicate
Nov 29 '18 at 3:18
I need to interop with JS, what I'm doing is dumping a re-frame db into firebase realtime database. I thought would be a simple operation, but I'm not seeing a way out of it. Maybe keeping the information of what values are keywords somehow when using clj->js.
– Jp_
Nov 29 '18 at 3:29
2
@Jp_ Or just assume all converted map keys are strings. Keywords are nice to have, but they aren't necessary. If you're dealing with records or maps that for whatever reason are using keywords, you can easily create a function to convert the map into a JSON friendly format by using thename
function.
– Carcigenicate
Nov 29 '18 at 3:34
This might work, I'll let you know when I try.
– Jp_
Nov 29 '18 at 3:36
Yes.. my structure is very nested, but I'm realising my problem is more general. When I convert to json I loose the information which values were keywords, so there isn't a way I can tell even in a map if I have a keyword that is not the key of the map.
– Jp_
Nov 29 '18 at 3:15
Yes.. my structure is very nested, but I'm realising my problem is more general. When I convert to json I loose the information which values were keywords, so there isn't a way I can tell even in a map if I have a keyword that is not the key of the map.
– Jp_
Nov 29 '18 at 3:15
@Jp_ I don't see how it could be handled any differently though. JSON doesn't include any concept of keywords, so that information is necessarily lost when converting a Clojure map to a JSON representation. You could try dealing with Clojure's EDN instead of JSON, unless you need to interop with JS.
– Carcigenicate
Nov 29 '18 at 3:18
@Jp_ I don't see how it could be handled any differently though. JSON doesn't include any concept of keywords, so that information is necessarily lost when converting a Clojure map to a JSON representation. You could try dealing with Clojure's EDN instead of JSON, unless you need to interop with JS.
– Carcigenicate
Nov 29 '18 at 3:18
I need to interop with JS, what I'm doing is dumping a re-frame db into firebase realtime database. I thought would be a simple operation, but I'm not seeing a way out of it. Maybe keeping the information of what values are keywords somehow when using clj->js.
– Jp_
Nov 29 '18 at 3:29
I need to interop with JS, what I'm doing is dumping a re-frame db into firebase realtime database. I thought would be a simple operation, but I'm not seeing a way out of it. Maybe keeping the information of what values are keywords somehow when using clj->js.
– Jp_
Nov 29 '18 at 3:29
2
2
@Jp_ Or just assume all converted map keys are strings. Keywords are nice to have, but they aren't necessary. If you're dealing with records or maps that for whatever reason are using keywords, you can easily create a function to convert the map into a JSON friendly format by using the
name
function.– Carcigenicate
Nov 29 '18 at 3:34
@Jp_ Or just assume all converted map keys are strings. Keywords are nice to have, but they aren't necessary. If you're dealing with records or maps that for whatever reason are using keywords, you can easily create a function to convert the map into a JSON friendly format by using the
name
function.– Carcigenicate
Nov 29 '18 at 3:34
This might work, I'll let you know when I try.
– Jp_
Nov 29 '18 at 3:36
This might work, I'll let you know when I try.
– Jp_
Nov 29 '18 at 3:36
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53531174%2fhow-to-keep-keywords-in-arrays-when-converting-to-json-using-js-clj%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown