> For the complete documentation index, see [llms.txt](https://rqndomhax.gitbook.io/versaapi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://rqndomhax.gitbook.io/versaapi/databasemanager/deserializer.md).

# Deserializer

{% hint style="success" %}
A deserializer will always have to return an Object
{% endhint %}

{% content-ref url="/pages/-MkGrrQYDsRDPAxNK4sa" %}
[User](/versaapi/databasemanager/deserializer/user.md)
{% endcontent-ref %}

### Creating its deserializer

```java
public class UserDeserializer implements JsonDeserializer<User> {

    @Override
    public User deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        return null;
    }
}
```

#### We need to initialize the user with the values in the `jsonElement`

```java
JsonObject asJsonObject = jsonElement.getAsJsonObject(); // This is faster than always doing jsonElement.getAsJsonObject()...
```

{% hint style="info" %}
To make it easier to understand how to use JsonObject we will do it step by step
{% endhint %}

```java
UUID uniqueId = UUID.fromString(asJsonObject.get("uniqueId").getAsString());
long timestamp = Long.parseLong(asJsonObject.get("timestamp").getAsString());    // I'm storing the user's timestamp as a string in the database
String username = asJsonObject.get("username").getAsString();
Role role = User.Role.valueOf(asJsonObject.get("role").getAsString());           // The user's role is stored as a string such as "ADMIN" or "USER"
```

{% hint style="success" %}
We can now finish the deserializer
{% endhint %}

```java
public class UserDeserializer implements JsonDeserializer<User> {

    @Override
    public User deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        UUID uniqueId = UUID.fromString(asJsonObject.get("uniqueId").getAsString());
        long timestamp = Long.parseLong(asJsonObject.get("timestamp").getAsString());    // I'm storing the user's timestamp as a string in the database
        String username = asJsonObject.get("username").getAsString();
        Role role = User.Role.valueOf(asJsonObject.get("role").getAsString());  
        return new User(uniqueId, timestamp, username, role);                            // We just have to create an User with the values we retrieved
    }
}
```

{% hint style="warning" %}
We now have to register it
{% endhint %}

```java
Gson deserializer = new GsonBuilder()
            .registerTypeAdapter(User.class, new UserDeserializer())
            .create();
```
