aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/server/api/activity/helpers/ActivityIdDeserializer.java
blob: ba8cfb8721e22a06445877e2b6281f539cf8a7df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.juick.server.api.activity.helpers;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.IOException;

public class ActivityIdDeserializer extends JsonDeserializer<String> {
    @Override
    public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        JsonToken jsonToken = p.getCurrentToken();
        if (jsonToken == JsonToken.START_OBJECT) {
            JsonNode node = p.getCodec().readTree(p);
            return node.get("id").textValue();
        }
        return p.getValueAsString();
    }
}