### Add a Format using m/install Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/configuration.md Demonstrates how to add a new format, such as MessagePack, to Muuntaja's configuration using the `m/install` function. This example shows the resulting set of formats after installation. ```clojure (require '[muuntaja.format.msgpack :as msgpack]) (def m (m/create (m/install m/default-options msgpack/format))) (m/formats m) ; => #{"application/json" "application/edn" "application/transit+json" ; "application/transit+msgpack" "application/msgpack"} ``` -------------------------------- ### Create Muuntaja Instance with Defaults Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/configuration.md Create a Muuntaja instance using default configuration options. This is the simplest way to get started. ```clojure (require '[muuntaja.core :as m]) ; Create with defaults (def m (m/create)) ``` -------------------------------- ### Custom Format Creation and Installation Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/types.md Demonstrates how to create a custom format using the Format record and install it into a Muuntaja instance. This involves defining the format's name, encoder/decoder factories, matching pattern, and return type. ```clojure (require '[muuntaja.core :as m] '[muuntaja.format.core :as core]) ; View a built-in format (def json-fmt (get (m/options (m/create)) :formats) (m/default-options)) (get-in json-fmt ["application/json"]) ; Create custom format (def custom-format (core/map->Format {:name "application/custom" :encoder [encoder-factory {:option "value"}] :decoder [decoder-factory {:option "value"}] :matches (re-pattern ".*custom.*") :return :bytes})) ; Install it (def m-custom (m/create (m/install m/default-options custom-format))) ``` -------------------------------- ### install Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/core.md Adds a new format to the options map. Can optionally specify a name for the format. ```APIDOC ## install ### Description Adds a new format to the options. ### Function Signature ```clojure (defn install ([options format] ...) ([options format name] ...)) ``` ### Parameters #### Parameters - **options** (Map) - Required - Options map - **format** (Map) - Required - Format specification with optional `:name` key - **name** (String) - Optional - Default: From format - Format identifier to register ### Returns Map. Updated options with the new format installed. ``` -------------------------------- ### Creating a format-negotiate-interceptor Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/interceptor.md Example of creating an instance of the format-negotiate-interceptor. ```clojure (require '[muuntaja.interceptor :as interceptor]) (def negotiate-interceptor (interceptor/format-negotiate-interceptor)) ``` -------------------------------- ### Set Version and Install Modules Source: https://github.com/metosin/muuntaja/blob/master/CONTRIBUTING.md Scripts to manage project versions and install modules. Ensure modules are installed after making changes. ```sh ./scripts/set-version 1.0.0 ``` ```sh ./scripts/lein-modules install ``` -------------------------------- ### Run Local nREPL Source: https://github.com/metosin/muuntaja/blob/master/CONTRIBUTING.md Starts a local nREPL environment for development. Requires modules to be installed if changes have been made. ```sh lein with-profile default,dev repl ``` -------------------------------- ### Install Protocol Buffering Support (MsgPack) Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/configuration.md Installs MsgPack format support for Muuntaja, enabling handling of binary data formats. ```clojure (require '[muuntaja.format.msgpack :as msgpack]) (def m (m/create (m/install m/default-options msgpack/format))) ``` -------------------------------- ### YAML Encoding and Decoding Example Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/formats.md Example demonstrating how to create a Muuntaja instance with YAML format and encode data. ```clojure (require '[muuntaja.core :as m] '[muuntaja.format.yaml :as yaml]) (def m (m/create (m/install m/default-options yaml/format))) (m/encode m "application/x-yaml" {:key "value"}) ``` -------------------------------- ### Params Interceptor Initialization Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/interceptor.md Provides an example of how to initialize the `params-interceptor` using `muuntaja.interceptor/params-interceptor`. ```clojure (require '[muuntaja.interceptor :as interceptor]) (def params-interceptor (interceptor/params-interceptor)) ``` -------------------------------- ### Form URL-Encoded Encoding and Decoding Example Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/formats.md Example demonstrating how to create a Muuntaja instance with form URL-encoded format and perform encoding and decoding. ```clojure (require '[muuntaja.core :as m] '[muuntaja.format.form :as form]) (def m (m/create (m/install m/default-options form/format))) (m/encode m "application/x-www-form-urlencoded" {:username "test" :password "pass"}) ; => #object[java.io.ByteArrayInputStream] (slurp *) ; => "username=test&password=pass" (m/decode m "application/x-www-form-urlencoded" "username=test&password=pass") ; => {:username "test" :password "pass"} ``` -------------------------------- ### Install MessagePack Format Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/formats.md Add the MessagePack module to your project.clj dependencies. ```clojure [metosin/muuntaja-msgpack "0.6.11"] ``` -------------------------------- ### Create Muuntaja Instance with MessagePack and Encode Example Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/formats.md Create a Muuntaja instance with MessagePack format and demonstrate encoding a map. ```clojure (require '[muuntaja.core :as m] '[muuntaja.format.msgpack :as msgpack]) (def m (m/create (m/install m/default-options msgpack/format))) (m/encode m "application/msgpack" {:key "value"}) ``` -------------------------------- ### Format-Specific Options Example Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/configuration.md Demonstrates how to define format-specific options like encoders, decoders, return types, and matching regex within the :formats configuration. ```clojure { :formats {"application/json" {:name "application/json" :encoder [encoder-factory {:option "value"}] :decoder [decoder-factory {:option "value"}] :return :bytes :matches #".*json.*" } ... } } ``` -------------------------------- ### Using format-interceptor with Default Instance Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/interceptor.md Example of creating a format-interceptor using the default Muuntaja instance. ```clojure (require '[muuntaja.interceptor :as interceptor] '[muuntaja.core :as m]) ; With default instance (def format-interceptor (interceptor/format-interceptor)) ``` -------------------------------- ### Adapter Usage Example Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/types.md Illustrates how to retrieve and use an adapter for a specific format, such as JSON. It shows checking for the existence of encoder and decoder functions and directly using the encoder. ```clojure (require '[muuntaja.core :as m]) (def m-instance (m/create)) (def adapters (m/adapters m-instance)) ; Get adapter for JSON format (def json-adapter (get adapters "application/json")) ; Check if both encoder and decoder exist (and (:encoder json-adapter) (:decoder json-adapter)) ; => true ; Use the encoder directly ((:encoder json-adapter) {:key "value"} "utf-8") ; => #object[java.io.ByteArrayInputStream ...] ``` -------------------------------- ### Create Muuntaja Instance with Cheshire JSON Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/formats.md Create a Muuntaja instance and install the Cheshire JSON format for 'application/json'. ```clojure (require '[muuntaja.core :as m] '[muuntaja.format.cheshire :as cheshire]) (def m (m/create (m/install m/default-options cheshire/format "application/json"))) ``` -------------------------------- ### JSON Encoding and Decoding Example Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/formats.md Demonstrates basic JSON encoding and decoding using the default JSON format. Ensure the jsonista library is available. ```clojure (require '[muuntaja.core :as m] '[muuntaja.format.json :as json]) (def encoded (m/encode "application/json" {:key "value"})) (m/slurp encoded) ; => "{\"key\":\"value\"}" (m/decode "application/json" "{\"key\":\"value\"}") ; => {:key "value"} ``` -------------------------------- ### Integrating format-interceptor with Pedestal Routes Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/interceptor.md Example demonstrating how to include format-interceptor within Pedestal HTTP routes. ```clojure (require '[muuntaja.interceptor :as interceptor] '[io.pedestal.http :as http]) (def routes [["/" :get {:name :home :interceptors [format-interceptor (interceptor/params-interceptor) (interceptor/exception-interceptor)] :handler handler}]]) ``` -------------------------------- ### Install Cheshire JSON Format Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/formats.md Add the Cheshire JSON module to your project.clj dependencies. ```clojure [metosin/muuntaja-cheshire "0.6.11"] ``` -------------------------------- ### Install Cheshire JSON Encoder Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/configuration.md Installs the Cheshire library as a JSON encoder for Muuntaja, allowing for specific JSON encoding needs. ```clojure (require '[muuntaja.format.cheshire :as cheshire]) (def m (m/create (m/install m/default-options cheshire/format "application/json"))) ``` -------------------------------- ### Creating a format-request-interceptor Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/interceptor.md Example of creating an instance of the format-request-interceptor. This interceptor requires :muuntaja/request to be set, typically by format-negotiate-interceptor. ```clojure (require '[muuntaja.interceptor :as interceptor]) ; Must be used with format-negotiate-interceptor (def request-interceptor (interceptor/format-request-interceptor)) ``` -------------------------------- ### Using format-interceptor with Custom Options Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/interceptor.md Example of creating a format-interceptor with custom Muuntaja options, such as setting a default format. ```clojure (require '[muuntaja.interceptor :as interceptor] '[muuntaja.core :as m]) ; With custom options (def custom-options (assoc m/default-options :default-format "application/edn")) (def format-interceptor-custom (interceptor/format-interceptor custom-options)) ``` -------------------------------- ### Example: Muuntaja with Transit JSON Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/formats.md Demonstrates creating a Muuntaja instance configured for Transit JSON and encoding a Clojure map to Transit JSON. ```clojure (require '[muuntaja.core :as m]) (def m-json (m/create (m/select-formats m/default-options ["application/transit+json"]))) (m/encode m-json "application/transit+json" {:key "value"}) ``` -------------------------------- ### Request Format Negotiation Example Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/types.md Demonstrates how to use `muuntaja.core/request-format` to negotiate the request format and charset from HTTP headers. Shows how to access the negotiated format and charset from the resulting FormatAndCharset record. ```clojure (require '[muuntaja.core :as m]) (def req-format (m/request-format (m/create) {:headers {"content-type" "application/json; charset=utf-8"}})) ; => #FormatAndCharset{:format "application/json" ; :charset "utf-8" ; :raw-format "application/json"} (:format req-format) ; => "application/json" (:charset req-format) ; => "utf-8" ``` -------------------------------- ### Install Muuntaja YAML Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/formats.md Add the Muuntaja YAML module to your project.clj file for YAML serialization support. ```clojure [metosin/muuntaja-yaml "0.6.11"] ``` -------------------------------- ### Install Muuntaja Form Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/formats.md Add the Muuntaja Form module to your project.clj file for form URL-encoded data support. ```clojure [metosin/muuntaja-form "0.6.11"] ``` -------------------------------- ### Install New Format Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/core.md Adds a new format specification to the options map. You can provide a format map directly or specify a name for the format identifier. ```clojure (defn install ([options format] ...) ([options format name] ...)) ``` -------------------------------- ### Example: Negotiate Request Format and Charset Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/core.md Demonstrates how to use Muuntaja to determine the negotiated request format and charset from an HTTP request's content-type header. ```clojure (require '[muuntaja.core :as m]) ; Create a request with content-type header (def request {:headers {"content-type" "application/json; charset=utf-8"}}) ; Get negotiated request format (m/request-format (m/create) request) ; => #FormatAndCharset{:format "application/json", :charset "utf-8", :raw-format "application/json"} ``` -------------------------------- ### EDN Encoding and Decoding Example Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/formats.md Shows how to encode and decode data using the EDN format. This is suitable for Clojure's native data representation. ```clojure (require '[muuntaja.core :as m]) (m/encode "application/edn" {:key 42}) (m/slurp *) ; => "{:key 42}" (m/decode "application/edn" "{:key 42}") ; => {:key 42} ``` -------------------------------- ### Custom Exception Interceptor Example Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/interceptor.md Demonstrates how to create a custom exception interceptor using `muuntaja.interceptor/exception-interceptor` with a user-defined error handling function. ```clojure (require '[muuntaja.interceptor :as interceptor]) (defn custom-error [exception format request] {:status 400 :headers {"Content-Type" "application/json"} :body (str "{\"error\":\"Malformed " format " request\"}")}) (def exception-interceptor (interceptor/exception-interceptor custom-error)) ``` -------------------------------- ### Test Muuntaja Header Parsing Functions Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/parsing.md Provides examples for testing the parse-content-type, parse-accept, and parse-accept-charset functions from the muuntaja.parse library. ```clojure (require '[muuntaja.parse :as parse]) ; Test parse-content-type (assert (= (parse/parse-content-type "application/json; charset=utf-8") ["application/json" "utf-8"])) ; Test parse-accept (assert (contains? (set (parse/parse-accept "application/json, application/edn")) "application/json")) ; Test parse-accept-charset (assert (contains? (set (parse/parse-accept-charset "utf-8, iso-8859-1")) "utf-8")) ``` -------------------------------- ### Import Optional Format Modules Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/INDEX.md Import optional format modules for Muuntaja. These provide support for additional data formats and may require separate installation. ```clojure ; Optional modules (require '[muuntaja.format.form :as form]) (require '[muuntaja.format.msgpack :as msgpack]) (require '[muuntaja.format.yaml :as yaml]) (require '[muuntaja.format.cheshire :as cheshire]) (require '[muuntaja.format.charred :as charred]) ``` -------------------------------- ### Context Transformation Example Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/interceptor.md Illustrates the transformation of the request and response context before and after the interceptor is applied. It shows how the response body is encoded and the Content-Type header is added. ```clojure {:request {:muuntaja/response #FormatAndCharset{:format "application/edn" :charset "utf-8"}} :response {:status 200 :body {:data "value"}}} ↓ {:response {:status 200 :body :headers {"Content-Type" "application/edn; charset=utf-8"}}} ``` -------------------------------- ### Interceptor Stack with Sieppari Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/interceptor.md Demonstrates integrating Muuntaja interceptors into a handler executed by Sieppari. This setup includes exception, format, and parameter interceptors. ```clojure (require '[muuntaja.interceptor :as interceptor] '[sieppari.core :as sieppari]) (def interceptors [(interceptor/exception-interceptor) (interceptor/format-interceptor) (interceptor/params-interceptor)]) (defn handler [ctx] {:status 200 :body (-> ctx :request :body-params)}) (defn response [ctx] (:response ctx)) (sieppari/execute handler response interceptors) ``` -------------------------------- ### Get All Supported Formats Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/core.md Returns the set of all formats (both encodable and decodable) that the Muuntaja instance supports. This provides a comprehensive list of supported formats. ```clojure (defn formats [m] ...) ``` ```clojure (require '[muuntaja.core :as m]) (formats (m/create)) ; => #{"application/json" "application/edn" "application/transit+json" "application/transit+msgpack"} ``` -------------------------------- ### Format Configuration Error Example Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/errors.md This Clojure snippet demonstrates how a format configuration error, such as providing an invalid encoder that doesn't satisfy the protocol, can be caught. The exception data will contain details about the error type and format. ```clojure (try (m/create (assoc-in m/default-options [:formats "application/json" :encoder] invalid-encoder)) ; Doesn't satisfy protocol (catch Exception e (ex-data e) ; => {:type :muuntaja/... :format "application/json" ...} )) ``` -------------------------------- ### Triggering :muuntaja/decode Error: Unsupported Charset Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/errors.md This example demonstrates how an unsupported charset during decoding can lead to a :muuntaja/decode exception. Ensure that the specified charset is available in the Muuntaja options. ```clojure (m/decode (m/create) "application/json" data "invalid-charset") ; => Exception (charset not in available charsets) ``` -------------------------------- ### Response Charset Negotiation Failure Example Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/errors.md Illustrates a scenario where a client's Accept-Charset header cannot be satisfied by the server's supported charsets, leading to an exception during response encoding. This occurs when no matching charset is found and no default charset is configured. ```clojure ; Client sends: Accept-Charset: iso-8859-1 only ; Server has: charsets: #{"utf-8"} ; And no default-charset configured ; => Exception during response encoding ``` ```clojure (def m (m/create (assoc m/default-options :charsets #{} :default-charset nil))) ; Response encoding triggers negotiation failure ``` -------------------------------- ### Catch and Log Decode Errors Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/errors.md A basic example of how to catch a general Exception and extract specific information, like the format, from the ex-data when a :muuntaja/decode error occurs. ```clojure (try (m/decode "application/json" "{bad}") (catch Exception e (println "Decode error:" (-> e ex-data :format)))) ``` -------------------------------- ### Defining and Registering a Custom Format Source: https://github.com/metosin/muuntaja/blob/master/doc/Creating-new-formats.md An example of creating a new format, 'streaming-upper-case-json-format', which decodes JSON with uppercase keys and encodes using a streaming encoder. This format is then registered with Muuntaja. ```clojure (require '[muuntaja.format.json :as json-format]) (require '[clojure.string :as str]) (require '[muuntaja.core :as muuntaja]) (def streaming-upper-case-json-format {:decoder [json-format/make-json-decoder {:keywords? false, :key-fn str/upper-case}] :encoder [json-format/make-streaming-json-encoder]}) (def m (muuntaja/create (assoc-in muuntaja/default-options [:formats "application/upper-json"] streaming-upper-case-json-format))) ``` ```clojure (->> {:kikka 42} (muuntaja/encode m "application/json") (muuntaja/decode m "application/upper-json")) ``` -------------------------------- ### Validating Muuntaja Configuration at Startup Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/errors.md Validate Muuntaja options during application startup to catch configuration errors early. This prevents runtime issues by ensuring the configuration is valid before the application starts processing requests. ```clojure (require '[muuntaja.core :as m]) (defn validate-muuntaja [options] (try (m/create options) (catch Exception e (println "Invalid Muuntaja configuration:" (ex-message e)) (throw e)))) ; At startup (def m (validate-muuntaja my-options)) ``` -------------------------------- ### View core API documentation Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/MANIFEST.txt Use this command to view the documentation for the core API functions. ```bash less -R api-reference/core.md ``` -------------------------------- ### View index file Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/MANIFEST.txt Use this command to view the main index file of the documentation. ```bash less -R INDEX.md ``` -------------------------------- ### Check configuration options Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/MANIFEST.txt This command is used to find and view the documented configuration options for Muuntaja. ```bash grep "^### :" configuration.md ``` -------------------------------- ### Install Charred JSON Format Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/formats.md Add the Charred JSON module to your project.clj dependencies. ```clojure [fi.metosin/muuntaja-charred "0.6.11"] ``` -------------------------------- ### Create Muuntaja Instance Source: https://github.com/metosin/muuntaja/blob/master/doc/Configuration.md Demonstrates creating a Muuntaja instance with defaults, from a prototype, or with default options. ```clojure (require '[muuntaja.core :as muuntaja]) ;; with defaults (def m (muuntaja/create)) ;; with a muuntaja as prototype (no-op) (muuntaja/create m) ;; with default options (same features, different instance) (muuntaja/create muuntaja/default-options) ``` -------------------------------- ### Format Management Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/INDEX.md Functions for querying available formats, managing format installations, and transforming format specifications. ```APIDOC ## Format Management - `muuntaja.core/formats` - Get available formats - `muuntaja.core/encodes` - Get encodable formats - `muuntaja.core/decodes` - Get decodable formats - `muuntaja.core/install` - Add format to instance - `muuntaja.core/select-formats` - Keep only specific formats - `muuntaja.core/transform-formats` - Modify all formats ``` -------------------------------- ### View Documentation with More Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/INDEX.md Use the `more` command to view markdown files in the terminal. ```bash # View with more more README.md ``` -------------------------------- ### Get Default Format Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/core.md Returns the default format of the Muuntaja instance. This is the format used when none is explicitly specified. ```clojure (defn default-format [m] ...) ``` ```clojure (require '[muuntaja.core :as m]) (default-format (m/create)) ; => "application/json" ``` -------------------------------- ### Get Supported Charsets Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/configuration.md Retrieves the set of supported charsets for Muuntaja. By default, this includes all charsets available on the JVM. ```clojure ; Use all available charsets (default) (m/create m/default-options) ``` -------------------------------- ### Muuntaja Protocol Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/types.md Protocol for querying a Muuntaja instance, providing methods to get encoders, decoders, adapters, and options. ```APIDOC ## Muuntaja Protocol ### Description Protocol for querying a Muuntaja instance. ### Methods - `encoder [this format]` - `decoder [this format]` - `adapters [this]` - `options [this]` ### Returns - `encoder`: Function or nil - `decoder`: Function or nil - `adapters`: Map of {format-string → Adapter} - `options`: Map of configuration options ### Satisfied By - Objects created by `muuntaja.core/create` ``` -------------------------------- ### Get Format Matchers Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/core.md Returns a map of formats to their regexp matchers for loose content-type matching. By default, this map is empty. ```clojure (defn matchers [m] ...) ``` ```clojure (require '[muuntaja.core :as m]) (matchers (m/create)) ; => {} (empty by default, no matchers configured) ``` -------------------------------- ### List API functions Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/MANIFEST.txt This command helps in listing all the API functions documented in the core API files. ```bash grep "^### " api-reference/*.md ``` -------------------------------- ### Get Decodable Formats Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/core.md Returns the set of formats that the Muuntaja instance can decode. Useful for understanding the decoding capabilities of a Muuntaja instance. ```clojure (defn decodes [m] ...) ``` ```clojure (require '[muuntaja.core :as m]) (decodes (m/create)) ; => #{"application/json" "application/edn" "application/transit+json" "application/transit+msgpack"} ``` -------------------------------- ### Validate Muuntaja Configuration at Startup Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/configuration.md Wrap the `m/create` call in a try-catch block to validate your custom options before the Muuntaja instance is created. This helps catch configuration errors early. ```clojure (try (def m (m/create my-options)) (catch Exception e (println "Configuration error:" (ex-message e)) (println "Details:" (ex-data e)))) ``` -------------------------------- ### Create a Muuntaja Instance Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/README.md Create a Muuntaja instance with default settings. This instance will hold supported formats and their encoder/decoder pairs. ```clojure (require '[muuntaja.core :as m]) (def m (m/create)) ; Uses defaults ``` -------------------------------- ### Add Optional Format to Muuntaja Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/README.md Add an optional format, such as MessagePack, to a Muuntaja instance. This requires installing the format using the provided module. ```clojure (require '[muuntaja.format.msgpack :as msgpack]) (def m (m/create (m/install m/default-options msgpack/format))) ``` -------------------------------- ### Get Encodable Formats Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/core.md Returns the set of formats that the Muuntaja instance can encode. Use this to determine which formats a Muuntaja instance supports for encoding. ```clojure (defn encodes [m] ...) ``` ```clojure (require '[muuntaja.core :as m]) (encodes (m/create)) ; => #{"application/json" "application/edn" "application/transit+json" "application/transit+msgpack"} ``` -------------------------------- ### Using Function Generators for Decoders Source: https://github.com/metosin/muuntaja/blob/master/doc/Creating-new-formats.md Demonstrates how to use function generators for decoders, with and without default options. ```clojure {:decoder [json-format/make-json-decoder]} ``` ```clojure {:decoder [formats/make-json-decoder {:key-fn true}]} ``` -------------------------------- ### Interceptor Stack with Pedestal Routes Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/interceptor.md Shows how to define Pedestal routes with Muuntaja interceptors for exception handling, format processing, and parameter merging. Assumes a handler function is defined elsewhere. ```clojure (require '[io.pedestal.http :as http] '[io.pedestal.http.route :as route] '[muuntaja.interceptor :as interceptor]) (def routes [["/" :get {:name :home :interceptors [(interceptor/exception-interceptor) (interceptor/format-interceptor) (interceptor/params-interceptor)] :handler handler}]]) (def service {::http/routes routes ::http/type :jetty ::http/port 3000}) ``` -------------------------------- ### Get Default Charset Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/configuration.md Retrieves the default charset used by Muuntaja when none is specified in headers. The default value is `"utf-8"`. ```clojure (m/default-charset (m/create)) ; => "utf-8" ``` -------------------------------- ### View Documentation with Less Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/INDEX.md Use the `less` command with the `-R` flag to view markdown files in the terminal with color support. ```bash # View with less (with color support) less -R README.md ``` -------------------------------- ### Import Core Functionality Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/INDEX.md Import the core Muuntaja functionality. This is a primary entry point for using the library. ```clojure ; Core functionality (require '[muuntaja.core :as m]) ``` -------------------------------- ### Search all documentation files Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/MANIFEST.txt This command allows you to search for specific terms across all markdown files in the documentation. ```bash grep -r "wrap-format" *.md api-reference/ ``` -------------------------------- ### Basic Ring Application with Default Formats Source: https://github.com/metosin/muuntaja/blob/master/doc/With-Ring.md Wrap a Ring handler with `wrap-format` to enable automatic JSON, EDN, and Transit request and response formatting using default Muuntaja options. ```clojure (require '[muuntaja.middleware :as mw]) (defn handler [_] {:status 200 :body {:ping "pong"}}) ;; with defaults (def app (mw/wrap-format handler)) (def request {:headers {"accept" "application/json"}}) (->> request app) ; {:status 200, ; :body #object[java.io.ByteArrayInputStream 0x1d07d794 "java.io.ByteArrayInputStream@1d07d794"], ; :headers {"Content-Type" "application/json; charset=utf-8"}} (->> request app :body slurp) ; "{\"ping\":\"pong\"}" ``` -------------------------------- ### format-interceptor Context Transformation (Enter) Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/interceptor.md Illustrates the context transformation during the enter phase (request processing) when using format-interceptor. ```clojure {:request {:headers {"content-type" "application/json" "accept" "application/edn"}}} ↓ {:request {:headers {...} :muuntaja/request #FormatAndCharset{:format "application/json" :charset "utf-8"} :muuntaja/response #FormatAndCharset{:format "application/edn" :charset "utf-8"} :body-params {:decoded "data"}}} ``` -------------------------------- ### Handling :muuntaja/response-format-negotiation Exception Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/errors.md Provides an example of catching and handling the :muuntaja/response-format-negotiation exception. The handler returns a 406 status with a body listing the supported formats. ```clojure (try ; Handler with response encoding (m/format-response m request response) (catch Exception e (let [{:keys [type formats]} (ex-data e)] (when (= type :muuntaja/response-format-negotiation) {:status 406 :body (str "Supported formats: " formats)})))) ``` -------------------------------- ### Use Only Selected Formats (Initial Attempt) Source: https://github.com/metosin/muuntaja/blob/master/doc/Configuration.md Attempt to restrict supported formats, which may lead to an invalid default format error if not handled correctly. ```clojure (def m (muuntaja/create (-> muuntaja/default-options (update :formats select-keys ["application/edn" "application/transit+json"])))) ;; clojure.lang.ExceptionInfo Invalid default format application/json ``` -------------------------------- ### Customize JSON Encoding in Muuntaja Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/README.md Customize the JSON encoder options, for example, to specify a date format. This is done by updating the :encoder-opts for the JSON format. ```clojure (def m (m/create (assoc-in m/default-options [:formats "application/json" :encoder-opts] {:date-format "yyyy-MM-dd"}))) ``` -------------------------------- ### Generate HTML Documentation with Pandoc Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/INDEX.md Use `pandoc` to generate an HTML document from all markdown files in the current directory and the `api-reference` subdirectory. ```bash # With pandoc pandoc *.md api-reference/*.md -o muuntaja-reference.html ``` -------------------------------- ### Get Default Format Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/configuration.md Retrieves the default format identifier used by Muuntaja when content negotiation fails. The default value is `"application/json"`. ```clojure (m/default-format (m/create)) ; => "application/json" ``` -------------------------------- ### Generate PDF Documentation with Pandoc Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/INDEX.md Use `pandoc` to generate a PDF document from all markdown files in the current directory and the `api-reference` subdirectory. ```bash # With pandoc pandoc *.md api-reference/*.md -o muuntaja-reference.pdf ``` -------------------------------- ### Generate HTML Documentation with markdown2html Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/INDEX.md Use the `markdown2html` Python module to convert markdown files to HTML. ```bash # With markdown2html python -m markdown *.md > out.html ``` -------------------------------- ### Complete Middleware Stack with Exception Handling Source: https://github.com/metosin/muuntaja/blob/master/doc/With-Ring.md Construct a comprehensive Ring middleware stack including parameter parsing, request/response formatting, exception handling, and format negotiation using Muuntaja. ```clojure (-> app ;; support for `:params` (mw/wrap-params) ;; format the request (mw/wrap-format-request muuntaja) ;; catch exceptions (mw/wrap-exceptions my-exception-handler) ;; format the response (mw/wrap-format-response muuntaja) ;; negotiate the request & response formats (mw/wrap-format-negotiate muuntaja)) ``` -------------------------------- ### Get Response Format and Charset Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/core.md Retrieves the negotiated format and charset for a response from a Ring request map. This helps in setting the appropriate Content-Type header for the response. ```clojure (defn get-response-format-and-charset [request] ...) ``` -------------------------------- ### Get Request Format and Charset Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/core.md Retrieves the negotiated format and charset from a Ring request map. Use this to determine the client's preferred content type. ```clojure (defn get-request-format-and-charset [request] ...) ``` -------------------------------- ### Select Formats with `select-formats` Source: https://github.com/metosin/muuntaja/blob/master/doc/Configuration.md Use the `select-formats` helper function to choose specific formats and set the default format. ```clojure (def m (muuntaja/create (muuntaja/select-formats muuntaja/default-options ["application/json" "application/edn"]))) (muuntaja/default-format m) ; "application/edn" ``` -------------------------------- ### Create Response Interceptor Instance Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/interceptor.md Shows how to create an instance of the response interceptor using the default prototype. This interceptor is then used to encode response bodies. ```clojure (require '[muuntaja.interceptor :as interceptor]) (def response-interceptor (interceptor/format-response-interceptor)) ``` -------------------------------- ### Set Custom Default Charset Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/configuration.md Sets a custom default charset for Muuntaja. This charset will be used when no charset is specified in headers. The example sets the default to `"iso-8859-1"`. ```clojure (def m (m/create (assoc m/default-options :default-charset "iso-8859-1"))) (m/default-charset m) ; => "iso-8859-1" ``` -------------------------------- ### Ensuring Formats and Default Format for Prevention Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/errors.md Demonstrates how to prevent response format negotiation failures. This involves ensuring that formats are available (e.g., using default options) and providing a `:default-format` to handle Accept header mismatches gracefully. ```clojure ; Default options already include formats (m/create) ; Safe - has json, edn, transit ; Custom options need explicit formats (m/create {:formats {"application/json" json-format/format ...} :default-format "application/json"}) ``` ```clojure ; With default, Accept header mismatches fall back gracefully (m/create (assoc m/default-options :default-format "application/json")) ``` -------------------------------- ### Triggering :muuntaja/request-charset-negotiation: Unsupported Charset Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/errors.md This example shows how specifying a charset that is not included in the Muuntaja options (e.g., :charsets #{"utf-8"}) during encoding will trigger a :muuntaja/request-charset-negotiation error. ```clojure (def m (m/create (assoc m/default-options :charsets #{"utf-8"}))) (m/encode m "application/json" {:x 1} "iso-8859-1") ; => Exception: requested charset not in :charsets ``` -------------------------------- ### Generate PDF Documentation with markdown-pdf Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/INDEX.md Use the `markdown-pdf` npm package to generate a PDF document from all markdown files in the current directory and the `api-reference` subdirectory. ```bash # With markdown-pdf npm package markdown-pdf *.md api-reference/*.md ``` -------------------------------- ### Set Custom Default Format Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/configuration.md Sets a custom default format identifier for Muuntaja, which is used as a fallback when content negotiation fails. The example sets the default to `"application/edn"`. ```clojure (def m (m/create (assoc m/default-options :default-format "application/edn"))) ``` -------------------------------- ### Inspect Muuntaja Configuration Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/configuration.md Use these functions to inspect the created Muuntaja instance and understand its configuration, including available formats, charsets, and options. ```clojure (require '[muuntaja.core :as m]) (def m (m/create)) ; Inspect what was created (m/formats m) (m/charsets m) (m/default-charset m) (m/default-format m) (m/decodes m) (m/encodes m) ; Check options (keys (m/options m)) ``` -------------------------------- ### create Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/core.md Creates a new Muuntaja instance from options, an existing Muuntaja, or default options. It supports creating with defaults, custom options, or by passing an existing instance for idempotency. ```APIDOC ## create ### Description Creates a new Muuntaja instance from options, an existing Muuntaja, or default options. ### Function Signature ```clojure (defn create ([] ...) ([muuntaja-or-options] ...)) ``` ### Parameters #### Function Parameters - **muuntaja-or-options** (Muuntaja or Map) - Optional - Default: `default-options` - Existing instance or options map ### Returns Muuntaja. A new or existing Muuntaja instance. ### Throws - `ex-info` (`:type` = `:muuntaja/format-creation`) - Invalid format specification in options - `ex-info` - Invalid default format specified ### Example ```clojure (require '[muuntaja.core :as m]) ; Create with defaults (def m1 (m/create)) ; Create with custom options (def m2 (m/create (assoc m/default-options :default-format "application/edn"))) ; Idempotent: already a Muuntaja (def m3 (m/create m1)) (= m1 m3) ; => true ``` ``` -------------------------------- ### Format Implementations Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/MANIFEST.txt Overview of supported format implementations, including built-in and optional modules for various data serialization formats. ```APIDOC ## Format Implementations Muuntaja supports a variety of data formats through its format modules. ### Built-in Formats - JSON (via jsonista) - EDN (Clojure native) - Transit JSON (Cognitect) - Transit MessagePack (Cognitect) ### Optional Formats (separate artifacts) - muuntaja-form (Form URL-encoded) - muuntaja-msgpack (MessagePack binary) - muuntaja-yaml (YAML) - muuntaja-cheshire (Alternative JSON) - muuntaja-charred (High-performance JSON) ``` -------------------------------- ### Create Muuntaja Instance with Selected Formats Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/configuration.md Create a Muuntaja instance that only supports a specific list of formats, like 'application/json' and 'application/edn'. ```clojure ; Create with specific formats only (def m (m/create (m/select-formats m/default-options ["application/json" "application/edn"]))) ``` -------------------------------- ### Create Custom Muuntaja Instance Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/README.md Create a custom Muuntaja instance with a default format and supported charsets. See configuration.md for all options. ```clojure (require '[muuntaja.core :as m]) (def m (m/create (assoc m/default-options :default-format "application/edn" :charsets #{"utf-8" "iso-8859-1"}))) ``` -------------------------------- ### format-negotiate-interceptor Context Transformation Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/interceptor.md Illustrates the context transformation performed by format-negotiate-interceptor, setting request and response format details. ```clojure {:request {:headers {"content-type" "application/json" "accept" "application/edn"}}} ↓ {:request {... :muuntaja/request #FormatAndCharset{:format "application/json" :charset "utf-8"} :muuntaja/response #FormatAndCharset{:format "application/edn" :charset "utf-8"}}} ``` -------------------------------- ### Create Muuntaja Instance Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/api-reference/core.md Creates a new Muuntaja instance. Use with no arguments for defaults, or provide custom options. If an existing Muuntaja instance is provided, it is returned as-is. ```clojure (defn create ([] ...) ([muuntaja-or-options] ...)) ``` ```clojure (require '[muuntaja.core :as m]) ; Create with defaults (def m1 (m/create)) ; Create with custom options (def m2 (m/create (assoc m/default-options :default-format "application/edn"))) ; Idempotent: already a Muuntaja (def m3 (m/create m1)) (= m1 m3) ; => true ``` -------------------------------- ### Minimal Configuration (JSON Only) Source: https://github.com/metosin/muuntaja/blob/master/_autodocs/configuration.md Creates a Muuntaja instance configured to only handle JSON. ```clojure (def m (m/create (m/select-formats m/default-options ["application/json"]))) ```