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

.

parent 7974b4d4
#ifndef JSONFUNCTIONS_H
#define JSONFUNCTIONS_H
#pragma once
#include "MException.h"
#include <iostream>
......@@ -18,14 +17,15 @@ typedef rapidjson::Document::AllocatorType jsonalloc;
typedef rapidjson::Value jsonval;
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());
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) {
*loaded = false;
}
......@@ -34,28 +34,57 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
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()) {
throw Exp() << "error: element is not an array";
}
for (jsonval::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) {
if (itr->IsString()) {
std::string str = itr->GetString();
for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) {
if (it->IsString()) {
std::string str = it->GetString();
vec.push_back(str);
}
else if (itr->IsInt()) {
else if (it->IsInt()) {
std::ostringstream ss;
ss << itr->GetInt();
ss << it->GetInt();
vec.push_back(ss.str());
}
else if (itr->IsBool()) {
else if (it->IsBool()) {
std::ostringstream ss;
ss << itr->GetBool();
ss << it->GetBool();
vec.push_back(ss.str());
}
else if (itr->IsDouble()) {
else if (it->IsDouble()) {
std::ostringstream ss;
ss << itr->GetDouble();
ss << it->GetDouble();
vec.push_back(ss.str());
}
else {
......@@ -63,24 +92,13 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
}
}
if (loaded) {
*loaded = true;
}
return true;
}
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());
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
......@@ -90,33 +108,22 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
throw Exp() << "error: element is not an array";
}
for (jsonval::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) {
if (itr->IsInt()) {
vec.push_back(itr->GetInt());
for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) {
if (it->IsInt()) {
vec.push_back(it->GetInt());
}
else {
throw Exp() << "error: unsupported type";
}
}
if (loaded) {
*loaded = true;
}
return true;
}
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());
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
......@@ -126,33 +133,22 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
throw Exp() << "error: element is not an array";
}
for (jsonval::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) {
if (itr->IsDouble()) {
vec.push_back(itr->GetDouble());
for (jsonval::ConstValueIterator it = a.Begin(); it != a.End(); ++it) {
if (it->IsDouble()) {
vec.push_back(it->GetDouble());
}
else {
throw Exp() << "error: unsupported type";
}
}
if (loaded) {
*loaded = true;
}
return true;
}
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());
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
......@@ -168,24 +164,13 @@ template <typename T> bool loadjson(const jsonval& parent, std::string key, std:
vec.push_back(item);
}
if (loaded) {
*loaded = 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)
{
jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str());
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
......@@ -201,92 +186,42 @@ template <typename T, typename Tid> bool loadjson(const jsonval& parent, std::st
map[item.getId()] = item;
}
if (loaded) {
*loaded = 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)
{
jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str());
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
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()) {
throw Exp() << "error: element is not an object";
}
f(a, item);
if (loaded) {
*loaded = 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());
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
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()) {
throw Exp() << "error: element is not an object";
}
item.load(a);
if (loaded) {
*loaded = true;
}
return true;
}
......@@ -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) {
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)
{
const jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str());
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
......@@ -383,24 +301,13 @@ static bool loadjson(const jsonval& parent, const std::string& key, double& valu
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
}
if (loaded) {
*loaded = true;
}
return true;
}
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());
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
......@@ -427,24 +334,13 @@ static bool loadjson(const jsonval& parent, const std::string& key, int& value,
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
}
if (loaded) {
*loaded = true;
}
return true;
}
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());
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
......@@ -474,24 +370,13 @@ static bool loadjson(const jsonval& parent, const std::string& key, bool& value,
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
}
if (loaded) {
*loaded = true;
}
return true;
}
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());
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element not found " << key;
}
else if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
......@@ -509,10 +394,6 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::string&
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
}
if (loaded) {
*loaded = true;
}
return true;
}
......@@ -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)
{
pretty = true;
if (!outputfile.length()) {
if (pretty) {
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
outroot.Accept(writer);
if(!outroot.Accept(writer)){
throw Exp() << "json write fail";
}
std::cout << buffer.GetString();
}
else {
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
outroot.Accept(writer);
if (!outroot.Accept(writer)) {
throw Exp() << "json write fail";
}
std::cout << buffer.GetString();
}
}
......@@ -663,13 +547,17 @@ static void flushjson(const jsondoc& outroot, const std::string& outputfile, boo
std::ofstream ofs(outputfile.c_str());
rapidjson::OStreamWrapper osw(ofs);
rapidjson::PrettyWriter<rapidjson::OStreamWrapper> writer(osw);
outroot.Accept(writer);
if (!outroot.Accept(writer)) {
throw Exp() << "json write fail";
}
}
else {
std::ofstream ofs(outputfile.c_str());
rapidjson::OStreamWrapper osw(ofs);
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)
{
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
outroot.Accept(writer);
if (!outroot.Accept(writer)) {
throw Exp() << "json write fail";
}
return buffer.GetString();
}
......@@ -686,8 +576,18 @@ static std::string jsontoprettystr(const jsondoc& outroot)
{
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
outroot.Accept(writer);
if (!outroot.Accept(writer)) {
throw Exp() << "json write fail";
}
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