Skip to content

Instantly share code, notes, and snippets.

@drewpts
Created March 30, 2015 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewpts/614ca61c06b798054615 to your computer and use it in GitHub Desktop.
Save drewpts/614ca61c06b798054615 to your computer and use it in GitHub Desktop.
#include "json11.hpp"
using json11::Json;
#include <string>
using std::string;
#include <map>
typedef std::map<string, Json> Map;
#include <utility>
typedef std::pair<string, Json> Pair;
#include <vector>
typedef std::vector<string> strVector;
typedef std::vector<Pair> PairVector;
#include <stdexcept>
using std::out_of_range;
#include <sstream>
using std::ostringstream;
class dataFacility {
Map data;
Map newData;
bool _isChanged;
public:
dataFacility() { }
dataFacility(const Map& m) {
for(const auto& key : {"project", "files", "subprojects"}) {
try {
data.emplace(key, m.at(key));
}
catch (const out_of_range& e) {
if (key == "subprojects")
continue;
else dataFacility();
}
}
}
// toJson() const;
// bool save();
void setProjectKey(const Json& pr) noexcept { newData["project"] = pr; }
void setProjectkey(const string& pr) {
class error { };
try {
string err;
ostringstream o;
o << "{ "
<< "\"project\": \""
<< pr
<< "\" }";
newData["project"] = Json::parse(o.str(), err);
if (err.empty() == false) {
throw error();
}
} catch (const error& e) {
return;
}
}
void setFilesKey(const Json& fi) noexcept { newData["files"] = fi; }
void setFileskey(const string&);
void setSubprojectsKey(const Json& su) noexcept { newData["project"] = su; }
void setSubprojectsKey(const string&);
Json getProjectKey() const noexcept { return data.at("project"); }
Json getFilesKey() const noexcept { return data.at("files"); }
Json getSubprojectsKey() const {
try {
return data.at("subprojects");
} catch (const out_of_range& e) {
return Json();
}
}
// enum Array { FILES, SUBPROJECTS };
// bool appendDataToArray(const Array, const string&);
private:
strVector keys = {
//don't change sequence if you don't know what you doing
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment