|
|
|
package com.sigmaflare.binancej.entities.transform;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonParser;
|
|
|
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
|
|
|
import com.sigmaflare.binancej.entities.OrderBookDepth;
|
|
|
|
import com.sigmaflare.binancej.entities.OrderBookPricing;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A custom deserializer for OrderBookDepth. Binance returns some non-standard looking data
|
|
|
|
* in it's array, so we convert the entire thing into our own representation here to make
|
|
|
|
* it easier to use.
|
|
|
|
*/
|
|
|
|
public class OrderBookDepthResponseDeserializer extends StdDeserializer<OrderBookDepth> {
|
|
|
|
|
|
|
|
public OrderBookDepthResponseDeserializer() {
|
|
|
|
this(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public OrderBookDepthResponseDeserializer(Class<?> vc) {
|
|
|
|
super(vc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a List of OrderBookPricing from the JSON supplied
|
|
|
|
* @param node The JsonNode given from the deserialize method
|
|
|
|
* @param nodeName The name of the node to transform
|
|
|
|
* @return A list of OrderBookPricing
|
|
|
|
*/
|
|
|
|
private List<OrderBookPricing> createOrderBookPricingListFromNode(JsonNode node, String nodeName) {
|
|
|
|
// NOTE: Jackson nodes don't seem to support conversion to streams
|
|
|
|
List<OrderBookPricing> data = new ArrayList<>();
|
|
|
|
for (JsonNode n : node.get(nodeName)) {
|
|
|
|
data.add(OrderBookPricing
|
|
|
|
.builder()
|
|
|
|
.price(new BigDecimal(n.get(0).asText()))
|
|
|
|
.quantity(new BigDecimal(n.get(1).asText()))
|
|
|
|
.build()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public OrderBookDepth deserialize(JsonParser jp, DeserializationContext context) throws IOException {
|
|
|
|
JsonNode node = jp.getCodec().readTree(jp);
|
|
|
|
|
|
|
|
Long lastUpdateId = node.get("lastUpdateId").asLong();
|
|
|
|
|
|
|
|
return OrderBookDepth
|
|
|
|
.builder()
|
|
|
|
.lastUpdateId(lastUpdateId)
|
|
|
|
.bids(createOrderBookPricingListFromNode(node, "bids"))
|
|
|
|
.asks(createOrderBookPricingListFromNode(node, "asks"))
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
}
|