com.github.dreamhead.moco.dumper.MessageContentDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of moco-core Show documentation
Show all versions of moco-core Show documentation
Moco is an easy setup stub framework, mainly focusing on testing and integration.
package com.github.dreamhead.moco.dumper;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.github.dreamhead.moco.model.MessageContent;
import java.io.IOException;
import static com.github.dreamhead.moco.model.MessageContent.content;
import static com.github.dreamhead.moco.util.Strings.strip;
public class MessageContentDeserializer extends JsonDeserializer {
@Override
public MessageContent deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException {
JsonToken currentToken = jp.getCurrentToken();
if (currentToken == JsonToken.VALUE_STRING) {
return content(strip(jp.getText()));
}
if (currentToken == JsonToken.START_OBJECT) {
InternalMessageContent content = jp.readValueAs(InternalMessageContent.class);
return content.toContent();
}
return (MessageContent) ctx.handleUnexpectedToken(MessageContent.class, jp);
}
}