<commit_before># Dispatch

The `dispatch` function takes a JSON-RPC request, calls the appropriate method
and gives a JSON-RPC response.

```python
>>> dispatch('{"jsonrpc": "2.0", "method": "ping", "id": 1}')
'{"jsonrpc": "2.0", "result": "pong", "id": 1}'
```

[See how dispatch is used in different frameworks.](examples)

## Options

### methods

This lets you specify a group of methods to dispatch to. It's an alternative to
using the `@method` decorator. The value should be a dict mapping function
names to functions.

```python
def ping():
    return Success("pong")

dispatch('{"jsonrpc": "2.0", "method": "ping", "id": 1}', methods={"ping": ping})
```

Default is `global_methods`, which is an internal dict populated by the
`@method` decorator.

### context

If specified, this will be the first argument to all methods.

```python
@method
def greet(context, name):
    return Success(context + " " + name)

>>> dispatch('{"jsonrpc": "2.0", "method": "greet", "params": ["Beau"], "id": 1}', context="Hello")
'{"jsonrpc": "2.0", "result": "Hello Beau", "id": 1}'
```

### deserializer

A function that parses the request string. Default is `json.loads`.

```python
import ujson

dispatch(request, deserializer=ujson.loads)
```

### serializer

A function that serializes the response string. Default is `json.dumps`.

```python
import ujson

dispatch(request, serializer=ujson.loads)
```<commit_msg>Update dispatch.md<commit_after># Dispatch

The `dispatch` function takes a JSON-RPC request, calls the appropriate method
and gives a JSON-RPC response.

```python
>>> dispatch('{"jsonrpc": "2.0", "method": "ping", "id": 1}')
'{"jsonrpc": "2.0", "result": "pong", "id": 1}'
```

[See how dispatch is used in different frameworks.](examples)

## Options

### methods

This lets you specify a group of methods to dispatch to. It's an alternative to
using the `@method` decorator. The value should be a dict mapping function
names to functions.

```python
def ping():
    return Success("pong")

dispatch('{"jsonrpc": "2.0", "method": "ping", "id": 1}', methods={"ping": ping})
```

Default is `global_methods`, which is an internal dict populated by the
`@method` decorator.

### context

If specified, this will be the first argument to all methods.

```python
@method
def greet(context, name):
    return Success(context + " " + name)

>>> dispatch('{"jsonrpc": "2.0", "method": "greet", "params": ["Beau"], "id": 1}', context="Hello")
'{"jsonrpc": "2.0", "result": "Hello Beau", "id": 1}'
```

### deserializer

A function that parses the request string. Default is `json.loads`.

```python
dispatch(request, deserializer=ujson.loads)
```

### serializer

A function that serializes the response string. Default is `json.dumps`.

```python
dispatch(request, serializer=ujson.dumps)
```
{'fraction_non_alphanumeric': 0.1748301751877011, 'fraction_numerical': 0.010725777618877368, 'mean_word_length': 5.095860566448802, 'pattern_counts': {'":': 34, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, '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': '1018604', 'n_tokens_mistral': 913, 'n_tokens_neox': 873, 'n_words': 327}