Commit 1e8ac4e0 authored by Alexander Lapshin's avatar Alexander Lapshin

.

parent 7974b4d4
#ifndef JSONFUNCTIONS_H #pragma once
#define JSONFUNCTIONS_H
#include "MException.h" #include "MException.h"
#include <iostream> #include <iostream>
...@@ -18,14 +17,15 @@ typedef rapidjson::Document::AllocatorType jsonalloc; ...@@ -18,14 +17,15 @@ typedef rapidjson::Document::AllocatorType jsonalloc;
typedef rapidjson::Value jsonval; typedef rapidjson::Value jsonval;
typedef rapidjson::Document jsondoc; typedef rapidjson::Document jsondoc;
static bool loadjson(const jsonval& parent, const std::string& key, std::vector<std::string>& vec, bool exist = true, bool* loaded = 0) inline bool extract_value(const jsonval& parent, const std::string& key, bool exist, bool* loaded, jsonval::ConstMemberIterator& result)
{ {
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str()); const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str());
if (itr == parent.MemberEnd() && exist) { if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key; throw Exp() << "error: element '" << key << "' not found";
} }
else if (itr == parent.MemberEnd() && !exist) {
if (itr == parent.MemberEnd() && !exist) {
if (loaded) { if (loaded) {
*loaded = false; *loaded = false;
} }
...@@ -34,28 +34,57 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector< ...@@ -34,28 +34,57 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
const jsonval& a = itr->value; const jsonval& a = itr->value;
if (a.IsNull()) {
if (exist) {
throw Exp() << "error: element [" << key << "] can't be null";
}
if (loaded) {
*loaded = false;
}
return false;
}
result = itr;
if (loaded) {
*loaded = true;
}
return true;
}
static bool loadjson(const jsonval& parent, const std::string& key, std::vector<std::string>& vec, bool exist = true, bool* loaded = 0)
{
jsonval::ConstMemberIterator itr;
if(!extract_value(parent, key, exist, loaded, itr)){
return false;
}
const jsonval& a = itr->value;
if (!a.IsArray()) { if (!a.IsArray()) {
throw Exp() << "error: element is not an array"; throw Exp() << "error: element is not an array";
} }
for (jsonval::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) { for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) {
if (itr->IsString()) { if (it->IsString()) {
std::string str = itr->GetString(); std::string str = it->GetString();
vec.push_back(str); vec.push_back(str);
} }
else if (itr->IsInt()) { else if (it->IsInt()) {
std::ostringstream ss; std::ostringstream ss;
ss << itr->GetInt(); ss << it->GetInt();
vec.push_back(ss.str()); vec.push_back(ss.str());
} }
else if (itr->IsBool()) { else if (it->IsBool()) {
std::ostringstream ss; std::ostringstream ss;
ss << itr->GetBool(); ss << it->GetBool();
vec.push_back(ss.str()); vec.push_back(ss.str());
} }
else if (itr->IsDouble()) { else if (it->IsDouble()) {
std::ostringstream ss; std::ostringstream ss;
ss << itr->GetDouble(); ss << it->GetDouble();
vec.push_back(ss.str()); vec.push_back(ss.str());
} }
else { else {
...@@ -63,24 +92,13 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector< ...@@ -63,24 +92,13 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
} }
} }
if (loaded) {
*loaded = true;
}
return true; return true;
} }
static bool loadjson(const jsonval& parent, const std::string& key, std::vector<int>& vec, bool exist = true, bool* loaded = 0) static bool loadjson(const jsonval& parent, const std::string& key, std::vector<int>& vec, bool exist = true, bool* loaded = 0)
{ {
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str()); jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
return false; return false;
} }
...@@ -90,33 +108,22 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector< ...@@ -90,33 +108,22 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
throw Exp() << "error: element is not an array"; throw Exp() << "error: element is not an array";
} }
for (jsonval::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) { for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) {
if (itr->IsInt()) { if (it->IsInt()) {
vec.push_back(itr->GetInt()); vec.push_back(it->GetInt());
} }
else { else {
throw Exp() << "error: unsupported type"; throw Exp() << "error: unsupported type";
} }
} }
if (loaded) {
*loaded = true;
}
return true; return true;
} }
static bool loadjson(const jsonval& parent, const std::string& key, std::vector<double>& vec, bool exist = true, bool* loaded = 0) static bool loadjson(const jsonval& parent, const std::string& key, std::vector<double>& vec, bool exist = true, bool* loaded = 0)
{ {
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str()); jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
return false; return false;
} }
...@@ -126,33 +133,22 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector< ...@@ -126,33 +133,22 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
throw Exp() << "error: element is not an array"; throw Exp() << "error: element is not an array";
} }
for (jsonval::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) { for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) {
if (itr->IsDouble()) { if (it->IsDouble()) {
vec.push_back(itr->GetDouble()); vec.push_back(it->GetDouble());
} }
else { else {
throw Exp() << "error: unsupported type"; throw Exp() << "error: unsupported type";
} }
} }
if (loaded) {
*loaded = true;
}
return true; return true;
} }
template <typename T> bool loadjson(const jsonval& parent, std::string key, std::vector<T>& vec, bool exist = true, bool* loaded = 0) template <typename T> bool loadjson(const jsonval& parent, std::string key, std::vector<T>& vec, bool exist = true, bool* loaded = 0)
{ {
jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str()); jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
return false; return false;
} }
...@@ -168,24 +164,13 @@ template <typename T> bool loadjson(const jsonval& parent, std::string key, std: ...@@ -168,24 +164,13 @@ template <typename T> bool loadjson(const jsonval& parent, std::string key, std:
vec.push_back(item); vec.push_back(item);
} }
if (loaded) {
*loaded = true;
}
return true; return true;
} }
template <typename T, typename Tid> bool loadjson(const jsonval& parent, std::string key, std::map<Tid, T>& map, bool exist = true, bool* loaded = 0) template <typename T, typename Tid> bool loadjson(const jsonval& parent, std::string key, std::map<Tid, T>& map, bool exist = true, bool* loaded = 0)
{ {
jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str()); jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
return false; return false;
} }
...@@ -201,92 +186,42 @@ template <typename T, typename Tid> bool loadjson(const jsonval& parent, std::st ...@@ -201,92 +186,42 @@ template <typename T, typename Tid> bool loadjson(const jsonval& parent, std::st
map[item.getId()] = item; map[item.getId()] = item;
} }
if (loaded) {
*loaded = true;
}
return true; return true;
} }
template <typename T> bool loadjson(const jsonval& parent, std::string key, T& item, void(*f)(const jsonval&, T&), bool exist = true, bool* loaded = 0) template <typename T> bool loadjson(const jsonval& parent, std::string key, T& item, void(*f)(const jsonval&, T&), bool exist = true, bool* loaded = 0)
{ {
jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str()); jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
return false; return false;
} }
const jsonval& a = itr->value; const jsonval& a = itr->value;
if (a.IsNull() && !exist) {
if (exist) {
throw Exp() << "error: no value for " << key;
}
else {
if (loaded) {
*loaded = false;
}
return false;
}
}
if (!a.IsObject()) { if (!a.IsObject()) {
throw Exp() << "error: element is not an object"; throw Exp() << "error: element is not an object";
} }
f(a, item); f(a, item);
if (loaded) {
*loaded = true;
}
return true; return true;
} }
template <typename T> bool loadjson(const jsonval& parent, std::string key, T& item, bool exist = true, bool* loaded = 0) template <typename T> bool loadjson(const jsonval& parent, const std::string& key, T& item, bool exist = true, bool* loaded = 0)
{ {
jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str()); jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
return false; return false;
} }
const jsonval& a = itr->value; const jsonval& a = itr->value;
if (a.IsNull() && !exist) {
if (exist) {
throw Exp() << "error: no value for " << key;
}
else {
if (loaded) {
*loaded = false;
}
return false;
}
}
if (!a.IsObject()) { if (!a.IsObject()) {
throw Exp() << "error: element is not an object"; throw Exp() << "error: element is not an object";
} }
item.load(a); item.load(a);
if (loaded) {
*loaded = true;
}
return true; return true;
} }
...@@ -332,31 +267,14 @@ template <typename T, typename T2> void loadjson(const jsonval& parent, std::map ...@@ -332,31 +267,14 @@ template <typename T, typename T2> void loadjson(const jsonval& parent, std::map
} }
} }
static void loadFileJson(const std::string& filename, rapidjson::Document& document)
{
std::string data;
if (!ReadFile(filename, data)) {
throw Exp() << "failed to read file " << filename;
}
rapidjson::ParseResult ok = document.Parse(data.c_str());
if (!ok) { if (!ok) {
throw Exp() << "json parse error " << filename << ", code: " << ok.Code() << ", offset: " << ok.Offset(); throw Exp() << "json parse error " << filename << ", code: " << ok.Code() << ", offset: " << ok.Offset();
} }
}
static bool loadjson(const jsonval& parent, const std::string& key, double& value, bool exist = true, bool* loaded = 0) static bool loadjson(const jsonval& parent, const std::string& key, double& value, bool exist = true, bool* loaded = 0)
{ {
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str()); jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
return false; return false;
} }
...@@ -383,24 +301,13 @@ static bool loadjson(const jsonval& parent, const std::string& key, double& valu ...@@ -383,24 +301,13 @@ static bool loadjson(const jsonval& parent, const std::string& key, double& valu
throw (Exp() << "element '" << key << "'" << " has unexpected format '"); throw (Exp() << "element '" << key << "'" << " has unexpected format '");
} }
if (loaded) {
*loaded = true;
}
return true; return true;
} }
static bool loadjson(const jsonval& parent, const std::string& key, int& value, bool exist = true, bool* loaded = 0) static bool loadjson(const jsonval& parent, const std::string& key, int& value, bool exist = true, bool* loaded = 0)
{ {
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str()); jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
return false; return false;
} }
...@@ -427,24 +334,13 @@ static bool loadjson(const jsonval& parent, const std::string& key, int& value, ...@@ -427,24 +334,13 @@ static bool loadjson(const jsonval& parent, const std::string& key, int& value,
throw (Exp() << "element '" << key << "'" << " has unexpected format '"); throw (Exp() << "element '" << key << "'" << " has unexpected format '");
} }
if (loaded) {
*loaded = true;
}
return true; return true;
} }
static bool loadjson(const jsonval& parent, const std::string& key, bool& value, bool exist = true, bool* loaded = 0) static bool loadjson(const jsonval& parent, const std::string& key, bool& value, bool exist = true, bool* loaded = 0)
{ {
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str()); jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
return false; return false;
} }
...@@ -474,24 +370,13 @@ static bool loadjson(const jsonval& parent, const std::string& key, bool& value, ...@@ -474,24 +370,13 @@ static bool loadjson(const jsonval& parent, const std::string& key, bool& value,
throw (Exp() << "element '" << key << "'" << " has unexpected format '"); throw (Exp() << "element '" << key << "'" << " has unexpected format '");
} }
if (loaded) {
*loaded = true;
}
return true; return true;
} }
static bool loadjson(const jsonval& parent, const std::string& key, std::string& value, bool exist = true, bool* loaded = 0) static bool loadjson(const jsonval& parent, const std::string& key, std::string& value, bool exist = true, bool* loaded = 0)
{ {
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str()); jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
return false; return false;
} }
...@@ -509,10 +394,6 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::string& ...@@ -509,10 +394,6 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::string&
throw (Exp() << "element '" << key << "'" << " has unexpected format '"); throw (Exp() << "element '" << key << "'" << " has unexpected format '");
} }
if (loaded) {
*loaded = true;
}
return true; return true;
} }
...@@ -643,18 +524,21 @@ static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& value) ...@@ -643,18 +524,21 @@ static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& value)
static void flushjson(const jsondoc& outroot, const std::string& outputfile, bool pretty = true) static void flushjson(const jsondoc& outroot, const std::string& outputfile, bool pretty = true)
{ {
pretty = true;
if (!outputfile.length()) { if (!outputfile.length()) {
if (pretty) { if (pretty) {
rapidjson::StringBuffer buffer; rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer); rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
outroot.Accept(writer); if(!outroot.Accept(writer)){
throw Exp() << "json write fail";
}
std::cout << buffer.GetString(); std::cout << buffer.GetString();
} }
else { else {
rapidjson::StringBuffer buffer; rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
outroot.Accept(writer); if (!outroot.Accept(writer)) {
throw Exp() << "json write fail";
}
std::cout << buffer.GetString(); std::cout << buffer.GetString();
} }
} }
...@@ -663,13 +547,17 @@ static void flushjson(const jsondoc& outroot, const std::string& outputfile, boo ...@@ -663,13 +547,17 @@ static void flushjson(const jsondoc& outroot, const std::string& outputfile, boo
std::ofstream ofs(outputfile.c_str()); std::ofstream ofs(outputfile.c_str());
rapidjson::OStreamWrapper osw(ofs); rapidjson::OStreamWrapper osw(ofs);
rapidjson::PrettyWriter<rapidjson::OStreamWrapper> writer(osw); rapidjson::PrettyWriter<rapidjson::OStreamWrapper> writer(osw);
outroot.Accept(writer); if (!outroot.Accept(writer)) {
throw Exp() << "json write fail";
}
} }
else { else {
std::ofstream ofs(outputfile.c_str()); std::ofstream ofs(outputfile.c_str());
rapidjson::OStreamWrapper osw(ofs); rapidjson::OStreamWrapper osw(ofs);
rapidjson::Writer<rapidjson::OStreamWrapper> writer(osw); rapidjson::Writer<rapidjson::OStreamWrapper> writer(osw);
outroot.Accept(writer); if (!outroot.Accept(writer)) {
throw Exp() << "json write fail";
}
} }
} }
} }
...@@ -678,7 +566,9 @@ static std::string jsontostr(const jsondoc& outroot) ...@@ -678,7 +566,9 @@ static std::string jsontostr(const jsondoc& outroot)
{ {
rapidjson::StringBuffer buffer; rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
outroot.Accept(writer); if (!outroot.Accept(writer)) {
throw Exp() << "json write fail";
}
return buffer.GetString(); return buffer.GetString();
} }
...@@ -686,8 +576,18 @@ static std::string jsontoprettystr(const jsondoc& outroot) ...@@ -686,8 +576,18 @@ static std::string jsontoprettystr(const jsondoc& outroot)
{ {
rapidjson::StringBuffer buffer; rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer); rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
outroot.Accept(writer); if (!outroot.Accept(writer)) {
throw Exp() << "json write fail";
}
return buffer.GetString(); return buffer.GetString();
} }
#endif static void loadFileJson(const std::string& filename, rapidjson::Document& document)
{
std::string data;
if (!ReadFile(filename, data)) {
throw Exp() << "failed to read file " << filename;
}
document.Parse(data.c_str());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment