Reader syntax for uri objects

149 views
Skip to first unread message

Taylor Sando

unread,
Jan 9, 2013, 8:20:53 PM1/9/13
to clo...@googlegroups.com
I am looking for a way to transfer URI objects from a clojure client to a clojurescript client.  The printed representation of a java.net.URI object is  #<URI "http//www.example.com>  The problem is that I can't read in this data when it's structured like that.  For example, I'd like to be able to pass this data strucutre to the client {:uri #<URI http://www.google.com>}.  From my understanding, the extensible reader needs something in the form of #symbol [value].  

I know how to extend the clojurescript reader:

(defn make-url [stuff]
  (str (first stuff)))

(reader/register-tag-parser! 'URI make-url)

(assert (=  "http://www.google.com" (reader/read-string "#URI [http://www.google.com]")))

I know that I could just map over the data structure and convert the uris to strings, and then send that to the client, but it seems like there should be a better way.

Dave Sann

unread,
Jan 9, 2013, 8:35:31 PM1/9/13
to clo...@googlegroups.com
why not just print/send your uris as strings?

you only need a reader tag if you want to read/interpret it in a particular way at read time in the client. do you want to do this?

D

Taylor Sando

unread,
Jan 9, 2013, 8:42:33 PM1/9/13
to clo...@googlegroups.com
The URIs are coming from a datomic database that actually stores the values as java.net.URIs.  All I'm doing is transferring the query values from the database to the client.  The server uses pr-str before sending back the results, and the client uses reader/read-string to get the results.  

Dave Sann

unread,
Jan 9, 2013, 8:51:11 PM1/9/13
to clo...@googlegroups.com
It's not going to be easy to read the #<URI form in clojurescript I think.

On the server, I would extend the the print-string (I think its called this - you will need to look at the core source code) multi-method. You can use this to make java....URI print as a string or as a tagged literal if you need it.

D

Taylor Sando

unread,
Jan 9, 2013, 9:15:05 PM1/9/13
to clo...@googlegroups.com

Here is what I came up with (defmethod print-method java.net.URI [x w] (print-method (symbol (format "#uri [%s]" (symbol (str x)))) w))

(pr-str (java.net.URI. "www.google.com"))  

If used with the cljs parser from above, it works.  Thanks
Reply all
Reply to author
Forward
0 new messages