diff --git a/src/lib/api/services/OrderService.cpp b/src/lib/api/services/OrderService.cpp new file mode 100644 index 000000000..d0cadc3f5 --- /dev/null +++ b/src/lib/api/services/OrderService.cpp @@ -0,0 +1,174 @@ +#include "OrderService.h" +#include "lib/api/services/OrderService.pb.h" + +namespace bts::api::orderman { + +#define PARSE_STRING(name, dest, it) \ + it = j.find(#name); \ + if (it != j.end()) { \ + dest.mutable_##name()->clear(); \ + it->get_to(*dest.mutable_##name()); \ + } +#define PARSE_VALUE(name, dest, it) \ + it = j.find(#name); \ + if (it != j.end()) { \ + dest.set_##name(it->get()); \ + } +#define PARSE_STRUCT(name, dest, it) \ + it = j.find(#name); \ + if (it != j.end()) { \ + it->get_to(*dest.mutable_##name()); \ + } +#define PARSE_ARRAY(name, dest, it) \ + it = j.find(#name); \ + if (it != j.end() && it->is_array()) { \ + c.mutable_##name()->Clear(); \ + it->get_to(*c.mutable_##name()); \ + } +#define PARSE_ONEOF(name, dest, it) \ + if (!parsed_oneof) { \ + it = j.find(#name); \ + if (it != j.end()) { \ + it->get_to(*dest.mutable_##name()); \ + parsed_oneof = true; \ + } \ + } +#define PARSE_ONEOFS(for_each) \ + { \ + bool parsed_oneof = false; \ + for_each(PARSE_ONEOF) \ + } + +#define WRITE_VALUE(src, name) j[#name] = src.name() +#define WRITE_STRUCT(src, name) \ + if (src.has_##name()) { \ + j[#name] = src.name(); \ + } + +#define WRITE_IF_NOT_ZERO(src, name) \ + if (src.name() != 0) { \ + j[#name] = src.name(); \ + } +#define WRITE_IF_NOT_NAN(src, name) \ + if (!std::isnan(src.name())) { \ + j[#name] = src.name(); \ + } +#define WRITE_IF_NOT_EMPTY(src, name) \ + if (!src.name().empty()) { \ + j[#name] = src.name(); \ + } +#define WRITE_TIMESTAMP(src, name) \ + j[#name] = bts::core::TimeStamp::from_nanos( \ + src.name().seconds() * 1'000'000'000 + src.name().nanos()) \ + .to_iso_string() + +#define PARSE_TIMESTAMP(name, dest, it) \ + it = j.find(#name); \ + if (it != j.end()) { \ + bts::core::TimeStamp tmp_ts = \ + bts::core::TimeStamp::from_iso_string(it->get()); \ + tmp_ts.to_protobuf(*dest.mutable_##name()); \ + } + +void to_json(nlohmann::json &j, const Fill &f) { + WRITE_IF_NOT_ZERO(f, order_key); + WRITE_IF_NOT_ZERO(f, security_key); + WRITE_IF_NOT_EMPTY(f, account); + WRITE_IF_NOT_NAN(f, last_price); + WRITE_IF_NOT_NAN(f, avg_price); + WRITE_VALUE(f, side); + WRITE_IF_NOT_ZERO(f, last_shares); + WRITE_IF_NOT_EMPTY(f, fill_key); + WRITE_TIMESTAMP(f, timestamp); + WRITE_IF_NOT_ZERO(f, internal_account_id); + WRITE_VALUE(f, fill_kind); + WRITE_VALUE(f, fill_status); +} + +void from_json(const nlohmann::json &j, Fill &f) { + nlohmann::json::const_iterator it; + PARSE_VALUE(order_key, f, it); + PARSE_VALUE(security_key, f, it); + PARSE_STRING(account, f, it); + PARSE_VALUE(last_price, f, it); + PARSE_VALUE(avg_price, f, it); + PARSE_VALUE(side, f, it); + PARSE_VALUE(last_shares, f, it); + PARSE_STRING(fill_key, f, it); + PARSE_TIMESTAMP(timestamp, f, it); + PARSE_VALUE(internal_account_id, f, it); + PARSE_VALUE(fill_kind, f, it); + PARSE_VALUE(fill_status, f, it); +} + +void to_json(nlohmann::json &j, const NativeOrderState &n) { + WRITE_VALUE(n, exchange_order_id); + WRITE_VALUE(n, last_msg_seq_num); + WRITE_VALUE(n, sub_error_code); + WRITE_VALUE(n, native_status); + WRITE_VALUE(n, routing_record); + WRITE_VALUE(n, native_type); + WRITE_VALUE(n, last_transact_time); + WRITE_VALUE(n, last_transact_time_unix_ns); + WRITE_VALUE(n, stop_px); + WRITE_VALUE(n, exchange_order_id_u64); +} + +void from_json(const nlohmann::json &j, NativeOrderState &n) { + nlohmann::json::const_iterator it; + PARSE_STRING(exchange_order_id, n, it); + PARSE_VALUE(last_msg_seq_num, n, it); + PARSE_VALUE(sub_error_code, n, it); + PARSE_VALUE(native_status, n, it); + PARSE_STRUCT(routing_record, n, it); + PARSE_VALUE(native_type, n, it); + PARSE_STRING(last_transact_time, n, it); + PARSE_VALUE(last_transact_time_unix_ns, n, it); + PARSE_VALUE(stop_px, n, it); + PARSE_VALUE(exchange_order_id_u64, n, it); +} + +void to_json(nlohmann::json &j, const RoutingRecord &r) { + WRITE_IF_NOT_EMPTY(r, session); + WRITE_IF_NOT_EMPTY(r, account); + WRITE_IF_NOT_EMPTY(r, operator_id); + WRITE_VALUE(r, cti_code); + WRITE_VALUE(r, customer_or_firm); + WRITE_IF_NOT_EMPTY(r, smp_id); + WRITE_VALUE(r, smp_instruction); + WRITE_IF_NOT_EMPTY(r, location); + WRITE_IF_NOT_EMPTY(r, cust_order_handing_inst); + WRITE_IF_NOT_EMPTY(r, sender_compid); + WRITE_IF_NOT_EMPTY(r, clearing_firm); + WRITE_IF_NOT_EMPTY(r, customer_account_ref_id); + WRITE_IF_NOT_EMPTY(r, account_code); + WRITE_IF_NOT_EMPTY(r, company_id); + WRITE_IF_NOT_EMPTY(r, authorized_trader); + WRITE_IF_NOT_EMPTY(r, authorized_group); + WRITE_IF_NOT_EMPTY(r, cust_order_handing_inst); + WRITE_IF_NOT_EMPTY(r, order_server_id); +} + +void from_json(const nlohmann::json &j, RoutingRecord &r) { + nlohmann::json::const_iterator it; + PARSE_STRING(session, r, it); + PARSE_STRING(account, r, it); + PARSE_STRING(operator_id, r, it); + PARSE_VALUE(cti_code, r, it); + PARSE_VALUE(customer_or_firm, r, it); + PARSE_STRING(smp_id, r, it); + PARSE_VALUE(smp_instruction, r, it); + PARSE_STRING(location, r, it); + PARSE_STRING(cust_order_handling_inst, r, it); + PARSE_STRING(sender_compid, r, it); + PARSE_STRING(clearing_firm, r, it); + PARSE_STRING(customer_account_ref_id, r, it); + PARSE_STRING(account_code, r, it); + PARSE_STRING(company_id, r, it); + PARSE_STRING(authorized_trader, r, it); + PARSE_STRING(authorized_group, r, it); + PARSE_STRING(cust_order_handing_inst, r, it); + PARSE_STRING(order_server_id, r, it); +} + +} // namespace bts::api::orderman diff --git a/src/lib/api/services/OrderService.h b/src/lib/api/services/OrderService.h index ea0dd855d..c54978535 100644 --- a/src/lib/api/services/OrderService.h +++ b/src/lib/api/services/OrderService.h @@ -1,6 +1,10 @@ #pragma once #include "lib/api/services/OrderService.pb.h" +#include "lib/api/services/native_order_type.h" +#include "lib/api/services/order_status.h" +#include "lib/api/services/time_in_force.h" +#include "lib/api/services/trigger_price_type.h" #include "lib/core/EnumMacros.h" #include "lib/core/FMT_IMPL.h" #include "lib/io/JsonReader.h" @@ -10,22 +14,61 @@ namespace bts::api::orderman { -PB_ENUM_FROM_JSON(OrderStatus) -PB_ENUM_TO_JSON(OrderStatus) +PB_ENUM_FROM_JSON(Fill_FillKind) +PB_ENUM_TO_JSON(Fill_FillKind) -PB_ENUM_FROM_JSON(TriggerPriceType) -PB_ENUM_TO_JSON(TriggerPriceType) +PB_ENUM_FROM_JSON(Fill_FillStatus) +PB_ENUM_TO_JSON(Fill_FillStatus) + +void to_json(nlohmann::json &j, const Fill &f); +void from_json(const nlohmann::json &j, Fill &f); + +PB_ENUM_FROM_JSON(OrderSubType) +PB_ENUM_TO_JSON(OrderSubType) + +PB_ENUM_FROM_JSON(SelfMatchPreventionInstruction) +PB_ENUM_TO_JSON(SelfMatchPreventionInstruction) + +PB_ENUM_FROM_JSON(CustomerOrFirm) +PB_ENUM_TO_JSON(CustomerOrFirm) + +void to_json(nlohmann::json &j, const RoutingRecord &r); +void from_json(const nlohmann::json &j, RoutingRecord &r); + +PB_ENUM_FROM_JSON(NativeOrderState_SubError) +PB_ENUM_TO_JSON(NativeOrderState_SubError) + +void to_json(nlohmann::json &j, const NativeOrderState &n); +void from_json(const nlohmann::json &j, NativeOrderState &n); } // namespace bts::api::orderman FMT_IMPL( - bts::api::orderman::OrderStatus, - s, + bts::api::orderman::Fill_FillKind, + k, "{}", - bts::api::orderman::OrderStatus_Name(s)); + bts::api::orderman::Fill_FillKind_Name(k)); FMT_IMPL( - bts::api::orderman::TriggerPriceType, + bts::api::orderman::Fill_FillStatus, + s, + "{}", + bts::api::orderman::Fill_FillStatus(s)); + +FMT_IMPL( + bts::api::orderman::OrderSubType, t, "{}", - bts::api::orderman::TriggerPriceType_Name(t)); + bts::api::orderman::OrderSubType_Name(t)); + +FMT_IMPL( + bts::api::orderman::SelfMatchPreventionInstruction, + s, + "{}", + bts::api::orderman::SelfMatchPreventionInstruction_Name(s)); + +FMT_IMPL( + bts::api::orderman::NativeOrderState_SubError, + s, + "{}", + bts::api::orderman::NativeOrderState_SubError_Name(s)); diff --git a/src/lib/api/services/native_order_type.h b/src/lib/api/services/native_order_type.h new file mode 100644 index 000000000..6fd5bfb64 --- /dev/null +++ b/src/lib/api/services/native_order_type.h @@ -0,0 +1,57 @@ +#pragma once + +#include "lib/api/services/OrderService.pb.h" +#include "lib/core/EnumMacros.h" +#include "lib/core/FMT_IMPL.h" +#include "lib/io/JsonReader.h" +#include "lib/logging/logging.h" +#include "lib/utils/string_utils.h" + +#include + +namespace bts::api::orderman { + +/** + * Parses NativeOrderType from a string. Unlike `bts::api::orderman::NativeOrderType_Parse`, + * this implementation is case insensitive, and makes the 'NATIVE_ORDER_TYPE_' prefix optional. + */ +bool native_order_type_parse_smart( + const std::string &str, NativeOrderType *result); + +PB_ENUM_FROM_JSON(NativeOrderType) +PB_ENUM_TO_JSON(NativeOrderType) + +std::ostream &operator<<(std::ostream &out, NativeOrderType t) noexcept; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline bool native_order_type_parse_smart( + const std::string &str, NativeOrderType *result) { + if (NativeOrderType_Parse(str, result)) { + return true; + } + std::string s = bts::system::string_utils::copy_to_upper(str); + if (NativeOrderType_Parse(s, result)) { + return true; + } + const char *prefix = "NATIVE_ORDER_TYPE_"; + if (!s.starts_with(prefix)) { + s.insert(0, prefix); + } + return NativeOrderType_Parse(s, result); +} + +inline std::ostream &operator<<(std::ostream &out, NativeOrderType t) noexcept { + out << NativeOrderType_Name(t); + return out; +} + +} // namespace bts::api::orderman + +FMT_IMPL( + bts::api::orderman::NativeOrderType, + t, + "{}", + bts::api::orderman::NativeOrderType_Name(t)); diff --git a/src/lib/api/services/order_status.h b/src/lib/api/services/order_status.h new file mode 100644 index 000000000..a0377b417 --- /dev/null +++ b/src/lib/api/services/order_status.h @@ -0,0 +1,56 @@ +#pragma once + +#include "lib/api/services/OrderService.pb.h" +#include "lib/core/EnumMacros.h" +#include "lib/core/FMT_IMPL.h" +#include "lib/io/JsonReader.h" +#include "lib/logging/logging.h" +#include "lib/utils/string_utils.h" + +#include + +namespace bts::api::orderman { + +/** + * Parses OrderStatus from a string. Unlike `bts::api::orderman::OrderStatus_Parse`, + * this implementation is case insensitive, and makes the 'ORDER_STATUS_' prefix optional. + */ +bool order_status_parse_smart(const std::string &str, OrderStatus *result); + +PB_ENUM_FROM_JSON(OrderStatus) +PB_ENUM_TO_JSON(OrderStatus) + +std::ostream &operator<<(std::ostream &out, OrderStatus s) noexcept; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline bool order_status_parse_smart( + const std::string &str, OrderStatus *result) { + if (OrderStatus_Parse(str, result)) { + return true; + } + std::string s = bts::system::string_utils::copy_to_upper(str); + if (OrderStatus_Parse(s, result)) { + return true; + } + const char *prefix = "ORDER_STATUS_"; + if (!s.starts_with(prefix)) { + s.insert(0, prefix); + } + return OrderStatus_Parse(s, result); +} + +inline std::ostream &operator<<(std::ostream &out, OrderStatus s) noexcept { + out << OrderStatus_Name(s); + return out; +} + +} // namespace bts::api::orderman + +FMT_IMPL( + bts::api::orderman::OrderStatus, + s, + "{}", + bts::api::orderman::OrderStatus_Name(s)); diff --git a/src/lib/api/services/time_in_force.h b/src/lib/api/services/time_in_force.h new file mode 100644 index 000000000..b651b16bc --- /dev/null +++ b/src/lib/api/services/time_in_force.h @@ -0,0 +1,56 @@ +#pragma once + +#include "lib/api/services/OrderService.pb.h" +#include "lib/core/EnumMacros.h" +#include "lib/core/FMT_IMPL.h" +#include "lib/io/JsonReader.h" +#include "lib/logging/logging.h" +#include "lib/utils/string_utils.h" + +#include + +namespace bts::api::orderman { + +/** + * Parses TimeInForce from a string. Unlike `bts::api::orderman::TimeInForce_Parse`, + * this implementation is case insensitive, and makes the 'TIME_IN_FORCE_' prefix optional. + */ +bool time_in_force_parse_smart(const std::string &str, TimeInForce *result); + +PB_ENUM_FROM_JSON(TimeInForce) +PB_ENUM_TO_JSON(TimeInForce) + +std::ostream &operator<<(std::ostream &out, TimeInForce tif) noexcept; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline bool time_in_force_parse_smart( + const std::string &str, TimeInForce *result) { + if (TimeInForce_Parse(str, result)) { + return true; + } + std::string s = bts::system::string_utils::copy_to_upper(str); + if (TimeInForce_Parse(s, result)) { + return true; + } + const char *prefix = "TIME_IN_FORCE_"; + if (!s.starts_with(prefix)) { + s.insert(0, prefix); + } + return TimeInForce_Parse(s, result); +} + +inline std::ostream &operator<<(std::ostream &out, TimeInForce tif) noexcept { + out << TimeInForce_Name(tif); + return out; +} + +} // namespace bts::api::orderman + +FMT_IMPL( + bts::api::orderman::TimeInForce, + v, + "{}", + bts::api::orderman::TimeInForce_Name(v)); diff --git a/src/lib/api/services/trigger_price_type.h b/src/lib/api/services/trigger_price_type.h new file mode 100644 index 000000000..53d9da320 --- /dev/null +++ b/src/lib/api/services/trigger_price_type.h @@ -0,0 +1,58 @@ +#pragma once + +#include "lib/api/services/OrderService.pb.h" +#include "lib/core/EnumMacros.h" +#include "lib/core/FMT_IMPL.h" +#include "lib/io/JsonReader.h" +#include "lib/logging/logging.h" +#include "lib/utils/string_utils.h" + +#include + +namespace bts::api::orderman { + +/** + * Parses TriggerPriceType from a string. Unlike `bts::api::orderman::TriggerPriceType_Parse`, + * this implementation is case insensitive, and makes the 'TRIGGER_PRICE_TYPE_' prefix optional. + */ +bool trigger_price_type_parse_smart( + const std::string &str, TriggerPriceType *result); + +PB_ENUM_FROM_JSON(TriggerPriceType) +PB_ENUM_TO_JSON(TriggerPriceType) + +std::ostream &operator<<(std::ostream &out, TriggerPriceType t) noexcept; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline bool trigger_price_type_parse_smart( + const std::string &str, TriggerPriceType *result) { + if (TriggerPriceType_Parse(str, result)) { + return true; + } + std::string s = bts::system::string_utils::copy_to_upper(str); + if (TriggerPriceType_Parse(s, result)) { + return true; + } + const char *prefix = "TRIGGER_PRICE_TYPE_"; + if (!s.starts_with(prefix)) { + s.insert(0, prefix); + } + return TriggerPriceType_Parse(s, result); +} + +inline std::ostream &operator<<( + std::ostream &out, TriggerPriceType t) noexcept { + out << TriggerPriceType_Name(t); + return out; +} + +} // namespace bts::api::orderman + +FMT_IMPL( + bts::api::orderman::TriggerPriceType, + t, + "{}", + bts::api::orderman::TriggerPriceType_Name(t)); diff --git a/src/lib/core/TimeStamp.cpp b/src/lib/core/TimeStamp.cpp index 1c812b5eb..e9d0dbf83 100644 --- a/src/lib/core/TimeStamp.cpp +++ b/src/lib/core/TimeStamp.cpp @@ -1,6 +1,7 @@ #include "TimeStamp.h" #include "lib/utils/datetime.h" #include +#include namespace bts::core { @@ -43,6 +44,12 @@ bool TimeStamp::from_ymd( return true; } +void TimeStamp::to_protobuf(google::protobuf::Timestamp &ts) const { + ts.set_seconds(seconds()); + bts_assert(nanos() >= seconds() * 1'000'000'000); + ts.set_nanos(nanos() - seconds() * 1'000'000'000); +} + TimeStamp TimeStamp::from_iso_string(const std::string &iso_string) { auto ptime = datetime::parse_iso_date_time_string(iso_string); return from_ptime(ptime); diff --git a/src/lib/core/TimeStamp.h b/src/lib/core/TimeStamp.h index 472806010..d185fc728 100644 --- a/src/lib/core/TimeStamp.h +++ b/src/lib/core/TimeStamp.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -87,6 +88,7 @@ struct TimeStamp { // Gets a TimeStamp representing this date at midnight. TimeStamp date() const; constexpr boost::posix_time::ptime to_ptime() const; + void to_protobuf(google::protobuf::Timestamp &ts) const; std::string to_iso_string() const; // current epoch ns static uint64_t now_ns() { diff --git a/src/lib/domain_model/PutOrCall.h b/src/lib/domain_model/PutOrCall.h new file mode 100644 index 000000000..b26b94b3c --- /dev/null +++ b/src/lib/domain_model/PutOrCall.h @@ -0,0 +1,52 @@ +#pragma once + +#include "lib/core/EnumMacros.h" +#include "lib/core/FMT_IMPL.h" +#include "lib/domain_model/Security.pb.h" +#include "lib/io/JsonReader.h" +#include "lib/logging/logging.h" +#include "lib/utils/string_utils.h" + +#include + +namespace bts::api { + +/** + * Parses PutOrCall from a string. Unlike `bts::api::SecurityType_Parse`, this + * implementation is case insensitive, and makes the 'PUTORCALL_' prefix optional. + */ +bool put_or_call_parse_smart(const std::string &str, PutOrCall *result); + +PB_ENUM_FROM_JSON(PutOrCall) +PB_ENUM_TO_JSON(PutOrCall) + +std::ostream &operator<<(std::ostream &out, const PutOrCall &poc) noexcept; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline bool put_or_call_parse_smart(const std::string &str, PutOrCall *result) { + if (PutOrCall_Parse(str, result)) { + return true; + } + std::string s = bts::system::string_utils::copy_to_upper(str); + if (PutOrCall_Parse(s, result)) { + return true; + } + const char *prefix = "PUTORCALL_"; + if (!s.starts_with(prefix)) { + s.insert(0, prefix); + } + return PutOrCall_Parse(s, result); +} + +inline std::ostream &operator<<( + std::ostream &out, const PutOrCall &poc) noexcept { + out << PutOrCall_Name(poc); + return out; +} + +} // namespace bts::api + +FMT_IMPL(bts::api::PutOrCall, poc, "{}", bts::api::PutOrCall_Name(poc)); diff --git a/src/lib/domain_model/Security.cpp b/src/lib/domain_model/Security.cpp index f3bc087e7..8829a4424 100644 --- a/src/lib/domain_model/Security.cpp +++ b/src/lib/domain_model/Security.cpp @@ -2,10 +2,13 @@ #include "lib/config/config.h" #include "lib/config/protos/orders_fills_cleanup.pb.h" #include "lib/domain_model/Security.pb.h" +#include "lib/io/JsonMacros.h" #include "lib/md/common.h" +#include "lib/utils/pb_json_macros.h" #include #include +#include namespace bts { namespace securities { @@ -191,3 +194,389 @@ std::string unique_descriptor(const api::Security &security) { } // namespace securities } // namespace bts + +namespace bts::api { + +using nlohmann::json; +using std::uint32_t; + +void to_json(json &j, const api::ContractQuantityInfo &cqi) { + PB_JSON_WRITE_IF_NOT_ZERO(j, cqi, min_value); + PB_JSON_WRITE_IF_NOT_ZERO(j, cqi, max_value); + PB_JSON_WRITE_IF_NOT_ZERO(j, cqi, tick_unit); + PB_JSON_WRITE_IF_NOT_ZERO(j, cqi, tick_base); + PB_JSON_WRITE_IF_NOT_ZERO(j, cqi, multiplier); + PB_JSON_WRITE_IF_NOT_ZERO(j, cqi, max_price); +} + +void from_json(const json &j, api::ContractQuantityInfo &cqi) { + json::const_iterator it; + PB_JSON_READ_VALUE(j, cqi, min_value, it); + PB_JSON_READ_VALUE(j, cqi, max_value, it); + PB_JSON_READ_VALUE(j, cqi, max_value, it); + PB_JSON_READ_VALUE(j, cqi, tick_unit, it); + PB_JSON_READ_VALUE(j, cqi, tick_base, it); + PB_JSON_READ_VALUE(j, cqi, max_price, it); +} + +void to_json(json &j, const api::ContractSpecification &spec) { + PB_JSON_WRITE_IF_NOT_ZERO(j, spec, low_tick); + PB_JSON_WRITE_IF_NOT_ZERO(j, spec, price_break); + PB_JSON_WRITE_IF_NOT_ZERO(j, spec, high_tick); + PB_JSON_WRITE_IF_NOT_ZERO(j, spec, cash_per_point); + PB_JSON_WRITE_IF_NOT_ZERO(j, spec, price_multiplier); + PB_JSON_WRITE_IF_NOT_ZERO(j, spec, main_fraction); + PB_JSON_WRITE_IF_NOT_ZERO(j, spec, sub_fraction); + PB_JSON_WRITE_IF_NOT_ZERO(j, spec, price_display_format); + PB_JSON_WRITE_IF_NOT_EMPTY(j, spec, quantity_infos); + PB_JSON_WRITE_IF_NOT_EMPTY(j, spec, notional_quantity_infos); +} + +void from_json(const json &j, api::ContractSpecification &spec) { + json::const_iterator it; + PB_JSON_READ_VALUE(j, spec, low_tick, it); + PB_JSON_READ_VALUE(j, spec, price_break, it); + PB_JSON_READ_VALUE(j, spec, high_tick, it); + PB_JSON_READ_VALUE(j, spec, cash_per_point, it); + PB_JSON_READ_VALUE(j, spec, price_multiplier, it); + PB_JSON_READ_VALUE(j, spec, main_fraction, it); + PB_JSON_READ_VALUE(j, spec, sub_fraction, it); + PB_JSON_READ_VALUE(j, spec, price_display_format, it); + PB_JSON_READ_ARRAY(j, spec, quantity_infos, it); + PB_JSON_READ_ARRAY(j, spec, notional_quantity_infos, it); +} + +void to_json(json &, const SpotContract &) {} +void from_json(const json &, SpotContract &) {} + +template +static void exp_to_json(json &j, const ExpContract &ec) { + j = bts::core::TimeStamp::from_ymd( + ec.expiration_year(), ec.expiration_month(), ec.expiration_day()) + .to_iso_string(); +} + +template +static void exp_from_json(const json &j, ExpContract &ec) { + const auto dt = bts::core::TimeStamp::from_iso_string(j.get()); + uint32_t y, m, d; + dt.to_date(y, m, d); + ec.set_expiration_year(y); + ec.set_expiration_month(m); + ec.set_expiration_day(d); +} + +void to_json(json &j, const FutureContract &f) { + exp_to_json(j["expiration"], f); +} + +void from_json(const json &j, FutureContract &f) { + const auto it = j.find("expiration"); + if (it != j.end()) { + exp_from_json(*it, f); + } +} + +void to_json(json &j, const OptionContract &opt) { + exp_to_json(j["expiration"], opt); + PB_JSON_WRITE_VALUE(j, opt, strike); + PB_JSON_WRITE_VALUE(j, opt, call_put); +} + +void from_json(const json &j, OptionContract &opt) { + json::const_iterator it; + it = j.find("expiration"); + if (it != j.end()) { + exp_from_json(*it, opt); + } + PB_JSON_READ_VALUE(j, opt, strike, it); + PB_JSON_READ_VALUE(j, opt, call_put, it); +} + +void to_json(json &j, const ContractLeg &leg) { + PB_JSON_WRITE_STRUCT(j, leg, security); + PB_JSON_WRITE_VALUE(j, leg, quantity); + PB_JSON_WRITE_VALUE(j, leg, side); + PB_JSON_WRITE_VALUE(j, leg, price_multiplier); + PB_JSON_WRITE_IF_NOT_ZERO(j, leg, leg_delta); + PB_JSON_WRITE_VALUE(j, leg, price_calc); +} + +void from_json(const json &j, ContractLeg &leg) { + json::const_iterator it; + PB_JSON_READ_STRUCT(j, leg, security, it); + PB_JSON_READ_VALUE(j, leg, quantity, it); + PB_JSON_READ_VALUE(j, leg, side, it); + PB_JSON_READ_VALUE(j, leg, price_multiplier, it); + PB_JSON_READ_VALUE(j, leg, leg_delta, it); + PB_JSON_READ_VALUE(j, leg, price_calc, it); +} + +void to_json(json &j, const SpreadContract &spd) { + PB_JSON_WRITE_IF_NOT_EMPTY(j, spd, legs); +} + +void from_json(const json &j, SpreadContract &spd) { + json::const_iterator it; + PB_JSON_READ_ARRAY(j, spd, legs, it); +} + +void to_json(json &j, const SyntheticContract &syn) { + PB_JSON_WRITE_IF_NOT_EMPTY(j, syn, name); + PB_JSON_WRITE_IF_NOT_ZERO(j, syn, user_id); + PB_JSON_WRITE_IF_NOT_EMPTY(j, syn, legs); + PB_JSON_WRITE_VALUE(j, syn, price_calc); +} + +void from_json(const json &j, SyntheticContract &syn) { + json::const_iterator it; + PB_JSON_READ_STRING(j, syn, name, it); + PB_JSON_READ_VALUE(j, syn, user_id, it); + PB_JSON_READ_ARRAY(j, syn, legs, it); + PB_JSON_READ_VALUE(j, syn, price_calc, it); +} + +void to_json(json &j, const GLBXExchangeData &data) { + PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, cfi_code); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, cme_symbol); + PB_JSON_WRITE_IF_NOT_ZERO(j, data, market_segment_id); +} + +void from_json(const json &j, GLBXExchangeData &data) { + json::const_iterator it; + PB_JSON_READ_VALUE(j, data, exchange_id, it); + PB_JSON_READ_STRING(j, data, security_desc, it); + PB_JSON_READ_STRING(j, data, cfi_code, it); + PB_JSON_READ_STRING(j, data, cme_symbol, it); + PB_JSON_READ_VALUE(j, data, market_segment_id, it); +} + +void to_json(json &j, const CBOEExchangeData &data) { + PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc); + PB_JSON_WRITE_IF_NOT_ZERO(j, data, class_key); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, source); +} + +void from_json(const json &j, CBOEExchangeData &data) { + json::const_iterator it; + PB_JSON_READ_VALUE(j, data, exchange_id, it); + PB_JSON_READ_STRING(j, data, security_desc, it); + PB_JSON_READ_VALUE(j, data, class_key, it); + PB_JSON_READ_STRING(j, data, source, it); +} + +void to_json(json &j, const CBOECFEExchangeData &data) { + PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc); + PB_JSON_WRITE_IF_NOT_ZERO(j, data, class_key); +} + +void from_json(const json &j, CBOECFEExchangeData &data) { + json::const_iterator it; + PB_JSON_READ_VALUE(j, data, exchange_id, it); + PB_JSON_READ_STRING(j, data, security_desc, it); + PB_JSON_READ_VALUE(j, data, class_key, it); +} + +void to_json(json &j, const ICEExchangeData &data) { + PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc); + PB_JSON_WRITE_IF_NOT_ZERO(j, data, price_denominator); + PB_JSON_WRITE_IF_NOT_ZERO(j, data, deal_denominator); + PB_JSON_WRITE_IF_NOT_ZERO(j, data, settle_denominator); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, market_description); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, cleared_alias); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, product_name); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, hub_alias); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, strip_name); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, mic_code); + PB_JSON_WRITE_IF_TRUE(j, data, is_strip); +} + +void from_json(const json &j, ICEExchangeData &data) { + json::const_iterator it; + PB_JSON_READ_VALUE(j, data, exchange_id, it); + PB_JSON_READ_STRING(j, data, security_desc, it); + PB_JSON_READ_VALUE(j, data, price_denominator, it); + PB_JSON_READ_VALUE(j, data, deal_denominator, it); + PB_JSON_READ_VALUE(j, data, settle_denominator, it); + PB_JSON_READ_STRING(j, data, market_description, it); + PB_JSON_READ_STRING(j, data, cleared_alias, it); + PB_JSON_READ_STRING(j, data, product_name, it); + PB_JSON_READ_STRING(j, data, hub_alias, it); + PB_JSON_READ_STRING(j, data, strip_name, it); + PB_JSON_READ_STRING(j, data, mic_code, it); + PB_JSON_READ_VALUE(j, data, is_strip, it); +} + +void to_json(json &j, const TAIFEXExchangeData &data) { + PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc); + PB_JSON_WRITE_IF_NOT_ZERO(j, data, class_key); +} + +void from_json(const json &j, TAIFEXExchangeData &data) { + json::const_iterator it; + PB_JSON_READ_VALUE(j, data, exchange_id, it); + PB_JSON_READ_STRING(j, data, security_desc, it); + PB_JSON_READ_VALUE(j, data, class_key, it); +} + +void to_json(json &j, const DeribitExchangeData &data) { + PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc); +} + +void from_json(const json &j, DeribitExchangeData &data) { + json::const_iterator it; + PB_JSON_READ_VALUE(j, data, exchange_id, it); + PB_JSON_READ_STRING(j, data, security_desc, it); +} + +void to_json(json &j, const FTXExchangeData &data) { + PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc); +} + +void from_json(const json &j, FTXExchangeData &data) { + json::const_iterator it; + PB_JSON_READ_VALUE(j, data, exchange_id, it); + PB_JSON_READ_STRING(j, data, security_desc, it); +} + +void to_json(json &j, const BinanceExchangeData &data) { + PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc); +} + +void from_json(const json &j, BinanceExchangeData &data) { + json::const_iterator it; + PB_JSON_READ_VALUE(j, data, exchange_id, it); + PB_JSON_READ_STRING(j, data, security_desc, it); +} + +void to_json(json &j, const BinanceUsdsFuturesExchangeData &data) { + PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc); +} + +void from_json(const json &j, BinanceUsdsFuturesExchangeData &data) { + json::const_iterator it; + PB_JSON_READ_VALUE(j, data, exchange_id, it); + PB_JSON_READ_STRING(j, data, security_desc, it); +} + +void to_json(json &j, const BinanceCoinFuturesExchangeData &data) { + PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc); +} + +void from_json(const json &j, BinanceCoinFuturesExchangeData &data) { + json::const_iterator it; + PB_JSON_READ_VALUE(j, data, exchange_id, it); + PB_JSON_READ_STRING(j, data, security_desc, it); +} + +void to_json(json &j, const BinanceUsExchangeData &data) { + PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc); +} + +void from_json(const json &j, BinanceUsExchangeData &data) { + json::const_iterator it; + PB_JSON_READ_VALUE(j, data, exchange_id, it); + PB_JSON_READ_STRING(j, data, security_desc, it); +} + +void to_json(json &j, const OkxExchangeData &data) { + PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc); +} + +void from_json(const json &j, OkxExchangeData &data) { + json::const_iterator it; + PB_JSON_READ_VALUE(j, data, exchange_id, it); + PB_JSON_READ_STRING(j, data, security_desc, it); +} + +void to_json(json &j, const CoinbaseExchangeData &data) { + PB_JSON_WRITE_IF_NOT_ZERO(j, data, exchange_id); + PB_JSON_WRITE_IF_NOT_EMPTY(j, data, security_desc); +} + +void from_json(const json &j, CoinbaseExchangeData &data) { + json::const_iterator it; + PB_JSON_READ_VALUE(j, data, exchange_id, it); + PB_JSON_READ_STRING(j, data, security_desc, it); +} + +void to_json(json &j, const Security &s) { + // clang-format off +#define CONTRACT_ONEOF(oneof) \ + oneof(j, s, Spot, spot) \ + oneof(j, s, Future, future) \ + oneof(j, s, Option, option) \ + oneof(j, s, Spread, spread) \ + oneof(j, s, Synthetic, synthetic) + +#define EXCHANGE_DATA_ONEOF(oneof) \ + oneof(j, s, Glbx, glbx) \ + oneof(j, s, Cboe, cboe) \ + oneof(j, s, Cfe, cfe) \ + oneof(j, s, Ice, ice) \ + oneof(j, s, Deribit, deribit) \ + oneof(j, s, Ftx, ftx) \ + oneof(j, s, Binance, binance) \ + oneof(j, s, BinanceCoinFutures, binance_coin_futures) \ + oneof(j, s, BinanceUsdsFutures, binance_usds_futures) \ + oneof(j, s, BinanceUs, binance_us) \ + oneof(j, s, Coinbase, coinbase) \ + oneof(j, s, Okx, okx) \ + oneof(j, s, Taifex, taifex) + // clang-format on + + PB_JSON_WRITE_IF_NOT_ZERO(j, s, id); + PB_JSON_WRITE_IF_NOT_EMPTY(j, s, symbol); + PB_JSON_WRITE_VALUE(j, s, exchange); + PB_JSON_WRITE_STRUCT(j, s, contract_spec); + PB_JSON_WRITE_IF_NOT_EMPTY(j, s, currency); + PB_JSON_WRITE_ONEOF(j, s, Contract, CONTRACT_ONEOF); + PB_JSON_WRITE_ONEOF(j, s, ExchangeData, EXCHANGE_DATA_ONEOF); +} + +void from_json(const json &j, Security &s) { + json::const_iterator it; + +#define SEC_TYPE_ONEOF(oneof) \ + oneof(j, s, future, it); \ + oneof(j, s, option, it); \ + oneof(j, s, spread, it); \ + oneof(j, s, synthetic, it) + +#define CONTRACT_CASE_ONEOF(oneof) \ + oneof(j, s, glbx, it); \ + oneof(j, s, cboe, it); \ + oneof(j, s, cfe, it); \ + oneof(j, s, ice, it); \ + oneof(j, s, deribit, it); \ + oneof(j, s, ftx, it); \ + oneof(j, s, binance, it); \ + oneof(j, s, binance_usds_futures, it); \ + oneof(j, s, binance_coin_futures, it); \ + oneof(j, s, binance_us, it); \ + oneof(j, s, coinbase, it); \ + oneof(j, s, okx, it); \ + oneof(j, s, taifex, it); + + PB_JSON_READ_VALUE(j, s, id, it); + PB_JSON_READ_STRING(j, s, symbol, it); + PB_JSON_READ_STRING(j, s, currency, it); + PB_JSON_READ_STRUCT(j, s, contract_spec, it); + PB_JSON_READ_ONEOFS(SEC_TYPE_ONEOF); + PB_JSON_READ_ONEOFS(CONTRACT_CASE_ONEOF); +} + +} // namespace bts::api diff --git a/src/lib/domain_model/Security.h b/src/lib/domain_model/Security.h index e94087c01..53f160aeb 100644 --- a/src/lib/domain_model/Security.h +++ b/src/lib/domain_model/Security.h @@ -1,6 +1,7 @@ #pragma once #include "lib/core/FMT_IMPL.h" +#include "lib/domain_model/PutOrCall.h" #include "lib/domain_model/Security.pb.h" #include "lib/domain_model/SecurityType.h" #include "lib/domain_model/Side.h" @@ -9,6 +10,7 @@ #include "lib/utils/bts_assert.h" #include +#include namespace boost { namespace gregorian { @@ -16,14 +18,83 @@ class date; } } // namespace boost -namespace bts { +namespace bts::api { -namespace api { class Security; class ICEExchangeData; -} // namespace api -namespace securities { +void to_json(nlohmann::json &j, const ContractQuantityInfo &c); +void from_json(const nlohmann::json &j, ContractQuantityInfo &c); + +void to_json(nlohmann::json &j, const ContractSpecification &c); +void from_json(const nlohmann::json &j, ContractSpecification &c); + +void to_json(nlohmann::json &j, const ContractLeg &c); +void from_json(const nlohmann::json &j, ContractLeg &c); + +void to_json(nlohmann::json &j, const SpotContract &c); +void from_json(const nlohmann::json &j, SpotContract &c); + +void to_json(nlohmann::json &j, const FutureContract &c); +void from_json(const nlohmann::json &j, FutureContract &c); + +void to_json(nlohmann::json &j, const OptionContract &c); +void from_json(const nlohmann::json &j, OptionContract &c); + +void to_json(nlohmann::json &j, const SpreadContract &c); +void from_json(const nlohmann::json &j, SpreadContract &c); + +void to_json(nlohmann::json &j, const SyntheticContract_PriceCalc &c); +void from_json(const nlohmann::json &j, SyntheticContract_PriceCalc &c); + +void to_json(nlohmann::json &j, const SyntheticContract &c); +void from_json(const nlohmann::json &j, SyntheticContract &c); + +void to_json(nlohmann::json &j, const GLBXExchangeData &data); +void from_json(const nlohmann::json &j, GLBXExchangeData &data); + +void to_json(nlohmann::json &j, const CBOEExchangeData &data); +void from_json(const nlohmann::json &j, CBOEExchangeData &data); + +void to_json(nlohmann::json &j, const CBOECFEExchangeData &data); +void from_json(const nlohmann::json &j, CBOECFEExchangeData &data); + +void to_json(nlohmann::json &j, const ICEExchangeData &data); +void from_json(const nlohmann::json &j, ICEExchangeData &data); + +void to_json(nlohmann::json &j, const DeribitExchangeData &data); +void from_json(const nlohmann::json &j, DeribitExchangeData &data); + +void to_json(nlohmann::json &j, const FTXExchangeData &data); +void from_json(const nlohmann::json &j, FTXExchangeData &data); + +void to_json(nlohmann::json &j, const BinanceExchangeData &data); +void from_json(const nlohmann::json &j, BinanceExchangeData &data); + +void to_json(nlohmann::json &j, const BinanceUsdsFuturesExchangeData &data); +void from_json(const nlohmann::json &j, BinanceUsdsFuturesExchangeData &data); + +void to_json(nlohmann::json &j, const BinanceCoinFuturesExchangeData &data); +void from_json(const nlohmann::json &j, BinanceCoinFuturesExchangeData &data); + +void to_json(nlohmann::json &j, const BinanceUsExchangeData &data); +void from_json(const nlohmann::json &j, BinanceUsExchangeData &data); + +void to_json(nlohmann::json &j, const OkxExchangeData &data); +void from_json(const nlohmann::json &j, OkxExchangeData &data); + +void to_json(nlohmann::json &j, const CoinbaseExchangeData &data); +void from_json(const nlohmann::json &j, CoinbaseExchangeData &data); + +void to_json(nlohmann::json &j, const Security &s); +void from_json(const nlohmann::json &j, Security &s); + +PB_ENUM_FROM_JSON(SyntheticContract_PriceCalc); +PB_ENUM_TO_JSON(SyntheticContract_PriceCalc); + +} // namespace bts::api + +namespace bts::securities { /** * Gets a descriptor string for a security (should be unique within a particular exchange). @@ -60,7 +131,8 @@ inline api::Side other_side(api::Side side) { return side; } } -template class TwoSided { +template +class TwoSided { public: TwoSided() : buy(), sell() {} @@ -104,7 +176,8 @@ inline bool is_worse(api::Side s, Price p1, Price p2) { return is_better(s, p2, p1); } -template void apply_to_both_sides(C c) { +template +void apply_to_both_sides(C c) { c(api::SIDE_BUY); c(api::SIDE_SELL); } @@ -178,8 +251,8 @@ inline Price encode_exchange_price(api::Security const &sec, Price in) { return in; } -inline double -raw_round_to_quote_price(double raw_price, double tick_size, api::Side side) { +inline double raw_round_to_quote_price( + double raw_price, double tick_size, api::Side side) { double factor = 1000000000; auto i_price = llround(factor * raw_price); auto i_tick_size = llround(tick_size * factor); @@ -222,8 +295,8 @@ inline double checked_round_to_quote_price( } } -inline Price -round_to_quote_price(api::Security const &sec, Price in, api::Side side) { +inline Price round_to_quote_price( + api::Security const &sec, Price in, api::Side side) { auto p = in.value(); bts_assert_d(!std::isnan(p)); if (p < -sec.contract_spec().price_break() || @@ -287,8 +360,8 @@ inline const std::string &descriptor( // Looking to round md::IntPrice? Look in IntPrice.h // computes price_2 - price_1 in ticks -inline int -price_diff_to_ticks(double price_1, double price_2, double tick_size) { +inline int price_diff_to_ticks( + double price_1, double price_2, double tick_size) { return static_cast(std::round((price_2 - price_1) / tick_size)); } // returns the difference (in ticks) bw and order and a market. @@ -312,8 +385,8 @@ inline int order_diff_market( // simple double equality can not be used to compare prices. // this function deems 2 prices to be equal if they are within // 1 % of the tick size -inline bool -test_price_equality(double price_1, double price_2, double tick_size) { +inline bool test_price_equality( + double price_1, double price_2, double tick_size) { return std::fabs(price_2 - price_1) < 0.01 * tick_size; } inline bool gr_than_eq(double p1, double p2, double tick_size) { @@ -362,7 +435,6 @@ inline bool is_spread_of_strips(const bts::api::Security &sec) { return false; } -} // namespace securities -} // namespace bts +} // namespace bts::securities FMT_IMPL(bts::api::Security, s, "{}", bts::securities::descriptor(s)); diff --git a/src/lib/utils/PBUtils.h b/src/lib/utils/PBUtils.h index b25c963b4..de35893ee 100644 --- a/src/lib/utils/PBUtils.h +++ b/src/lib/utils/PBUtils.h @@ -1,8 +1,10 @@ #pragma once +#include +#include #include #include -#include +#include #include #ifndef _WIN32 @@ -25,14 +27,16 @@ namespace utils { namespace pb { BTS_EXPORT std::string to_string(google::protobuf::Message const &msg); -BTS_EXPORT google::protobuf::Timestamp -to_timestamp(datetime::PTime const &ptime); +BTS_EXPORT google::protobuf::Timestamp to_timestamp( + datetime::PTime const &ptime); -template bool from_string(const std::string &str, T *out) { +template +bool from_string(const std::string &str, T *out) { return google::protobuf::TextFormat::ParseFromString(str, out); } -template T from_string(const std::string &str) { +template +T from_string(const std::string &str) { T result; bts_assert(google::protobuf::TextFormat::ParseFromString(str, &result)); return result; @@ -105,3 +109,36 @@ inline double to_double_time(google::protobuf::Timestamp const &t1) { } // namespace pb } // namespace utils } // namespace bts + +namespace google::protobuf { + +template < + class T, + typename std::enable_if_t< + nlohmann::detail::has_to_json::value, + int> = 0> +void to_json( + nlohmann::json &j, const google::protobuf::RepeatedPtrField &vals) { + j = nlohmann::json::array(); + for (const T &v : vals) { + to_json(j.emplace_back(), v); + } +} + +template < + class T, + typename std::enable_if_t< + nlohmann::detail::has_to_json::value, + int> = 0> +void from_json( + const nlohmann::json &j, google::protobuf::RepeatedPtrField &vals) { + vals.Clear(); + if (!j.is_array()) { + return; + } + for (auto it = j.begin(); it != j.end(); it++) { + it->get_to(*vals.Add()); + } +} + +} // namespace google::protobuf diff --git a/src/lib/utils/pb_json_macros.h b/src/lib/utils/pb_json_macros.h new file mode 100644 index 000000000..5364f5b55 --- /dev/null +++ b/src/lib/utils/pb_json_macros.h @@ -0,0 +1,67 @@ +#pragma once + +#define PB_JSON_READ_STRING(j, dest, name, it) \ + it = j.find(#name); \ + if (it != j.end()) { \ + dest.clear_##name(); \ + it->get_to(*dest.mutable_##name()); \ + } +#define PB_JSON_READ_VALUE(j, dest, name, it) \ + it = j.find(#name); \ + if (it != j.end()) { \ + dest.set_##name(it->get()); \ + } +#define PB_JSON_READ_STRUCT(j, dest, name, it) \ + it = j.find(#name); \ + if (it != j.end()) { \ + it->get_to(*dest.mutable_##name()); \ + } +#define PB_JSON_READ_ARRAY(j, dest, name, it) \ + it = j.find(#name); \ + if (it != j.end() && it->is_array()) { \ + dest.clear_##name(); \ + for (auto &pb_json_array_val : *it) { \ + pb_json_array_val.get_to(*dest.add_##name()); \ + } \ + } +#define PB_JSON_READ_ONEOF(j, dest, name, it) \ + if (!parsed_oneof) { \ + it = j.find(#name); \ + if (it != j.end()) { \ + it->get_to(*dest.mutable_##name()); \ + parsed_oneof = true; \ + } \ + } +#define PB_JSON_READ_ONEOFS(fields) \ + { \ + bool parsed_oneof = false; \ + fields(PB_JSON_READ_ONEOF) \ + } +#define PB_JSON_WRITE_IF_TRUE(j, src, name) \ + if (src.name()) { \ + j[#name] = src.name(); \ + } +#define PB_JSON_WRITE_IF_NOT_ZERO(j, src, name) \ + if (src.name() != 0) { \ + j[#name] = src.name(); \ + } +#define PB_JSON_WRITE_IF_NOT_EMPTY(j, src, name) \ + if (!src.name().empty()) { \ + j[#name] = src.name(); \ + } + +#define PB_JSON_WRITE_VALUE(j, src, name) j[#name] = src.name() +#define PB_JSON_WRITE_STRUCT(j, src, name) \ + if (src.has_##name()) { \ + j[#name] = src.name(); \ + } + +#define _PB_JSON_WRITE_ONEOF_CASE(j, src, enum_name, field_name) \ + case std::remove_const< \ + std::remove_reference::type>::type::k##enum_name: \ + PB_JSON_WRITE_VALUE(j, src, field_name); \ + break; +#define PB_JSON_WRITE_ONEOF(j, src, oneof_name, cases) \ + switch (src.oneof_name##_case()) { \ + cases(_PB_JSON_WRITE_ONEOF_CASE) default : break; \ + }