---
title: Mixson Codecs
---

Although Mixson only supports .json, .png, and .nbt files by default, `MixsonCodec`s can be created so that the user
can handle custom resource types.

Mixson's default codecs can be found in the `MixsonCodecs` class. When registering an event or reference using the JSON
specific registration methods, Mixson internally associates the `MixsonCodecs.JSON_ELEMENT` with the event or reference.

The `MixsonCodec` interface is defined as follows:
```java
public interface MixsonCodec<T> {

    String extensionAndDot();

    T deserialize(Resource resource) throws IOException;

    Resource serialize(Resource associatedResource, T elem) throws IOException;

    ByteArrayOutputStream export(T resource) throws IOException;
}
```
The `deserialize` method will take in the resource Minecraft finds and will convert it into whatever value the codec is
designed for.

The `serialize` method converts the given custom resource and turns it back into a Minecraft resource. An
associatedResource is also provided as context for serialization.

The `serializeOutputFile` converts the custom resource into a string to be written to a file for debugging in the
`EXPORT` debug mode.

the `extensionAndDot` method returns the extension of the file the codec is targeting with a dot (.).

The codec can be passed to a registration method to apply it to the event or reference.

## Example
This example codec will create a `JsonObject` for event handling by wrapping the `MixsonCodecs.JSON_ELEMENT` codec:
```java
MixsonCodec<JsonObject> JSON_OBJECT = new MixsonCodec<>() {
        @Override
        public String extensionAndDot() {
            return ".json";
        }

        @Override
        public JsonObject deserialize(Resource r) throws IOException {
            return MixsonCodecs.JSON_ELEMENT.deserialize(r).getAsJsonObject();
        }

        @Override
        public Resource serialize(Resource r, JsonObject x) {
            return MixsonCodecs.JSON_ELEMENT.serialize(r, x);
        }

        @Override
        public ByteArrayOutputStream export(JsonObject resource) throws IOException {
            return MixsonCodecs.JSON_ELEMENT.export(resource);
        }
    };
```