metosin/muuntaja

Name: muuntaja

Owner: Metosin

Description: Clojure library for fast http api format negotiation, encoding and decoding.

Created: 2016-08-13 11:01:21.0

Updated: 2018-05-18 21:01:23.0

Pushed: 2018-05-23 18:52:40.0

Homepage:

Size: 733

Language: Clojure

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Muuntaja Continuous Integration status

Clojure library for fast http format negotiation, encoding and decoding. Standalone library, but ships with adapters for ring (async) middleware & Pedestal-style interceptors. Explicit & extendable, supporting out-of-the-box JSON, EDN and Transit (both JSON & Msgpack). Ships with optional adapters for MessagePack and YAML.

Based on ring-middleware-format, but a complete rewrite (and up to 30x faster).

Rationale

Check the Wiki & api-docs for more details.

Latest version

Clojars Project

Muuntaja requires Java 1.8+

Quickstart
Ring
uire '[muuntaja.middleware :as middleware])

n echo [request]
status 200
body (:body-params request)})

th defaults
 app (middleware/wrap-format echo))

 {:headers
  {"content-type" "application/edn"
   "accept" "application/json"}
  :body "{:kikka 42}"})
status 200,
body #object[java.io.ByteArrayInputStream]
muuntaja/format "application/json",
headers {"Content-Type" "application/json; charset=utf-8"}}

There is a Ring guide in a wiki. See also differences to ring-middleware-format & ring-json if you are migrating from those.

Interceptors

See muuntaja.interceptor.

Standalone

Create a Muuntaja and use it to encode & decode JSON:

uire '[muuntaja.core :as m])

ith defaults
 m (muuntaja/create))

 {:kikka 42}
 (m/encode m "application/json")
 slurp)
 "{\"kikka\":42}"

 {:kikka 42}
 (m/encode m "application/json")
 (m/decode m "application/json"))
 {:kikka 42}

With custom EDN decoder opts:

 m
/create
(assoc-in
  m/default-options
  [:formats "application/edn" :decoder-opts]
  {:readers {'INC inc}})))

 "{:value #INC 41}"
 (m/decode m "application/edn"))
 {:value 42}

A function to encode Transit-json:

 encode-transit-json
/encoder m "application/transit+json"))

rp (encode-transit-json {:kikka 42}))
 "[\"^ \",\"~:kikka\",42]"
Streaming

Muuntaja ships with streaming encoders for both JSON & Transit. With these, the encoded data can be lazily written to provided OutputStream, avoiding intermediate byte-streams. These encoders return a muuntaja.protocols.StreamableResponse type, which satisifies the following protocols & interfaces:

uire '[muuntaja.format.json :as json-format])

 m
/create
(-> m/default-options
    json-format/with-streaming-json-format)))

 {:kikka 42}
 (m/encode m "application/json"))
StreamableResponse>>

 {:kikka 42}
 (m/encode m "application/json")
 slurp)
\"kikka\":42}"

 {:kikka 42}
 (m/encode m "application/json")
 (m/decode m "application/json"))
kikka 42}
HTTP format negotiation

HTTP format negotiation is done via request headers for both request (content-type, including the charset) and response (accept and accept-charset). With the default options, a full match on the content-type is required, e.g. application/json. Adding a :matches regexp for formats enables more loose matching. See Configuration wiki-page for more info.

Results of the negotiation are published into request & response under namespaced keys for introspection. These keys can also be set manually, overriding the content negotiation process.

Exceptions

When something bad happens, an typed exception is thrown. You should handle it elsewhere. Thrown exceptions have an ex-data with the following :type value (plus extra info to generate descriptive erros to clients):

Server Spec
Request
Response
Options
Default options
tp {:extract-content-type extract-content-type-ring
    :extract-accept-charset extract-accept-charset-ring
    :extract-accept extract-accept-ring
    :decode-request-body? (constantly true)
    :encode-response-body? encode-collections-with-override}

low-empty-input? true

fault-charset "utf-8"
arsets available-charsets

fault-format "application/json"
rmats {"application/json" json-format/json-format
       "application/edn" edn-format/edn-format
       "application/transit+json" transit-format/transit-json-format
       "application/transit+msgpack" transit-format/transit-msgpack-format}}
Profiling

YourKit supports open source projects with its full-featured Java Profiler. YourKit, LLC is the creator of YourKit Java Profiler and YourKit .NET Profiler, innovative and intelligent tools for profiling Java and .NET applications.

License
Picture

By Unknown. The drawing is signed “E. Ducretet”, indicating that the apparatus was made by Eugene Ducretet, a prominent Paris scientific instrument manufacturer and radio researcher. The drawing was undoubtedly originally from the Ducretet instrument catalog. [Public domain], via Wikimedia Commons.

Original Code (ring-middleware-format)

Copyright © 2011, 2012, 2013, 2014 Nils Grunwald
Copyright © 2015, 2016 Juho Teperi

This library

Copyright © 2016-2017 Metosin Oy

Distributed under the Eclipse Public License, the same as Clojure.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.