Skip to content

Commit

Permalink
Merge pull request #56 from BrainlessLabs/development
Browse files Browse the repository at this point in the history
1. Objects can be converted to messagepack
2. Objects can be converted into messagepack
3. Added some examples

Co-Authored-By: BrainlessLabs <brainlesslabs@users.noreply.github.com>
Co-Authored-By: BrainlessLabs <sp-mishra@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 20, 2018
2 parents 93126b0 + b7a0982 commit a168a54
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
20 changes: 17 additions & 3 deletions include/blib/bun/impl/orm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include <cstddef>
#include <memory>
#include <bitset>
#include <third_party/fmt/format.hpp>
#include <third_party/rapidjson/rapidjson.h>
#include <third_party/rapidjson/document.h>
#include <third_party/json/json.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/preprocessor.hpp>
#include <boost/fusion/sequence.hpp>
Expand All @@ -32,9 +36,6 @@
#include <boost/preprocessor/tuple/rem.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/proto/proto.hpp>
#include <third_party/fmt/format.hpp>
#include <third_party/rapidjson/rapidjson.h>
#include <third_party/rapidjson/document.h>
#include <soci/error.h>
#include "blib/utils/MD5.hpp"
#include "blib/bun/impl/DbBackend.hpp"
Expand Down Expand Up @@ -908,6 +909,7 @@ namespace blib {
/// This will distinguish them from other object.
OidType oid;

using ByteArray = std::vector<std::uint8_t>;
public:
PRef() noexcept : _obj(), _md5(), oid() {}

Expand Down Expand Up @@ -1048,6 +1050,18 @@ namespace blib {
blib::bun::__private::QueryHelper<ObjType>::jsonToObject(document, *_obj.get());
}

ByteArray toMesssagepack() const {
const std::string json_str = toJson();
const auto json_obj = nlohmann::json::parse(json_str);
return nlohmann::json::to_msgpack(json_obj);
}

void fromMessagepack(ByteArray const& messagePack) {
const auto json = nlohmann::json::from_msgpack(messagePack);
const std::string json_str = json.dump();
fromJson(json_str);
}

private:
void copyToOther(PRef& in_other) {
in_other.oid = oid;
Expand Down
20 changes: 16 additions & 4 deletions src/Generate.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "blib/bun/bun.hpp"
#include <boost/core/noncopyable.hpp>
#include <boost/optional.hpp>
#include <boost/preprocessor.hpp>
Expand All @@ -7,7 +8,6 @@
#include <soci/sqlite3/soci-sqlite3.h>
#include <boost/preprocessor.hpp>
#include <third_party/fmt/format.hpp>
#include "blib/bun/bun.hpp"

using namespace soci;
using namespace std;
Expand Down Expand Up @@ -319,12 +319,24 @@ int jsonTest() {

blib::bun::PRef<dbg::C> c = new dbg::C;
c->c = 666;
// Convert the object to JSON
const std::string json_string = p.toJson();
std::cout << "Object1:" << json_string << std::endl;
// Construct the new object out of json
// Construct the new object out of JSON
blib::bun::PRef<dbg::P> p1;
p1.fromJson(json_string);
std::cout << "Object2:" << p1.toJson() << std::endl;
const auto msgpack = p1.toMesssagepack();
// Construct another object out of messagepack
blib::bun::PRef<dbg::P> p2;
p2.fromMessagepack(p1.toMesssagepack());
// messagepack to string
std::string msgpack_string;
for (auto c : msgpack) {
msgpack_string.push_back(c);
}
std::cout << "1. Original object Object:" << json_string << std::endl;
std::cout << "2. Object from JSON :" << p1.toJson() << std::endl;
std::cout << "3. Object to Messagepack :" << msgpack_string << std::endl;
std::cout << "4. Object from Messagepck:" << p2.toJson() << std::endl;
return 1;
}

Expand Down

0 comments on commit a168a54

Please sign in to comment.