forked from binance-exchange/binance-java-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request binance-exchange#26 from xezon/event_serialization
Add Json Serializers for CandlestickEvent and OrderBookEntry.
- Loading branch information
Showing
4 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/binance/api/client/domain/event/CandlestickEventSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.binance.api.client.domain.event; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Custom serializer for a candlestick stream event, since the structure of the candlestick json differ from the one in the REST API. | ||
* | ||
* @see CandlestickEvent | ||
*/ | ||
public class CandlestickEventSerializer extends JsonSerializer<CandlestickEvent> { | ||
|
||
@Override | ||
public void serialize(CandlestickEvent candlestickEvent, JsonGenerator gen, SerializerProvider serializers) throws IOException { | ||
gen.writeStartObject(); | ||
|
||
// Write header | ||
gen.writeStringField("e", candlestickEvent.getEventType()); | ||
gen.writeNumberField("E", candlestickEvent.getEventTime()); | ||
gen.writeStringField("s", candlestickEvent.getSymbol()); | ||
|
||
// Write candlestick data | ||
gen.writeObjectFieldStart("k"); | ||
gen.writeNumberField("t", candlestickEvent.getOpenTime()); | ||
gen.writeNumberField("T", candlestickEvent.getCloseTime()); | ||
gen.writeStringField("i", candlestickEvent.getIntervalId()); | ||
gen.writeNumberField("f", candlestickEvent.getFirstTradeId()); | ||
gen.writeNumberField("L", candlestickEvent.getLastTradeId()); | ||
gen.writeStringField("o", candlestickEvent.getOpen()); | ||
gen.writeStringField("c", candlestickEvent.getClose()); | ||
gen.writeStringField("h", candlestickEvent.getHigh()); | ||
gen.writeStringField("l", candlestickEvent.getLow()); | ||
gen.writeStringField("v", candlestickEvent.getVolume()); | ||
gen.writeNumberField("n", candlestickEvent.getNumberOfTrades()); | ||
gen.writeBooleanField("x", candlestickEvent.getBarFinal()); | ||
gen.writeStringField("q", candlestickEvent.getQuoteAssetVolume()); | ||
gen.writeStringField("V", candlestickEvent.getTakerBuyBaseAssetVolume()); | ||
gen.writeStringField("Q", candlestickEvent.getTakerBuyQuoteAssetVolume()); | ||
gen.writeEndObject(); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/binance/api/client/domain/market/OrderBookEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/com/binance/api/client/domain/market/OrderBookEntrySerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.binance.api.client.domain.market; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Custom serializer for an OrderBookEntry. | ||
*/ | ||
public class OrderBookEntrySerializer extends JsonSerializer<OrderBookEntry> { | ||
|
||
@Override | ||
public void serialize(OrderBookEntry orderBookEntry, JsonGenerator gen, SerializerProvider serializers) throws IOException { | ||
gen.writeStartArray(); | ||
gen.writeString(orderBookEntry.getPrice()); | ||
gen.writeString(orderBookEntry.getQty()); | ||
gen.writeEndArray(); | ||
} | ||
} |