Naruto
11/21/2014 - 5:47 PM

test of msgpack. convert array msgpack binary that is created by ruby to memory.

test of msgpack. convert array msgpack binary that is created by ruby to memory.

// msgpack deserialize test
/*
How to test.

$ ruby serialize.rb
$ c++ -Wall -std=c++11 ./deserialize.cpp -o deserialize -lmsgpack
$ ./deserialize
[["hello", [1, 2, 3, 4, 5]], ["foo", [2, 4, 8, 16, 32]], ["bar", [3, 6, 9, 12, 15]]]
item num: 3
hello
1 2 3 4 5 
foo
2 4 8 16 32 
bar
3 6 9 12 15 
 */
#include <msgpack.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>

class Oreclass {
public:
    std::string _str;
    std::vector<int> _vec;
public:
    MSGPACK_DEFINE(_str, _vec);
};


int main() {
    FILE *fp;
    size_t siz;
    char buf[BUFSIZ];
    fp = fopen("data.mpac", "r");
    if(!fp) return 1;

    siz = fread(buf, 1, BUFSIZ, fp);
    msgpack::unpacked msg;
    msgpack::unpack( &msg, buf, siz);

    msgpack::object obj = msg.get();
    std::cout << obj << std::endl;

    std::vector<Oreclass> vec;
    obj.convert(&vec);
    std::cout << "item num: " << vec.size() << std::endl;
    for(auto& ore : vec) {
        std::cout << ore._str << std::endl;
        for(auto& num : ore._vec) {
            std::cout << num << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}
require 'msgpack'

data = 
  [
   [
    "hello", 
    [1, 2, 3, 4, 5],
   ],
   [
    "foo",
    [2, 4, 8, 16, 32],
   ],
   [
    "bar",
    [3, 6, 9, 12, 15],
   ]
  ]

File.open('data.mpac', 'w') { |io| io.print data.to_msgpack }