Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Few tweaks #3

Open
wants to merge 2 commits into from

1 participant

@kzar

Updated to use Clojure 1.3, stopped throwing exceptions on badly formed JSON and made existing params take precedence in the merge with JSON ones.

kzar added some commits
@kzar

Just made another small change, I added an optional json-key parameter. It lets you handle data that's not in a map structure properly. Say if the user just posts [1,2,3,4] it's nice to wrap it in a map before you merge with the parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 29, 2011
  1. @kzar

    Updated to use Clojure 1.3, stopped throwing exceptions on badly form…

    kzar authored
    …ed JSON and made existing params take precedence in the merge with JSON ones.
Commits on Sep 30, 2011
  1. @kzar
This page is out of date. Refresh to see the latest.
View
2  README.md
@@ -1,3 +1,5 @@
# `ring-json-params`
[Ring](http://github.com/mmcgrana/ring) middleware that augments `:params` according to a parsed JSON request body.
+
+Takes optional parameter `json-key`, if given wraps the JSON parameters in it's own map: `{json-key parsed-json}`. (Useful if the JSON expected does not merge into a map elegantly, for example an array of numbers.)
View
6 project.clj
@@ -1,5 +1,5 @@
-(defproject ring-json-params "0.1.3"
+(defproject ring-json-params "0.1.4"
:description "Ring middleware for JSON params parsing."
:dependencies
- [[org.clojure/clojure "1.2.0"]
- [clj-json "0.3.1"]])
+ [[org.clojure/clojure "1.3.0"]
+ [clj-json "0.4.3"]])
View
23 src/ring/middleware/json_params.clj
@@ -6,13 +6,16 @@
(if-let [#^String type (:content-type req)]
(not (empty? (re-find #"^application/(vnd.+)?json" type)))))
-(defn wrap-json-params [handler]
- (fn [req]
- (if-let [body (and (json-request? req) (:body req))]
- (let [bstr (slurp body)
- json-params (json/parse-string bstr)
- req* (assoc req
- :json-params json-params
- :params (merge (:params req) json-params))]
- (handler req*))
- (handler req))))
+(defn wrap-json-params
+ ([handler] (wrap-json-params handler nil))
+ ([handler json-key]
+ (fn [req]
+ (if-let [body (and (json-request? req) (:body req))]
+ (let [bstr (slurp body)
+ json-params (try (json/parse-string bstr) (catch Exception e nil))
+ json-params (if json-key {json-key json-params} json-params)
+ req* (assoc req
+ :json-params json-params
+ :params (merge json-params (:params req)))]
+ (handler req*))
+ (handler req)))))
View
3  test/ring/middleware/json_params_test.clj
@@ -1,7 +1,6 @@
(ns ring.middleware.json-params-test
(:use clojure.test)
(:use ring.middleware.json-params)
- (:require [clojure.contrib.io :as io])
(:import java.io.ByteArrayInputStream))
(defn stream [s]
@@ -15,7 +14,7 @@
:body (stream "<xml></xml>")
:params {"id" 3}}
resp (json-echo req)]
- (is (= "<xml></xml>") (io/slurp* (:body resp)))
+ (is (= "<xml></xml>") (slurp (:body resp)))
(is (= {"id" 3} (:params resp)))
(is (nil? (:json-params resp)))))
Something went wrong with that request. Please try again.