This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
forked from joaopsilva/binance-java-api
-
Notifications
You must be signed in to change notification settings - Fork 622
/
UserDataUpdateEventDeserializer.java
53 lines (44 loc) · 2.19 KB
/
UserDataUpdateEventDeserializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.binance.api.client.domain.event;
import com.binance.api.client.domain.event.UserDataUpdateEvent.UserDataUpdateEventType;
import com.binance.api.client.exception.BinanceApiException;
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.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
/**
* Custom deserializer for a User Data stream event, since the API can return two different responses in this stream.
* @see UserDataUpdateEvent
*/
public class UserDataUpdateEventDeserializer extends JsonDeserializer<UserDataUpdateEvent> {
@Override
public UserDataUpdateEvent deserialize(JsonParser jp, DeserializationContext ctx) throws IOException {
ObjectCodec oc = jp.getCodec();
JsonNode node = oc.readTree(jp);
String json = node.toString();
final String eventTypeId = node.get("e").asText();
final Long eventTime = node.get("E").asLong();
UserDataUpdateEventType userDataUpdateEventType = UserDataUpdateEventType.fromEventTypeId(eventTypeId);
UserDataUpdateEvent userDataUpdateEvent = new UserDataUpdateEvent();
userDataUpdateEvent.setEventType(userDataUpdateEventType);
userDataUpdateEvent.setEventTime(eventTime);
if (userDataUpdateEventType == UserDataUpdateEventType.ACCOUNT_UPDATE) {
AccountUpdateEvent accountUpdateEvent = getUserDataUpdateEventDetail(json, AccountUpdateEvent.class);
userDataUpdateEvent.setAccountUpdateEvent(accountUpdateEvent);
} else { // userDataUpdateEventType == UserDataUpdateEventType.ORDER_TRADE_UPDATE
OrderTradeUpdateEvent orderTradeUpdateEvent = getUserDataUpdateEventDetail(json, OrderTradeUpdateEvent.class);
userDataUpdateEvent.setOrderTradeUpdateEvent(orderTradeUpdateEvent);
}
return userDataUpdateEvent;
}
public <T> T getUserDataUpdateEventDetail(String json, Class<T> clazz) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(json, clazz);
} catch (IOException e) {
throw new BinanceApiException(e);
}
}
}