msgpack / msgpack-c

MessagePack implementation for C and C++ / msgpack.org[C/C++]
Other
3.03k stars 883 forks source link

How to implement msgpack deserialized with Timestamp #1130

Open NickLin910221 opened 4 months ago

NickLin910221 commented 4 months ago

With C++ 11 https://github.com/msgpack/msgpack-c.git branch:cpp_master

std::cout << message << std::endl;
msgpack::object_handle oh = msgpack::unpack(message.data(), message.size());

msgpack::object deserialized = oh.get();

std::cout << deserialized << std::endl;

Tick tick;
deserialized.convert(tick);

std::cout << "Received Tick: " 
          << "exchange=" << tick.exchange
          << ", code=" << tick.code
          << ", timestamp=" << tick.timestamp.seconds << "." << tick.timestamp.nanoseconds
          << ", close=" << tick.close
          << ", volume=" << tick.volume
          << std::endl;

I sent the msgpack packet with python in {'exchange': 'TFE', 'code': 'TXFR1', 'timestamp': Timestamp(seconds=1718040379, nanoseconds=767436000), 'close': 21000.0, 'volume': 1.0, 'tick_Type': 0, 'simtrade': 1}

And I can receive msg payload in C++ as {"method":"Publish","exchange":"TFE","code":"TXFR1","timestamp":"EXT(type:-1,size:8)","close":21000,"volume":1,"tick_Type":0,"simtrade":1}

And How to deserialized with the ext. type with Timestamp.

I use origin msgpack support msgpack.Timestamp to encode the packet

redboltz commented 4 months ago

This test code could help you:

https://github.com/msgpack/msgpack-c/blob/820ccf1f1d919b98a0bbe1ed9e9a004e922a80fc/test/msgpack_cpp11.cpp#L873-L887

NickLin910221 commented 4 months ago

Thanks a lot. It inspired me and helpful.