|
| 1 | +#include <iostream> |
| 2 | +#include <string> |
| 3 | + |
| 4 | +#include <mfast.h> |
| 5 | +#include <mfast/coder/fast_decoder.h> |
| 6 | +#include <mfast/json/json.h> |
| 7 | +#include <mfast/xml_parser/dynamic_templates_description.h> |
| 8 | + |
| 9 | +using std::string; |
| 10 | +using std::ostringstream; |
| 11 | +using std::cout; |
| 12 | +using std::endl; |
| 13 | + |
| 14 | +using mfast::templates_description; |
| 15 | +using mfast::dynamic_templates_description; |
| 16 | +using mfast::fast_decoder; |
| 17 | +using mfast::message_cref; |
| 18 | +using mfast::ascii_string_cref; |
| 19 | +using mfast::json::encode; |
| 20 | + |
| 21 | +// example from http://jettekfix.com/node/36 |
| 22 | +static const string fast_template = |
| 23 | + "\ |
| 24 | +<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\ |
| 25 | +<templates xmlns=\"http://www.fixprotocol.org/ns/fast/td/1.1\">\ |
| 26 | + <template dictionary=\"1\" id=\"1\" name=\"HelloWorld\">\ |
| 27 | + <string id=\"58\" name=\"Text\">\ |
| 28 | + <default value=\"\"></default>\ |
| 29 | + </string>\ |
| 30 | + </template>\ |
| 31 | +</templates>\ |
| 32 | +"; |
| 33 | + |
| 34 | +// 58=HelloWorld<SOH> |
| 35 | +static const string fast_message = |
| 36 | + "\xE0\x81\x48\x65\x6C\x6C\x6F\x57\x6F\x72\x6C\xE4"; |
| 37 | + |
| 38 | +int main() { |
| 39 | + dynamic_templates_description description(fast_template); |
| 40 | + |
| 41 | + const templates_description* descriptions[] = {&description}; |
| 42 | + |
| 43 | + fast_decoder decoder; |
| 44 | + decoder.include(descriptions); |
| 45 | + |
| 46 | + const char* start = fast_message.c_str(); |
| 47 | + const char* end = start + fast_message.length(); |
| 48 | + |
| 49 | + cout << "Decoding message \"58=HelloWorld<SOH>\":" << endl; |
| 50 | + cout << endl; |
| 51 | + |
| 52 | + message_cref msg = decoder.decode(start, end); |
| 53 | + |
| 54 | + cout << "Template id: " << msg.id() << endl; |
| 55 | + cout << "Template name: " << msg.name() << endl; |
| 56 | + cout << endl; |
| 57 | + |
| 58 | + ascii_string_cref field = static_cast<ascii_string_cref>((msg)[0]); |
| 59 | + |
| 60 | + cout << "Field id: " << field.id() << endl; |
| 61 | + cout << "Field name: " << field.name() << endl; |
| 62 | + cout << "Field content: " << field.c_str() << endl; |
| 63 | + cout << endl; |
| 64 | + |
| 65 | + cout << "Encoding message to JSON:" << endl; |
| 66 | + |
| 67 | + ostringstream json_message; |
| 68 | + bool result = encode(json_message, msg, 0); |
| 69 | + if (result) cout << "Success: " << json_message.str() << endl; |
| 70 | + |
| 71 | + return 0; |
| 72 | +} |
0 commit comments