On Mon, Jun 18, 2012 at 4:18 PM, Antix <matthias.kal...@googlemail.com>wrote:

> Hi Guys,
> I'm very new to clojure and searching for a way to convert a given
> String to a Hashmap as far as this is possible.
> I thought already about the use of a macro, but all the different
> quotes are a little bit confusing for me.
>
> Is it possible to create a Hashmap or some similar structure by using
> a given String.
>
> My Input-String is something like: :Name "John", :age 20, :gender
> "n"
> Is it possible to convert this to a hashmap similar to this:
> (def hashm {:Name "John", :age 20, :gender "n"  }) ?
>

Use the built in read-string function.:
(read-string (str "{" s "}")))

Don't use eval or load-string.  read-string is safer, faster, and won't hit
JVM size limits.

Also, if the input isn't trusted, you should bind *read-eval* to false, to
avoid being subject to running hostile user-supplied code:

(defn parse-map
  [s]
  (binding [*read-eval* false]
    (read-string (str "{" s "}"))))

-- 
Dave

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to