4

Do you know a function that takes a Clojure string and converts it to a map. For example, if the function gets the string

:first "John" :last "Lukas" :city "London"

it returns a map with the previous key-value relations.

1 Answer 1

7

You can use the clojure.end/read-string function for this. It makes sure that nobody injects code to your system, but otherwise will parse clojure data structures. Prepend "{" and append "}" to make it a map, that can be parsed that way:

(def stringtoparse ":first \"John\" :last \"Lukas\" :city \"London\"") 

(clojure.edn/read-string (str "{" stringtoparse "}"))
Sign up to request clarification or add additional context in comments.

7 Comments

When I use the module clojure.edn I get an error: ClassNotFoundException clojure.edn java.net.URLClassLoader.findClass (URLClassLoader.java:381) I use Clojure 1.7. Does this module exist only in Clojure 1.8 ? Is there anyway to use it in version 1.7 ?
What version of clojure are use using? The clojure.edn namespace is present since Clojure 1.5.
@CrazySynthax That would be the read-string method in clojure.core, which you should not use for reasons listed here and here.
@Mattias Wimmer, before typing clojure.edn/read-string I did: (require '[clojure.edn]) and then it passed.
If you are using read-string you have a security hole and your app will be pwn3e. @Elogent is completely correct here and it's important. If you dismiss this because you are sure the "special flag" is set to false, and you run this in production then you will randomly have a security hole sometime in the future.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.