Skip to content
Snippets Groups Projects
Commit 625848cd authored by Evgeny Lavrik's avatar Evgeny Lavrik
Browse files

Add merging json files

parent c6918fe9
No related branches found
No related tags found
No related merge requests found
......@@ -49,26 +49,6 @@ static std::string ClassName() {
return result.substr(idx + 2, result.size() - idx - 2);
}
// static std::string FullClassName(void* object) {
// int status = -4;
// const char * name = typeid(*object).name();
// std::string result = abi::__cxa_demangle(name, nullptr, nullptr, &status);
// if (status != 0)
// return name;
// return result;
// }
// static std::string ClassName(void* object) {
// std::string result = FullClassName(object);
// auto idx = result.find("::");
// if (idx == std::string::npos)
// return result;
// return result.substr(idx + 2, result.size() - idx - 2);
// }
template <class T>
static inline T* FromJson(jsoncons::ojson json) {
if (!json.is_object()) return nullptr;
......@@ -100,7 +80,7 @@ class BaseClass
public:
virtual std::string FullClassName() {
int status = -4;
const char * name = typeid(*this).name();
const char* name = typeid(*this).name();
std::string result = abi::__cxa_demangle(name, nullptr, nullptr, &status);
if (status != 0)
return name;
......@@ -205,9 +185,9 @@ class BaseObject: public BaseClass {
return TGenBase::FromJson<BaseObject>(json);
}
int GetId() const { return fId; };
virtual int GetId() const { return fId; };
void SetId(int value) { fId = value; };
virtual void SetId(int value) { fId = value; };
protected:
int fId{0}; ///< Object Id
......@@ -227,15 +207,15 @@ class BaseCondition: public BaseClass {
#else
virtual std::string GetName() const { return fName; };
#endif
std::map<std::string, std::string> GetMetadata() const { return fMetadata; }
int GetRunId() const { return fRunId; }
int GetVersion() const { return fVersion; }
virtual std::map<std::string, std::string> GetMetadata() const { return fMetadata; }
virtual int GetRunId() const { return fRunId; }
virtual int GetVersion() const { return fVersion; }
void SetName(std::string value) { fName = value; };
void SetName(char* value) { fName = value; };
void SetMetadata(std::map<std::string, std::string> value) { fMetadata = value; }
void SetRunId(int value) { fRunId = value; }
void SetVersion(int value) { fVersion = value; }
virtual void SetName(std::string value) { fName = value; };
virtual void SetName(char* value) { fName = value; };
virtual void SetMetadata(std::map<std::string, std::string> value) { fMetadata = value; }
virtual void SetRunId(int value) { fRunId = value; }
virtual void SetVersion(int value) { fVersion = value; }
protected:
std::string fName{}; ///< Parameter Name
......@@ -253,8 +233,8 @@ class ParameterJsonWrapper: public BaseCondition {
return TGenBase::FromJson<ParameterJsonWrapper>(json);
}
jsoncons::ojson GetParameter() const { return fParameter; }
void SetParameter(jsoncons::ojson value) { fParameter = value; }
virtual jsoncons::ojson GetParameter() const { return fParameter; }
virtual void SetParameter(jsoncons::ojson value) { fParameter = value; }
template <class T>
T* GetParameter() {
......@@ -292,8 +272,8 @@ class ParameterJsonStringWrapper: public BaseCondition {
virtual std::string ClassName() { return "ParameterWrapper"; }
std::string GetParameter() const { return fParameter; }
void SetParameter(std::string value) { fParameter = value; }
virtual std::string GetParameter() const { return fParameter; }
virtual void SetParameter(std::string value) { fParameter = value; }
template <class T>
T* GetParameter() {
......@@ -327,8 +307,8 @@ class ParameterWrapper: public BaseCondition {
return TGenBase::FromJson<ParameterWrapper>(json);
}
std::string GetParameter() const { return fParameter; }
void SetParameter(std::string value) { fParameter = value; }
virtual std::string GetParameter() const { return fParameter; }
virtual void SetParameter(std::string value) { fParameter = value; }
template <class T>
T* GetParameter() {
......@@ -389,8 +369,8 @@ class ParameterTemplate: public BaseCondition {
return instance;
}
P* GetParameter() const { return fParameter; }
void SetParameter(P* value) {
virtual P* GetParameter() const { return fParameter; }
virtual void SetParameter(P* value) {
if (fParameter) delete fParameter;
fParameter = new P(*value);
}
......
......@@ -59,6 +59,38 @@ void JsonFileClient::FlushFile() {
os << jsoncons::pretty_print(fJson, options);
}
void JsonFileClient::MergeFiles(std::vector<std::string> files, std::string outputFile) {
std::ofstream os(outputFile);
if (!os.good()) {
std::cout << "Can not access output file " << outputFile << std::endl;
return;
}
jsoncons::ojson result = jsoncons::ojson::object();
for (auto& file : files) {
std::ifstream is(file.c_str());
if (!is.good()) {
std::cout << "File " << file << " does not exist. "
"It will be skipped." << std::endl;
} else {
std::string jsonString((std::istreambuf_iterator<char>(is)),
std::istreambuf_iterator<char>());
try {
result.merge(jsoncons::ojson::parse(jsonString));
}
catch (const jsoncons::ser_error& e) {
std::cout << "Can not read in the json file " << file << std::endl;
std::cout << e.what() << std::endl;
}
}
}
jsoncons::json_options options;
options.indent_size(2);
os << jsoncons::pretty_print(result, options);
}
void JsonFileClient::Store(BaseClass* instance, std::string name, int ts) {
std::cout << "Json File Store" << std::endl;
instance->SetCreatedAt(ts);
......
......@@ -27,6 +27,8 @@ class JsonFileClient: public Client
void CloseFile();
void FlushFile();
void MergeFiles(std::vector<std::string> files, std::string outputFile);
std::string GetFileName() { return fFileName; }
virtual BaseObject* GetById(int id, int ts, BaseObject* context);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment