# Custom Type Handler

{% hint style="warning" %}
**This is the manual for older MicroStream versions (Version < 5.0).**

**The new documentation (Version >= 5.0) is located at:**

[https://docs.microstream.one/](https://docs.microstream.one/manual)
{% endhint %}

Custom Type Handler allow taking control over the storing and loading procedure of specific java types. This is useful to optimize the performance for storing complex objects or in the rare case that it is not possible to store a type with the default type handlers.

### Implementation

Suitable base class to start the implementation of a custom type handler for the Microstream standard binary storage implementation are:

`one.microstream.persistence.binary.internal.AbstractBinaryHandlerCustomValue`\
for simpler custom type handling in case only value have to be stored\
or\
`one.microstream.persistence.binary.internal.AbstractBinaryHandlerCustom`  \
if the object own references that have to be persisted too.

{% hint style="info" %}
[Example on GitHub](https://github.com/microstream-one/examples/tree/2.1/customTypeHandler)

This example implements a custom type handler for the java.awt.image.BufferedImage class. Instead of storing the rather complex object structure of that class the image is serialized as png image format using javax.imageio.ImageIO into an byte array. This byte array is stored by microstream.
{% endhint %}

### Setup

The custom type handler must be registered in the CustomTypeHandlerRegistry to enable it:

```java
mbeddedStorageManager storage = EmbeddedStorage
     .Foundation(WORKINGDIR)
     .onConnectionFoundation(f->
          f.registerCustomTypeHandlers(new CustomBufferedImageHandler()))
     .start(ROOT);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://manual.docs.microstream.one/2.1/data-store/customizing/custom-type-handler.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
