refactor: Move macro defined json fns to cpp file

Some fns are big (shouldn't be inlined); plus its best to avoid exposing
macros in headers.
This commit is contained in:
John Colvin committed 2025-02-19 22:50:29 +00:00
1 parent 8db34a2a11
commit ecfc5405fe
15 files changed
+182 -181

No files matched your search

+5 -9
View File
@@ -3,18 +3,14 @@
#define _TO_JSON_FIELD(field) j[#field] = t.field;
#define _FROM_JSON_FIELD(field) bts::io::JsonReader{&j, #field}.read(t.field);
#define SPARK_TO_JSON_IMPL(type, for_each) \
inline void to_json(nlohmann::json &j, const type &t) { \
FOR_EACH(_TO_JSON_FIELD) \
}
#define SPARK_TO_JSON_IMPL(type, fields) \
void to_json(nlohmann::json &j, const type &t) { fields(_TO_JSON_FIELD) }
#define SPARK_FROM_JSON_IMPL(type, for_each) \
inline void from_json(const nlohmann::json &j, type &t) { \
FOR_EACH(_FROM_JSON_FIELD) \
}
#define SPARK_FROM_JSON_IMPL(type, fields) \
void from_json(const nlohmann::json &j, type &t) { fields(_FROM_JSON_FIELD) }
#define OSTREAM_JSON_IMPL(type) \
inline std::ostream &operator<<(std::ostream &out, const type &t) { \
std::ostream &operator<<(std::ostream &out, const type &t) { \
nlohmann::json j; \
to_json(j, t); \
out << j.dump(2); \
@@ -0,0 +1,15 @@
#include "BinanceIcebergPartsFilter.h"
#include "lib/io/JsonMacros.h"
#include "lib/io/JsonReader.h"
namespace bts::md::binance {
#define BINANCE_ICEBERG_PARTS_FILTER_FIELDS(op) op(filterType) op(limit)
SPARK_TO_JSON_IMPL(
BinanceIcebergPartsFilter, BINANCE_ICEBERG_PARTS_FILTER_FIELDS);
SPARK_FROM_JSON_IMPL(
BinanceIcebergPartsFilter, BINANCE_ICEBERG_PARTS_FILTER_FIELDS);
OSTREAM_JSON_IMPL(BinanceIcebergPartsFilter);
} // namespace bts::md::binance
@@ -1,7 +1,5 @@
#pragma once
#include "lib/io/JsonMacros.h"
#include "lib/io/JsonReader.h"
#include "server/sparkd/md/binance/BinanceFilterType.h"
#include <iostream>
@@ -20,16 +18,4 @@ void to_json(nlohmann::json &j, const BinanceIcebergPartsFilter &f);
std::ostream &operator<<(std::ostream &out, const BinanceIcebergPartsFilter &f);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
#define FOR_EACH(op) op(filterType) op(limit)
SPARK_TO_JSON_IMPL(BinanceIcebergPartsFilter, FOR_EACH);
SPARK_FROM_JSON_IMPL(BinanceIcebergPartsFilter, FOR_EACH);
OSTREAM_JSON_IMPL(BinanceIcebergPartsFilter);
#undef FOR_EACH
} // namespace bts::md::binance
@@ -0,0 +1,23 @@
#include "BinanceMinNotionalFilter.h"
#include "lib/io/JsonMacros.h"
#include "lib/io/JsonReader.h"
#include <nlohmann/json.hpp>
namespace bts::md::binance {
// clang-format off
#define BINANCE_MIN_NOTIONAL_FILTER_FIELDS(op) \
op(filterType) \
op(applyToMarket) \
op(minNotional) \
op(avgPriceMins)
// clang-format on
SPARK_TO_JSON_IMPL(
BinanceMinNotionalFilter, BINANCE_MIN_NOTIONAL_FILTER_FIELDS);
SPARK_FROM_JSON_IMPL(
BinanceMinNotionalFilter, BINANCE_MIN_NOTIONAL_FILTER_FIELDS);
OSTREAM_JSON_IMPL(BinanceMinNotionalFilter);
} // namespace bts::md::binance
@@ -1,12 +1,9 @@
#pragma once
#include "lib/io/JsonMacros.h"
#include "lib/io/JsonReader.h"
#include "server/sparkd/md/binance/BinanceFilterType.h"
#include <iostream>
#include <nlohmann/json.hpp>
#include <string>
#include <nlohmann/json_fwd.hpp>
namespace bts::md::binance {
@@ -31,22 +28,4 @@ void to_json(nlohmann::json &j, const BinanceMinNotionalFilter &f);
std::ostream &operator<<(std::ostream &out, const BinanceMinNotionalFilter &f);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
// clang-format off
#define FOR_EACH(op) \
op(filterType) \
op(applyToMarket) \
op(minNotional) \
op(avgPriceMins)
// clang-format on
SPARK_TO_JSON_IMPL(BinanceMinNotionalFilter, FOR_EACH);
SPARK_FROM_JSON_IMPL(BinanceMinNotionalFilter, FOR_EACH);
OSTREAM_JSON_IMPL(BinanceMinNotionalFilter);
#undef FOR_EACH
} // namespace bts::md::binance
@@ -0,0 +1,22 @@
#include "BinanceNotionalFilter.h"
#include "lib/io/JsonMacros.h"
#include "lib/io/JsonReader.h"
#include <nlohmann/json.hpp>
namespace bts::md::binance {
// clang-format off
#define FOR_EACH(op) \
op(filterType) \
op(applyMaxToMarket) \
op(applyMinToMarket) \
op(avgPriceMins) \
op(maxNotional) \
op(minNotional) // clang-format on
SPARK_TO_JSON_IMPL(BinanceNotionalFilter, FOR_EACH);
SPARK_FROM_JSON_IMPL(BinanceNotionalFilter, FOR_EACH);
OSTREAM_JSON_IMPL(BinanceNotionalFilter);
} // namespace bts::md::binance
@@ -1,11 +1,8 @@
#pragma once
#include "lib/io/JsonMacros.h"
#include "lib/io/JsonReader.h"
#include "server/sparkd/md/binance/BinanceFilterType.h"
#include <nlohmann/json.hpp>
#include <string>
#include <nlohmann/json_fwd.hpp>
namespace bts::md::binance {
@@ -28,24 +25,4 @@ void from_json(const nlohmann::json &j, BinanceNotionalFilter &f);
void to_json(nlohmann::json &j, const BinanceNotionalFilter &f);
std::ostream &operator<<(std::ostream &out, const BinanceNotionalFilter &f);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
// clang-format off
#define FOR_EACH(op) \
op(filterType) \
op(applyMaxToMarket) \
op(applyMinToMarket) \
op(avgPriceMins) \
op(maxNotional) \
op(minNotional) \
// clang-format on
SPARK_TO_JSON_IMPL(BinanceNotionalFilter, FOR_EACH);
SPARK_FROM_JSON_IMPL(BinanceNotionalFilter, FOR_EACH);
OSTREAM_JSON_IMPL(BinanceNotionalFilter);
#undef FOR_EACH
} // namespace bts::md::binance
+27 -1
View File
@@ -1,12 +1,13 @@
#include "BinanceSpot.h"
#include "lib/api/domain/Venue.pb.h"
#include "lib/domain/Quantity.h"
#include "lib/domain/Security.h"
#include "lib/io/JsonMacros.h"
#include "lib/io/JsonReader.h"
#include "lib/logging/logging.h"
#include <ccapi_cpp/ccapi_message.h>
#include <iostream>
#include <nlohmann/json.hpp>
#include <regex>
#include <string>
@@ -90,4 +91,29 @@ void BinanceSpot::parse(
}
}
// clang-format off
#define BINANCE_SPOT_FIELDS(f) \
f(symbol) \
f(status) \
f(baseAsset) \
f(baseAssetPrecision) \
f(quoteAsset) \
f(quotePrecision) \
f(quoteAssetPrecision) \
f(orderTypes) \
f(icebergAllowed) \
f(ocoAllowed) \
f(quoteOrderQtyMarketAllowed) \
f(allowTrailingStop) \
f(cancelReplaceAllowed) \
f(isSpotTradingAllowed) \
f(isMarginTradingAllowed) \
f(permissions) \
f(filters)
// clang-format on
SPARK_FROM_JSON_IMPL(BinanceSpot, BINANCE_SPOT_FIELDS);
SPARK_TO_JSON_IMPL(BinanceSpot, BINANCE_SPOT_FIELDS);
OSTREAM_JSON_IMPL(BinanceSpot);
} // namespace bts::md::binance
+1 -33
View File
@@ -2,14 +2,13 @@
#include "lib/api/domain/Venue.h"
#include "lib/core/FMT_IMPL.h"
#include "lib/io/JsonMacros.h"
#include "server/sparkd/md/binance/BinanceFilterTuple.h"
#include "server/sparkd/md/binance/BinanceInstrumentStatus.h"
#include "server/sparkd/md/binance/BinanceOrderType.h"
#include "server/sparkd/md/binance/BinancePermission.h"
#include "server/sparkd/md/binance/BinanceQuoteAsset.h"
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include <ostream>
#include <string>
#include <vector>
@@ -62,37 +61,6 @@ void from_json(const nlohmann::json &j, BinanceSpot &spot);
void to_json(nlohmann::json &j, const BinanceSpot &spot);
std::ostream &operator<<(std::ostream &out, const BinanceSpot &spot);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
// clang-format off
#define FOR_EACH(op) \
op(symbol) \
op(status) \
op(baseAsset) \
op(baseAssetPrecision) \
op(quoteAsset) \
op(quotePrecision) \
op(quoteAssetPrecision) \
op(orderTypes) \
op(icebergAllowed) \
op(ocoAllowed) \
op(quoteOrderQtyMarketAllowed) \
op(allowTrailingStop) \
op(cancelReplaceAllowed) \
op(isSpotTradingAllowed) \
op(isMarginTradingAllowed) \
op(permissions) \
op(filters)
// clang-format on
SPARK_FROM_JSON_IMPL(BinanceSpot, FOR_EACH);
SPARK_TO_JSON_IMPL(BinanceSpot, FOR_EACH);
OSTREAM_JSON_IMPL(BinanceSpot);
#undef FOR_EACH
} // namespace bts::md::binance
FMT_IMPL(bts::md::binance::BinanceSpot, s, "{}", s.symbol);
@@ -2,11 +2,13 @@
#include "lib/domain/Price.h"
#include "lib/domain/Quantity.h"
#include "lib/domain/SecurityMarketDataStatistics.h"
#include "lib/io/JsonMacros.h"
#include "lib/io/JsonReader.h"
#include "server/sparkd/md/ccapi/CCApiReader.h"
#include <ccapi_cpp/ccapi_macro.h>
#include <ccapi_cpp/ccapi_message.h>
#include <nlohmann/json.hpp>
#include <string>
namespace bts::md::binance_coin_futures {
@@ -66,4 +68,33 @@ void BinanceCoinFuturesPriceStatistics::to_security_statistics(
result.volume.value() = static_cast<Quantity::Value>(volume);
}
// clang-format off
#define BINANCE_COIN_FUTURES_PRICE_STATISTICS_FIELDS(f) \
f(symbol) \
f(pair) \
f(priceChange) \
f(priceChangePercent) \
f(weightedAvgPrice) \
f(lastPrice) \
f(lastQty) \
f(openPrice) \
f(highPrice) \
f(lowPrice) \
f(volume) \
f(baseVolume) \
f(openTime) \
f(closeTime) \
f(firstId) \
f(lastId) \
f(count)
// clang-format on
SPARK_TO_JSON_IMPL(
BinanceCoinFuturesPriceStatistics,
BINANCE_COIN_FUTURES_PRICE_STATISTICS_FIELDS);
SPARK_FROM_JSON_IMPL(
BinanceCoinFuturesPriceStatistics,
BINANCE_COIN_FUTURES_PRICE_STATISTICS_FIELDS);
OSTREAM_JSON_IMPL(BinanceCoinFuturesPriceStatistics)
} // namespace bts::md::binance_coin_futures
@@ -1,9 +1,6 @@
#pragma once
#include "lib/io/JsonMacros.h"
#include "lib/io/JsonReader.h"
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include <string>
#include <vector>
@@ -53,29 +50,4 @@ class BinanceCoinFuturesPriceStatistics {
domain::SecurityMarketDataStatistics &result) const;
};
// clang-format off
#define FOR_EACH(op) \
op(symbol) \
op(pair) \
op(priceChange) \
op(priceChangePercent) \
op(weightedAvgPrice) \
op(lastPrice) \
op(lastQty) \
op(openPrice) \
op(highPrice) \
op(lowPrice) \
op(volume) \
op(baseVolume) \
op(openTime) \
op(closeTime) \
op(firstId) \
op(lastId) \
op(count)
// clang-format on
SPARK_TO_JSON_IMPL(BinanceCoinFuturesPriceStatistics, FOR_EACH);
SPARK_FROM_JSON_IMPL(BinanceCoinFuturesPriceStatistics, FOR_EACH);
OSTREAM_JSON_IMPL(BinanceCoinFuturesPriceStatistics)
} // namespace bts::md::binance_coin_futures
@@ -0,0 +1,23 @@
#include "BinanceUsdsFuturesMinNotionalFilter.h"
#include "lib/io/JsonMacros.h"
#include "lib/io/JsonReader.h"
#include <nlohmann/json.hpp>
namespace bts::md::binance_usds_futures {
// clang-format off
#define BINANCE_USDS_FUTURES_MIN_NOTIONAL_FILTER_FIELDS(f) \
f(filterType) \
f(notional)
// clang-format on
SPARK_TO_JSON_IMPL(
BinanceUsdsFuturesMinNotionalFilter,
BINANCE_USDS_FUTURES_MIN_NOTIONAL_FILTER_FIELDS);
SPARK_FROM_JSON_IMPL(
BinanceUsdsFuturesMinNotionalFilter,
BINANCE_USDS_FUTURES_MIN_NOTIONAL_FILTER_FIELDS);
OSTREAM_JSON_IMPL(BinanceUsdsFuturesMinNotionalFilter);
} // namespace bts::md::binance_usds_futures
@@ -1,12 +1,9 @@
#pragma once
#include "lib/io/JsonMacros.h"
#include "lib/io/JsonReader.h"
#include "server/sparkd/md/binance/BinanceFilterType.h"
#include <iostream>
#include <nlohmann/json.hpp>
#include <string>
#include <nlohmann/json_fwd.hpp>
namespace bts::md::binance_usds_futures {
@@ -31,20 +28,4 @@ void to_json(nlohmann::json &j, const BinanceUsdsFuturesMinNotionalFilter &f);
std::ostream &operator<<(
std::ostream &out, const BinanceUsdsFuturesMinNotionalFilter &f);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
// clang-format off
#define FOR_EACH(op) \
op(filterType) \
op(notional)
// clang-format on
SPARK_TO_JSON_IMPL(BinanceUsdsFuturesMinNotionalFilter, FOR_EACH);
SPARK_FROM_JSON_IMPL(BinanceUsdsFuturesMinNotionalFilter, FOR_EACH);
OSTREAM_JSON_IMPL(BinanceUsdsFuturesMinNotionalFilter);
#undef FOR_EACH
} // namespace bts::md::binance_usds_futures
@@ -2,11 +2,13 @@
#include "lib/domain/Price.h"
#include "lib/domain/Quantity.h"
#include "lib/domain/SecurityMarketDataStatistics.h"
#include "lib/io/JsonMacros.h"
#include "lib/io/JsonReader.h"
#include "server/sparkd/md/ccapi/CCApiReader.h"
#include <ccapi_cpp/ccapi_macro.h>
#include <ccapi_cpp/ccapi_message.h>
#include <nlohmann/json.hpp>
#include <string>
namespace bts::md::binance_usds_futures {
@@ -67,4 +69,32 @@ void BinanceUsdsFuturesPriceStatistics::to_security_statistics(
result.volume.value() = static_cast<Quantity::Value>(volume);
}
// clang-format off
#define BINANCE_USDS_FUTURES_PRICE_STATISTICS_FIELDS(f) \
f(symbol) \
f(priceChange) \
f(priceChangePercent) \
f(weightedAvgPrice) \
f(lastPrice) \
f(lastQty) \
f(openPrice) \
f(highPrice) \
f(lowPrice) \
f(volume) \
f(quoteVolume) \
f(openTime) \
f(closeTime) \
f(firstId) \
f(lastId) \
f(count)
// clang-format on
SPARK_TO_JSON_IMPL(
BinanceUsdsFuturesPriceStatistics,
BINANCE_USDS_FUTURES_PRICE_STATISTICS_FIELDS);
SPARK_FROM_JSON_IMPL(
BinanceUsdsFuturesPriceStatistics,
BINANCE_USDS_FUTURES_PRICE_STATISTICS_FIELDS);
OSTREAM_JSON_IMPL(BinanceUsdsFuturesPriceStatistics)
} // namespace bts::md::binance_usds_futures
@@ -1,9 +1,6 @@
#pragma once
#include "lib/io/JsonMacros.h"
#include "lib/io/JsonReader.h"
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include <string>
#include <vector>
@@ -51,29 +48,4 @@ class BinanceUsdsFuturesPriceStatistics {
void to_security_statistics(
domain::SecurityMarketDataStatistics &result) const;
};
// clang-format off
#define FOR_EACH(op) \
op(symbol) \
op(priceChange) \
op(priceChangePercent) \
op(weightedAvgPrice) \
op(lastPrice) \
op(lastQty) \
op(openPrice) \
op(highPrice) \
op(lowPrice) \
op(volume) \
op(quoteVolume) \
op(openTime) \
op(closeTime) \
op(firstId) \
op(lastId) \
op(count)
// clang-format on
SPARK_TO_JSON_IMPL(BinanceUsdsFuturesPriceStatistics, FOR_EACH);
SPARK_FROM_JSON_IMPL(BinanceUsdsFuturesPriceStatistics, FOR_EACH);
OSTREAM_JSON_IMPL(BinanceUsdsFuturesPriceStatistics)
} // namespace bts::md::binance_usds_futures