You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
2.1 KiB
67 lines
2.1 KiB
6 years ago
|
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);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public OrderBookDepth deserialize(JsonParser jp, DeserializationContext context) throws IOException {
|
||
|
JsonNode node = jp.getCodec().readTree(jp);
|
||
|
|
||
|
Long lastUpdateId = node.get("lastUpdateId").asLong();
|
||
|
List<OrderBookPricing> bids = new ArrayList<>();
|
||
|
List<OrderBookPricing> asks = new ArrayList<>();
|
||
|
|
||
|
|
||
|
// NOTE: Jackson nodes don't seem to support conversion to streams
|
||
|
|
||
|
for (JsonNode n : node.get("bids")) {
|
||
|
bids.add(OrderBookPricing
|
||
|
.builder()
|
||
|
.price(new BigDecimal(n.get(0).asText()))
|
||
|
.quantity(new BigDecimal(n.get(1).asText()))
|
||
|
.build()
|
||
|
);
|
||
|
}
|
||
|
|
||
|
for (JsonNode n : node.get("asks")) {
|
||
|
asks.add(OrderBookPricing
|
||
|
.builder()
|
||
|
.price(new BigDecimal(n.get(0).asText()))
|
||
|
.quantity(new BigDecimal(n.get(1).asText()))
|
||
|
.build()
|
||
|
);
|
||
|
}
|
||
|
|
||
|
return OrderBookDepth
|
||
|
.builder()
|
||
|
.lastUpdateId(lastUpdateId)
|
||
|
.bids(bids)
|
||
|
.asks(asks)
|
||
|
.build();
|
||
|
}
|
||
|
}
|