#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