knowm / XChange

XChange is a Java library providing a streamlined API for interacting with 60+ Bitcoin and Altcoin exchanges providing a consistent interface for trading and accessing market data.
http://knowm.org/open-source/xchange/
MIT License
3.87k stars 1.94k forks source link

DTO classes should be JSON serializable #3924

Open phraktle opened 3 years ago

phraktle commented 3 years ago

Many DTOs have inconsistencies that break JSON serialization, such as divergent getter method names. It should be possible to serialize then de-serialize these objects.

As a workaround, one can use Jackson mixins. For example this provides a fix for serializing BitmexPrivateOrder:

abstract class BitmexPrivateOrderMixin {

    @JsonProperty("orderID")
    abstract String getId();

    @JsonProperty("orderQty")
    abstract BigDecimal getVolume();

    @JsonProperty("ordStatus")
    abstract OrderStatus getOrderStatus();

    @JsonProperty("settlCurrency")
    abstract String getSettleCurrency();

}
phraktle commented 3 years ago

A similar inconsistency in Deribit DTOs prevents bi-directional Jackson serialization. The timestamp field (which is of type long) vs getTimestamp (of type Date) in Trade and Settlement. A workaround for these is to ignore the getter:

ObjectMapper mapper = new ObjectMapper();    
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);