Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
Add Json Serializers for CandlestickEvent and OrderBookEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
xezon committed Jan 10, 2018
1 parent f9d65c0 commit 076dc91
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.binance.api.client.domain.event;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
* An interval candlestick for a symbol providing informations on price that can be used to produce candlestick charts.
*/
@JsonDeserialize(using = CandlestickEventDeserializer.class)
@JsonSerialize(using = CandlestickEventSerializer.class)
public class CandlestickEvent {

private String eventType;
Expand All @@ -31,7 +34,9 @@ public class CandlestickEvent {
private Long closeTime;

private String intervalId;

private Long firstTradeId;

private Long lastTradeId;

private String quoteAssetVolume;
Expand Down
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();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.binance.api.client.domain.market;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
* An order book entry consisting of price and quantity.
*/
@JsonDeserialize(using = OrderBookEntryDeserializer.class)
@JsonSerialize(using = OrderBookEntrySerializer.class)
public class OrderBookEntry {

private String price;
private String qty;

Expand Down
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();
}
}

0 comments on commit 076dc91

Please sign in to comment.