Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how would you guys implement auto body conversion? #29

Closed
boxxxie opened this issue Apr 15, 2013 · 6 comments
Closed

how would you guys implement auto body conversion? #29

boxxxie opened this issue Apr 15, 2013 · 6 comments

Comments

@boxxxie
Copy link

boxxxie commented Apr 15, 2013

something like (wrap-json-body)

@ordnungswidrig
Copy link
Member

Liberator has the concept of representations. In liberator.representation there is a Protocol Representation which defines a single method as-response. To convert a sequence to a json response the protocol would have to be implemented for sequences. The implementation then dispatches on the media-type to use clojure.data.json in the case that the negotiated media-type is "application/json".

Fortunately there are already implementations for strings, sequences, maps, etc. for the media types text/plain, csv, tsv, application/clojure and application/json in liberator.representation

A simple resource definition would look like this:

(defresource me-can-haz-json
  :available-media-types ["application/json"]
  :handle-ok ["me" "can" "be" "a" {:application "json"}])

In the examples there a more resources, e.g. here: https://github.com/clojure-liberator/liberator/blob/master/examples/clj/examples.clj#L123

@boxxxie
Copy link
Author

boxxxie commented Apr 15, 2013

i already know about this, as i have changed the clojure.data.json function to use the latest version of cheshire on the version of liberator that i have pulled down and have been playing with.

i want to know about dealing with the request.

@ordnungswidrig
Copy link
Member

I'm not sure if I understand what you want? Do you man to automatically parse the request body?

@boxxxie
Copy link
Author

boxxxie commented Apr 16, 2013

yes
it's a stream, but if the user tells me they are sending me json, then i can run that stream through cheshire.

@ordnungswidrig
Copy link
Member

The most natural implementation IMHO is to use the valid-content-header? decision point:

(defresource foo
:valid-content-header?
(fn [ctx](when %28.equalsIgnoreCase "application/json" %28get-in ctx [:request "content-type"]))
{::parsed-content (parse-stream (get-in ctx [:request :body]))})))

Of course there is room for some to-be-written helper functionsm e.g.:

(parse-body "application/json" chesire/parse-stream "application/clojure" safe-parse-with-clojure-reader)

I'd be happy if you would contribute something like this.

@boxxxie
Copy link
Author

boxxxie commented Apr 16, 2013

that looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants