Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
common
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Alexander Lapshin
common
Commits
641aae96
Commit
641aae96
authored
May 25, 2019
by
Alexander Lapshin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
1e8ac4e0
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
603 additions
and
18 deletions
+603
-18
json_functions.h
json/json_functions.h
+14
-18
json_functions.h.orig
json/json_functions.h.orig
+589
-0
No files found.
json/json_functions.h
View file @
641aae96
...
...
@@ -19,7 +19,7 @@ typedef rapidjson::Document jsondoc;
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
());
jsonval
::
ConstMemberIterator
itr
=
parent
.
FindMember
(
key
.
c_str
());
if
(
itr
==
parent
.
MemberEnd
()
&&
exist
)
{
throw
Exp
()
<<
"error: element '"
<<
key
<<
"' not found"
;
...
...
@@ -57,7 +57,7 @@ inline bool extract_value(const jsonval& parent, const std::string& key, bool ex
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
))
{
if
(
!
extract_value
(
parent
,
key
,
exist
,
loaded
,
itr
))
{
return
false
;
}
...
...
@@ -267,10 +267,6 @@ template <typename T, typename T2> void loadjson(const jsonval& parent, std::map
}
}
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
)
{
jsonval
::
ConstMemberIterator
itr
;
...
...
@@ -528,7 +524,7 @@ static void flushjson(const jsondoc& outroot, const std::string& outputfile, boo
if
(
pretty
)
{
rapidjson
::
StringBuffer
buffer
;
rapidjson
::
PrettyWriter
<
rapidjson
::
StringBuffer
>
writer
(
buffer
);
if
(
!
outroot
.
Accept
(
writer
))
{
if
(
!
outroot
.
Accept
(
writer
))
{
throw
Exp
()
<<
"json write fail"
;
}
std
::
cout
<<
buffer
.
GetString
();
...
...
json/json_functions.h.orig
0 → 100644
View file @
641aae96
#pragma once
#include "MException.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "TextFunctions.h"
#include "Converter.h"
#include "rapidjson/document.h"
#include <rapidjson/ostreamwrapper.h>
#include <rapidjson/prettywriter.h>
#define RAPIDJSON_HAS_STDSTRING 1
typedef rapidjson::Document::AllocatorType jsonalloc;
typedef rapidjson::Value jsonval;
typedef rapidjson::Document jsondoc;
inline bool extract_value(const jsonval& parent, const std::string& key, bool exist, bool* loaded, jsonval::ConstMemberIterator& result)
{
jsonval::ConstMemberIterator itr = parent.FindMember(key.c_str());
if (itr == parent.MemberEnd() && exist) {
throw Exp() << "error: element '" << key << "' not found";
}
if (itr == parent.MemberEnd() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
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 it = a.Begin(); it != a.End(); ++it) {
if (it->IsString()) {
std::string str = it->GetString();
vec.push_back(str);
}
else if (it->IsInt()) {
std::ostringstream ss;
ss << it->GetInt();
vec.push_back(ss.str());
}
else if (it->IsBool()) {
std::ostringstream ss;
ss << it->GetBool();
vec.push_back(ss.str());
}
else if (it->IsDouble()) {
std::ostringstream ss;
ss << it->GetDouble();
vec.push_back(ss.str());
}
else {
throw Exp() << "error: unsupported type";
}
}
return true;
}
static bool loadjson(const jsonval& parent, const std::string& key, std::vector<int>& 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 it = a.Begin(); it != a.End(); ++it) {
if (it->IsInt()) {
vec.push_back(it->GetInt());
}
else {
throw Exp() << "error: unsupported type";
}
}
return true;
}
static bool loadjson(const jsonval& parent, const std::string& key, std::vector<double>& 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 it = a.Begin(); it != a.End(); ++it) {
if (it->IsDouble()) {
vec.push_back(it->GetDouble());
}
else {
throw Exp() << "error: unsupported type";
}
}
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;
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 (rapidjson::SizeType i = 0; i < a.Size(); i++) {
T item;
loadjson(a[i], item);
vec.push_back(item);
}
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;
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 (rapidjson::SizeType i = 0; i < a.Size(); i++) {
T item;
loadjson(a[i], item);
map[item.getId()] = item;
}
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;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
if (!a.IsObject()) {
throw Exp() << "error: element is not an object";
}
f(a, item);
return true;
}
template <typename T> bool loadjson(const jsonval& parent, const std::string& key, T& item, 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.IsObject()) {
throw Exp() << "error: element is not an object";
}
item.load(a);
return true;
}
template <typename T> void loadjson(const jsonval& parent, std::vector<T>& vec)
{
if (parent.IsArray()) {
for (rapidjson::SizeType i = 0; i < parent.Size(); i++) {
T item;
loadjson(parent[i], item);
vec.push_back(item);
}
}
else if (parent.IsObject()) {
for (jsonval::ConstMemberIterator itr = parent.MemberBegin(); itr != parent.MemberEnd(); ++itr) {
T item;
loadjson(itr->value, item);
vec.push_back(item);
}
}
else {
throw Exp() << "error: element is not an array";
}
}
template <typename T, typename T2> void loadjson(const jsonval& parent, std::map<T2, T>& map)
{
if (parent.IsArray()) {
for (rapidjson::SizeType i = 0; i < parent.Size(); i++) {
T item;
loadjson(parent[i], item);
map[item.getId()] = item;
}
}
else if (parent.IsObject()) {
for (jsonval::ConstMemberIterator itr = parent.MemberBegin(); itr != parent.MemberEnd(); ++itr) {
T item;
loadjson(itr->value, item);
map[item.getId()] = item;
}
}
else {
throw Exp() << "error: element is not an array";
}
}
static bool loadjson(const jsonval& parent, const std::string& key, double& value, 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.IsNumber()) {
value = a.GetDouble();
}
else if (a.IsString()) {
std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
}
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
}
return true;
}
static bool loadjson(const jsonval& parent, const std::string& key, int& value, 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.IsNumber()) {
value = a.GetInt();
}
else if (a.IsString()) {
std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
}
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
}
return true;
}
static bool loadjson(const jsonval& parent, const std::string& key, bool& value, 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.IsInt()) {
value = a.GetInt();
}
else if (a.IsBool()) {
value = a.GetBool();
}
else if (a.IsString()) {
std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
}
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
}
return true;
}
static bool loadjson(const jsonval& parent, const std::string& key, std::string& value, 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.IsString()) {
value = a.GetString();
}
else if (a.IsInt()) {
std::stringstream stream;
stream << a.GetInt();
value = stream.str();
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
}
return true;
}
template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, std::string conName, const T& object, void(*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
{
jsonval obj(rapidjson::kObjectType);
f(alc, obj, object);
jsonval jname(conName, alc);
ss.AddMember(jname, obj, alc);
}
template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, std::string conName, const std::vector<T>& vec, void(*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
{
jsonval jsvec(rapidjson::kArrayType);
for (typename std::vector<T>::const_iterator it = vec.begin(); it != vec.end(); ++it) {
jsonval child(rapidjson::kObjectType);
f(alc, child, *it);
jsvec.PushBack(child, alc);
}
jsonval jname(conName, alc);
ss.AddMember(jname, jsvec, alc);
}
template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, std::string conName, const std::vector<const T*>& vec, void(*f)(rapidjson::Document::AllocatorType&, jsonval&, const T*))
{
jsonval jsvec(rapidjson::kArrayType);
for (typename std::vector<const T*>::const_iterator it = vec.begin(); it != vec.end(); ++it) {
jsonval child(rapidjson::kObjectType);
f(alc, child, *it);
jsvec.PushBack(child, alc);
}
jsonval jname(conName, alc);
ss.AddMember(jname, jsvec, alc);
}
template <typename T, typename TK > void PutValue(jsonalloc& alc, jsonval& ss, std::string conName, const std::map<TK, T>& vec, void(*f)(rapidjson::Document::AllocatorType&, jsonval&, const T&))
{
jsonval jsvec(rapidjson::kObjectType);
for (typename std::map<TK, T>::const_iterator it = vec.begin(); it != vec.end(); ++it) {
jsonval child(rapidjson::kObjectType);
f(alc, child, it->second);
std::stringstream stream;
stream << it->first;
std::string key = stream.str();
jsonval jkey(key, alc);
jsvec.AddMember(jkey, child, alc);
}
jsonval jname(conName, alc);
ss.AddMember(jname, jsvec, alc);
}
template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, std::string conName, const std::vector<T>& vec)
{
jsonval jsvec(rapidjson::kArrayType);
for (typename std::vector<T>::const_iterator it = vec.begin(); it != vec.end(); ++it) {
jsonval child(rapidjson::kObjectType);
PutValue(alc, child, *it);
jsvec.PushBack(child, alc);
}
jsonval jname(conName, alc);
ss.AddMember(jname, jsvec, alc);
}
template <typename T> void PutValue(jsonalloc& alc, jsonval& ss, std::string conName, const T& object)
{
jsonval obj(rapidjson::kObjectType);
PutValue(alc, obj, object);
jsonval jname(conName, alc);
ss.AddMember(jname, obj, alc);
}
static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, size_t value)
{
jsonval jname(conName, alc);
ss.AddMember(jname, value, alc);
}
static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, bool value)
{
jsonval jname(conName, alc);
ss.AddMember(jname, value, alc);
}
static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, int value)
{
jsonval jname(conName, alc);
ss.AddMember(jname, value, alc);
}
static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, double value)
{
jsonval jname(conName, alc);
ss.AddMember(jname, value, alc);
}
static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const char* value)
{
jsonval jname(conName, alc);
jsonval jvalue(value, alc);
ss.AddMember(jname, jvalue, alc);
}
static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const std::string& value)
{
jsonval jname(conName, alc);
jsonval jvalue(value, alc);
ss.AddMember(jname, jvalue, alc);
}
static void PutValue(jsonalloc& alc, jsonval& ss, double value)
{
ss.PushBack(value, alc);
}
static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& value)
{
jsonval jvalue(value, alc);
ss.PushBack(jvalue, alc);
}
static void flushjson(const jsondoc& outroot, const std::string& outputfile, bool pretty = true)
{
if (!outputfile.length()) {
if (pretty) {
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
if(!outroot.Accept(writer)){
throw Exp() << "json write fail";
}
std::cout << buffer.GetString();
}
else {
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
if (!outroot.Accept(writer)) {
throw Exp() << "json write fail";
}
std::cout << buffer.GetString();
}
}
else {
if (pretty) {
std::ofstream ofs(outputfile.c_str());
rapidjson::OStreamWrapper osw(ofs);
rapidjson::PrettyWriter<rapidjson::OStreamWrapper> writer(osw);
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);
if (!outroot.Accept(writer)) {
throw Exp() << "json write fail";
}
}
}
}
static std::string jsontostr(const jsondoc& outroot)
{
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
if (!outroot.Accept(writer)) {
throw Exp() << "json write fail";
}
return buffer.GetString();
}
static std::string jsontoprettystr(const jsondoc& outroot)
{
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
if (!outroot.Accept(writer)) {
throw Exp() << "json write fail";
}
return buffer.GetString();
}
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());
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment