<commit_before><commit_msg>format negotiation documentation to reitit<commit_after># Format Negotiation

`reitit` uses [muuntaja](https://github.com/metosin/muuntaja) as a library to handle format negotiation, encoding and decoding.

In order to have access to the options below, you need the following namespaces:

```clj
(require '[muuntaja.core :as m])
```

Then, a `muuntaja` instance must be installed in the router to your coercion benefit from the customizations.

```clj
(ring/ring-handler
   (ring/router
    ;; your route definitions
    ;; ....
    :data {:coercion reitit.coercion.spec/coercion
           :muuntaja m/instance}})
```


## Changing default parameters

The current JSON formatter used by `reitit` already have the option to parse keys as `keyword` which is a sane default in Clojure. However, if you would like to parse all the `double` as `bigdecimal` you'd need to change an option of the [JSON formatter](https://github.com/metosin/jsonista)


```clj
(def new-muuntaja-instance
  (m/create
   (assoc-in
    m/default-options
    [:formats "application/json" :decoder-opts :bigdecimals]
    true)))

```

Now you should change the `m/instance` installed in the router with the `new-muuntaja-instance`.

You can find more options for [JSON](https://cljdoc.org/d/metosin/jsonista/0.2.5/api/jsonista.core#object-mapper) and [EDN].


## Adding custom encoder

The example below is from `muuntaja` explaining how to add a custom encoder to parse a `java.util.Date` instance.

```clj

(def muuntaja-instance
  (m/create
    (assoc-in
      m/default-options
      [:formats "application/json" :encoder-opts]
      {:date-format "yyyy-MM-dd"})))

(->> {:value (java.util.Date.)}
     (m/encode m "application/json")
     slurp)
; => "{\"value\":\"2019-10-15\"}"

```

## Adding all together

If you inspect `m/default-options` it's only a map, therefore you can compose your new muuntaja instance with as many options as you need it.

```clj
(def new-muuntaja
  (m/create
   (-> m/default-options
       (assoc-in [:formats "application/json" :decoder-opts :bigdecimals] true)
       (assoc-in [:formats "application/json" :encoder-opts :data-format] "yyyy-MM-dd"))))
```
{'fraction_non_alphanumeric': 0.12995896032831739, 'fraction_numerical': 0.005015959872321021, 'mean_word_length': 3.7903930131004366, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4021177', 'n_tokens_mistral': 763, 'n_tokens_neox': 712, 'n_words': 231}