com.geotab.model.serialization.StringResponseDeserializer Maven / Gradle / Ivy
/*
*
* 2020 Copyright (C) Geotab Inc. All rights reserved.
*/
package com.geotab.model.serialization;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.geotab.http.response.StringResponse;
import com.geotab.model.Id;
import com.geotab.model.error.JsonRpcError;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class StringResponseDeserializer extends JsonDeserializer {
@Override
public StringResponse deserialize(JsonParser jsonParser, DeserializationContext context)
throws IOException {
ObjectCodec parserCodec = jsonParser.getCodec();
JsonNode node = parserCodec.readTree(jsonParser);
if (node.isTextual()) {
return StringResponse.builder()
.result(node.textValue())
.build();
} else if (node.isObject()) {
Id id = node.get("id") != null ? parserCodec.treeToValue(node.get("id"), Id.class) : null;
String result = node.get("result") != null ? node.get("result").toString() : null;
JsonRpcError error = node.get("error") != null
? parserCodec.treeToValue(node.get("error"), JsonRpcError.class) : null;
String jsonrpc = node.get("jsonrpc") != null ? node.get("jsonrpc").textValue() : null;
Integer requestIndex =
node.get("requestIndex") != null ? node.get("requestIndex").intValue() : null;
return StringResponse.builder()
.result(result)
.error(error)
.jsonrpc(jsonrpc)
.id(id)
.requestIndex(requestIndex)
.build();
}
return null;
}
}