Skip to content

Instantly share code, notes, and snippets.

@physacco
Last active November 27, 2023 19:12
Show Gist options
  • Save physacco/df07352f70da4cecd2adbaf5cffc2cf3 to your computer and use it in GitHub Desktop.
Save physacco/df07352f70da4cecd2adbaf5cffc2cf3 to your computer and use it in GitHub Desktop.
MsgPack sample programs.

MsgPack Sample Programs

Build and Run the C++ Programs

sudo apt-get install -y cmake libmsgpack-dev
./build.sh

./debug/test_vector
./debug/test_stream
./debug/test_class
./debug/test_array
./debug/test_map1
./debug/test_map2

Tested on Ubuntu 14.04.4, libmsgpack-dev 0.5.7.

Read the MsgPack files with Ruby

sudo gem install msgpack

./read_msgpack.rb *.msgpack

Tested on Ruby 2.3.1p112.

Read the MsgPack files with Python 3

sudo pip3 install msgpack-python

./read_msgpack.py *.msgpack

Tested on Python 3.4.3.

License

These programs are released into the public domain.

References

#!/bin/sh
set -e
BUILD_DIR=debug
mkdir -p $BUILD_DIR
cd $BUILD_DIR
cmake -DCMAKE_BUILD_TYPE=Debug ..
make -j $(nproc)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.2)
PROJECT(msgpack_test)
SET(CMAKE_CXX_FLAGS_DEBUG "-g -std=c++11")
SET(EXECUTABLES test_vector test_stream test_class test_array test_map1 test_map2)
FOREACH(EXE ${EXECUTABLES})
ADD_EXECUTABLE(${EXE} "${EXE}.cpp")
TARGET_LINK_LIBRARIES(${EXE} msgpack)
ENDFOREACH()
// Copyright This program is released into the public domain.
#pragma once
#include <cstdio>
#include <string>
inline bool save_msgpack_sbuffer(const msgpack::sbuffer& buffer,
const std::string& filename) {
FILE* fp = std::fopen(filename.c_str(), "wb");
if (fp == nullptr) {
return false;
}
const void* ptr = (const void*)buffer.data();
std::fwrite(ptr, buffer.size(), 1, fp);
fclose(fp);
return true;
}
#!/usr/bin/env python3
# encoding: utf-8
import sys
import msgpack
def process_file(filename):
with open(filename, 'rb') as f:
unpacker = msgpack.Unpacker(f)
for unpacked in unpacker:
print(unpacked)
def process_multiple_files(files):
for filename in files:
print('Processing %s ...' % filename)
process_file(filename)
print()
def main():
argc = len(sys.argv)
if argc < 2:
sys.stderr.write('Usage: %s msgpack_files\n' % sys.argv[0])
exit(1)
elif argc == 2:
process_file(sys.argv[1])
else:
process_multiple_files(sys.argv[1:])
if __name__ == '__main__':
main()
#!/usr/bin/env ruby
require "msgpack"
def process_file(filename)
File.open(filename, "rb") do |f|
upk = MessagePack::Unpacker.new(f)
upk.each do |obj|
p obj
end
end
rescue Exception => e
STDERR.puts "#{e.class}: #{e.message}"
end
def process_multiple_files(files)
files.each do |filename|
puts "Processing #{filename} ..."
process_file filename
puts
end
end
if ARGV.size < 1
STDERR.puts "Usage: #{$0} msgpack_files"
exit 1
elsif ARGV.size == 1
process_file ARGV[0]
else
process_multiple_files ARGV
end
// Copyright This program is released into the public domain.
#include <string>
#include <iostream>
#include <msgpack.hpp>
#include "./msgio.h"
int main(void) {
msgpack::sbuffer buffer;
msgpack::packer<msgpack::sbuffer> pk(&buffer);
pk.pack_array(4);
pk.pack(std::string("Log message ... 1"));
pk.pack(std::string("Log message ... 2"));
pk.pack(std::string("Log message ... 3"));
pk.pack(2233);
// save
save_msgpack_sbuffer(buffer, __FILE__".msgpack");
// use msgpack::unpacker to unpack multiple objects.
msgpack::unpacker upk;
// feeds the buffer.
upk.reserve_buffer(buffer.size());
memcpy(upk.buffer(), buffer.data(), buffer.size());
upk.buffer_consumed(buffer.size());
// // now starts streaming deserialization.
msgpack::unpacked result;
while (upk.next(&result)) {
msgpack::object obj = result.get();
if (obj.type != msgpack::type::ARRAY) {
throw msgpack::type_error();
}
if (obj.via.array.size != 4) {
throw msgpack::type_error();
}
std::cout << obj.via.array.ptr[0].as<std::string>() << std::endl;
std::cout << obj.via.array.ptr[1].as<std::string>() << std::endl;
std::cout << obj.via.array.ptr[2].as<std::string>() << std::endl;
std::cout << obj.via.array.ptr[3].as<int>() << std::endl;
std::cout << obj << std::endl;
}
}
// Copyright This program is released into the public domain.
#include <string>
#include <vector>
#include <iostream>
#include <msgpack.hpp>
#include "./msgio.h"
class MyClass {
private:
std::string m_string;
std::vector<int> m_int_vec;
std::vector<std::string> m_string_vec;
public:
MyClass() {
}
MyClass(std::string a,
const std::vector<int>& b,
const std::vector<std::string>& c) {
m_string = a;
for (auto it = b.begin(); it != b.end(); ++it) {
m_int_vec.push_back(*it);
}
for (auto it = c.begin(); it != c.end(); ++it) {
m_string_vec.push_back(*it);
}
}
friend std::ostream& operator<< (std::ostream& os, const MyClass& obj) {
os << "MyClass(m_string: " << obj.m_string << ", m_int_vec: [";
for (auto it = obj.m_int_vec.begin(); it != obj.m_int_vec.end(); ++it) {
if (it != obj.m_int_vec.begin()) {
os << ", ";
}
os << *it;
}
os << "], m_string_vec: [";
for (auto it = obj.m_string_vec.begin();
it != obj.m_string_vec.end(); ++it) {
if (it != obj.m_string_vec.begin()) {
os << ", ";
}
os << *it;
}
os << "])";
return os;
}
MSGPACK_DEFINE(m_string, m_int_vec, m_string_vec);
};
int main() {
std::vector<int> int_vec;
std::vector<std::string> string_vec;
// MyClass obj1
int_vec.push_back(2);
int_vec.push_back(4);
string_vec.push_back("foo");
string_vec.push_back("bar");
MyClass obj1("obj1", int_vec, string_vec);
// MyClass obj2
int_vec.push_back(8);
string_vec.push_back("baz");
MyClass obj2("obj2", int_vec, string_vec);
// MyClass vector: [obj1, obj2]
std::vector<MyClass> my_class_vec;
my_class_vec.push_back(obj1);
my_class_vec.push_back(obj2);
// pack
msgpack::sbuffer buffer;
msgpack::pack(buffer, my_class_vec);
// save
save_msgpack_sbuffer(buffer, __FILE__".msgpack");
// unpack
msgpack::unpacked msg;
msgpack::unpack(&msg, buffer.data(), buffer.size());
// convert
msgpack::object obj = msg.get();
std::vector<MyClass> result_vec;
obj.convert(&result_vec);
// print
for (auto it = result_vec.begin(); it != result_vec.end(); ++it) {
std::cout << *it << std::endl;
}
return 0;
}
// Copyright This program is released into the public domain.
#include <map>
#include <string>
#include <iostream>
#include <msgpack.hpp>
#include "./msgio.h"
int main(void) {
msgpack::sbuffer buffer;
msgpack::packer<msgpack::sbuffer> pk(&buffer);
pk.pack_map(2);
pk.pack(std::string("x"));
pk.pack(3);
pk.pack(std::string("y"));
pk.pack(4);
// save
save_msgpack_sbuffer(buffer, __FILE__".msgpack");
// use msgpack::unpacker to unpack multiple objects.
msgpack::unpacker upk;
// feeds the buffer.
upk.reserve_buffer(buffer.size());
memcpy(upk.buffer(), buffer.data(), buffer.size());
upk.buffer_consumed(buffer.size());
// // now starts streaming deserialization.
msgpack::unpacked result;
while (upk.next(&result)) {
msgpack::object obj = result.get();
if (obj.type != msgpack::type::MAP) {
throw msgpack::type_error();
}
if (obj.via.map.size != 2) {
throw msgpack::type_error();
}
typedef std::map<std::string, int> Str2Int;
Str2Int map = obj.as<Str2Int>();
for (auto it = map.begin(); it != map.end(); ++it) {
std::cout << it->first << " => " << it->second << std::endl;
}
std::cout << obj << std::endl;
}
}
// Copyright This program is released into the public domain.
#include <string>
#include <iostream>
#include <msgpack.hpp>
#include "./msgio.h"
int main(void) {
msgpack::sbuffer buffer;
msgpack::packer<msgpack::sbuffer> pk(&buffer);
pk.pack_map(2);
pk.pack(std::string("x"));
pk.pack(3);
pk.pack(std::string("y"));
pk.pack(3.4321);
// save
save_msgpack_sbuffer(buffer, __FILE__".msgpack");
// use msgpack::unpacker to unpack multiple objects.
msgpack::unpacker upk;
// feeds the buffer.
upk.reserve_buffer(buffer.size());
memcpy(upk.buffer(), buffer.data(), buffer.size());
upk.buffer_consumed(buffer.size());
// // now starts streaming deserialization.
msgpack::unpacked result;
while (upk.next(&result)) {
msgpack::object obj = result.get();
if (obj.type != msgpack::type::MAP) {
throw msgpack::type_error();
}
if (obj.via.map.size != 2) {
throw msgpack::type_error();
}
std::cout << obj.via.map.ptr[0].key.as<std::string>() << std::endl;
std::cout << obj.via.map.ptr[0].val.as<int>() << std::endl;
std::cout << obj.via.map.ptr[1].key.as<std::string>() << std::endl;
std::cout << obj.via.map.ptr[1].val.as<double>() << std::endl;
std::cout << obj << std::endl;
}
}
// Copyright This program is released into the public domain.
#include <string>
#include <iostream>
#include <msgpack.hpp>
#include "./msgio.h"
int main() {
// use msgpack::packer to pack multiple objects.
msgpack::sbuffer buffer;
msgpack::packer<msgpack::sbuffer> pk(&buffer);
pk.pack(std::string("this is 1st string"));
pk.pack(std::string("this is 2nd string"));
pk.pack(std::string("this is 3th string"));
// save
save_msgpack_sbuffer(buffer, __FILE__".msgpack");
// use msgpack::unpacker to unpack multiple objects.
msgpack::unpacker upk;
// feeds the buffer.
upk.reserve_buffer(buffer.size());
memcpy(upk.buffer(), buffer.data(), buffer.size());
upk.buffer_consumed(buffer.size());
// now starts streaming deserialization.
msgpack::unpacked result;
while (upk.next(&result)) {
std::cout << result.get() << std::endl;
}
return 0;
}
// Copyright This program is released into the public domain.
#include <vector>
#include <string>
#include <iostream>
#include <msgpack.hpp>
#include "./msgio.h"
int main() {
std::vector<std::string> string_vec;
string_vec.push_back("Hello");
string_vec.push_back("world");
// pack
msgpack::sbuffer buffer;
msgpack::pack(buffer, string_vec);
std::cout << buffer.data() << std::endl;
// save
save_msgpack_sbuffer(buffer, __FILE__".msgpack");
// unpack
msgpack::unpacked msg;
msgpack::unpack(&msg, buffer.data(), buffer.size());
msgpack::object obj = msg.get();
std::cout << obj << std::endl;
// convert
std::vector<std::string> string_vec2;
obj.convert(&string_vec2);
// print
for (size_t i = 0; i < string_vec2.size(); ++i) {
std::cout << string_vec2[i] << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment