Skip to content

Commit

Permalink
Merge pull request #54 from BrainlessLabs/development
Browse files Browse the repository at this point in the history
FromJson implementation was done.
  • Loading branch information
BrainlessLabs authored Oct 19, 2018
2 parents 96dc1c2 + 0b4f591 commit 93126b0
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 17 deletions.
107 changes: 103 additions & 4 deletions include/blib/bun/impl/orm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#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 @@ -159,6 +161,11 @@ namespace blib {
const std::string ret = fmt::format("{}", value);
return ret;
}

template<typename T>
inline void from_json(std::string const& json_string, T const& obj) {

}
}
}
}
Expand Down Expand Up @@ -593,6 +600,8 @@ namespace blib {
return QueryHelper<T>::objToJson(obj);
}

/// @class ToJson
/// @brief Helper class to convert an object into a json representation.
struct ToJson {
private:
std::string& _str;
Expand All @@ -602,7 +611,6 @@ namespace blib {

template<typename IT, bool IsCompositeType>
struct _ToJsonImpl {
public:
static void impl(IT const& obj, const std::string& obj_name, bool const apply_comma, std::string& str) {
const std::string comma = apply_comma ? "," : "";
str += fmt::format("{}\"{}\": {}", comma, obj_name, tojson_string(obj));
Expand All @@ -611,7 +619,6 @@ namespace blib {

template<typename IT>
struct _ToJsonImpl<IT, true> {
public:
static void impl(IT const& obj, const std::string& obj_name, bool const apply_comma, std::string& str) {
const std::string comma = apply_comma ? "," : "";
str += fmt::format("{}\"{}\":{}", comma, obj_name, to_json<IT>(obj));
Expand All @@ -629,7 +636,6 @@ namespace blib {
template <typename O>
void operator()(O const& x) const {
const std::string member_name = _member_names.at(const_cast<ToJson*>(this)->_count++);
const std::string obj_name = blib::bun::__private::to_valid_query_string(member_name, "'");
ToJson::_ToJsonImpl<O, IsComposite<O>::value>::impl(x, member_name, _apply_comma, _str);
const_cast<ToJson*>(this)->_apply_comma = true;
}
Expand All @@ -640,6 +646,7 @@ namespace blib {
/// @param obj The object which we need to create to string
/// @brief Converts the object to a json representation and
/// returns the json representation as a string
/// @return JSON representation
inline static std::string objToJson(T const& obj) {
std::string str;
boost::fusion::for_each(obj, QueryHelper<T>::ToJson(str));
Expand All @@ -648,6 +655,89 @@ namespace blib {
}


struct FromJson {
private:
rapidjson::Value const& _document;
std::vector<std::string> const& _member_names;
int _count;

private:
template<typename CppType>
static CppType jsonTypeToCppType(rapidjson::Value const& val);

template<>
static int jsonTypeToCppType<int>(rapidjson::Value const& val) {
return val.GetInt();
}

template<>
static std::int64_t jsonTypeToCppType<std::int64_t>(rapidjson::Value const& val) {
return val.GetInt64();
}

template<>
static unsigned int jsonTypeToCppType<unsigned int>(rapidjson::Value const& val) {
return val.GetUint();
}

template<>
static std::uint64_t jsonTypeToCppType<std::uint64_t>(rapidjson::Value const& val) {
return val.GetUint64();
}

template<>
static bool jsonTypeToCppType<bool>(rapidjson::Value const& val) {
return val.GetBool();
}

template<>
static double jsonTypeToCppType<double>(rapidjson::Value const& val) {
return val.GetDouble();
}

template<>
static float jsonTypeToCppType<float>(rapidjson::Value const& val) {
return val.GetFloat();
}

template<>
static std::string jsonTypeToCppType<std::string>(rapidjson::Value const& val) {
return val.GetString();
}

template<typename IT, bool isComplex>
struct _FromJson {
static void impl(IT& field, std::string const& field_name, rapidjson::Value const& doc) {
field = jsonTypeToCppType<IT>(doc.FindMember(field_name.c_str())->value);
}
};

template<typename IT>
struct _FromJson<IT, true> {
static void impl(IT& field, std::string const& field_name, rapidjson::Value const& doc) {
QueryHelper<IT>::jsonToObject(doc.FindMember(field_name.c_str())->value, field);
}
};

public:
FromJson(rapidjson::Value const& document):_document(document),
_member_names(TypeMetaData<T>::member_names()),
_count(2){}

template <typename O>
void operator()(O& x) const {
const std::string member_name = _member_names.at(const_cast<FromJson*>(this)->_count++);
FromJson::_FromJson<O, IsComposite<O>::value>::impl(x, member_name, _document);
}
};
/// @fn jsonToObject
/// @brief This function converts
inline static void jsonToObject(rapidjson::Value const& json, T& obj) {
QueryHelper<T>::FromJson fromJson(json);
boost::fusion::for_each(obj, fromJson);
}


template<typename TA, bool IsComposite>
struct GetAllObjectsImpl {
inline static void impl(TA& x, const soci::row& row, const std::string& member_name, const std::string& /*oid_ref*/) {
Expand Down Expand Up @@ -946,9 +1036,18 @@ namespace blib {
/// @fn toJson
/// @brief Returns a JSON representation of the object.
std::string toJson() const {
return blib::bun::__private::QueryHelper<T>::objToJson(*_obj.get());
return blib::bun::__private::QueryHelper<ObjType>::objToJson(*_obj.get());
}

/// @fn fromJson
/// @brief Creates the object from the given json string
void fromJson(const std::string& json) {
rapidjson::Document document;
document.Parse(json.c_str());
_obj = std::make_unique<ObjType>();
blib::bun::__private::QueryHelper<ObjType>::jsonToObject(document, *_obj.get());
}

private:
void copyToOther(PRef& in_other) {
in_other.oid = oid;
Expand Down
21 changes: 8 additions & 13 deletions src/Generate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,6 @@ SPECIALIZE_BUN_HELPER((dbg::P, p, c));

int jsonTest() {
namespace bun = blib::bun;
namespace query = blib::bun::query;

#if defined(BUN_SQLITE)
bun::connect("obj.db");
#elif defined(BUN_POSTGRES)
bun::connect("postgresql://localhost/postgres?user=postgres&password=postgres");
#endif
blib::bun::createSchema<dbg::C1>();
blib::bun::createSchema<dbg::C>();
blib::bun::createSchema<dbg::P>();

blib::bun::PRef<dbg::P> p = new dbg::P;
p->p = "s11";
Expand All @@ -329,9 +319,12 @@ int jsonTest() {

blib::bun::PRef<dbg::C> c = new dbg::C;
c->c = 666;

std::cout << p.toJson() << std::endl;

const std::string json_string = p.toJson();
std::cout << "Object1:" << json_string << std::endl;
// Construct the new object out of json
blib::bun::PRef<dbg::P> p1;
p1.fromJson(json_string);
std::cout << "Object2:" << p1.toJson() << std::endl;
return 1;
}

Expand Down Expand Up @@ -433,6 +426,8 @@ int ormDbgSimple() {

int main() {
namespace bun = blib::bun;
//rapidjson::Document d;
//rapidjson::Value v = d.GetObjectW();
jsonTest();
//kvTest();
//ormTest();
Expand Down

0 comments on commit 93126b0

Please sign in to comment.