Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ring-clojure/ring-json
Ring middleware for handling JSON
https://github.com/ring-clojure/ring-json
clojure json middleware ring
Last synced: 4 days ago
JSON representation
Ring middleware for handling JSON
- Host: GitHub
- URL: https://github.com/ring-clojure/ring-json
- Owner: ring-clojure
- Created: 2012-07-16T12:23:39.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2021-06-09T19:33:32.000Z (over 3 years ago)
- Last Synced: 2024-12-13T16:47:02.619Z (12 days ago)
- Topics: clojure, json, middleware, ring
- Language: Clojure
- Size: 63.5 KB
- Stars: 318
- Watchers: 13
- Forks: 47
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Ring-JSON
[![Build Status](https://travis-ci.org/ring-clojure/ring-json.svg?branch=master)](https://travis-ci.org/ring-clojure/ring-json)
Standard Ring middleware functions for handling JSON requests and
responses.## Installation
To install, add the following to your project `:dependencies`:
[ring/ring-json "0.5.1"]
## Usage
#### wrap-json-response
The `wrap-json-response` middleware will convert any response with a
collection as a body (e.g. map, vector, set, seq, etc) into JSON:```clojure
(require '[ring.middleware.json :refer [wrap-json-response]]
'[ring.util.response :refer [response]])(defn handler [request]
(response {:foo "bar"}))(def app
(wrap-json-response handler))
```#### wrap-json-body
The `wrap-json-body` middleware will parse the body of any request
with a JSON content-type into a Clojure data structure, and assign it
to the `:body` key.This is the preferred way of handling JSON requests.
```clojure
(use '[ring.middleware.json :only [wrap-json-body]]
'[ring.util.response :only [response]])(defn handler [request]
(prn (get-in request [:body "user"]))
(response "Uploaded user."))(def app
(wrap-json-body handler {:keywords? true :bigdecimals? true}))
```#### wrap-json-params
The `wrap-json-params` middleware is an alternative to
`wrap-json-body` for when it's convenient to treat a JSON request as a
map of parameters. Rather than replace the `:body` key, the parsed
data structure will be assigned to the `:json-params`. The parameters
will also be merged into the standard `:params` map.```clojure
(require '[ring.middleware.json :refer [wrap-json-params]]
'[ring.util.response :refer [response]])(defn handler [request]
(prn (get-in request [:params "user"]))
(response "Uploaded user."))(def app
(wrap-json-params handler))
```Note that Ring parameter maps use strings for keys. For consistency,
this means that `wrap-json-params` does not have a `:keywords?`
option. Instead, use the standard Ring `wrap-keyword-params` function:```clojure
(require '[ring.middleware.keyword-params :refer [wrap-keyword-params]])(def app
(-> handler
wrap-keyword-params
wrap-json-params))
```## License
Copyright © 2021 James Reeves
Distributed under the MIT License, the same as Ring.