Giter VIP home page Giter VIP logo

Comments (6)

ordnungswidrig avatar ordnungswidrig commented on May 21, 2024

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

from liberator.

boxxxie avatar boxxxie commented on May 21, 2024

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.

from liberator.

ordnungswidrig avatar ordnungswidrig commented on May 21, 2024

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

from liberator.

boxxxie avatar boxxxie commented on May 21, 2024

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

from liberator.

ordnungswidrig avatar ordnungswidrig commented on May 21, 2024

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.

from liberator.

boxxxie avatar boxxxie commented on May 21, 2024

that looks good.

from liberator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.