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
1e8ac4e0
Commit
1e8ac4e0
authored
May 25, 2019
by
Alexander Lapshin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
7974b4d4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
108 additions
and
208 deletions
+108
-208
json_functions.h
json/json_functions.h
+108
-208
No files found.
json/json_functions.h
View file @
1e8ac4e0
#ifndef JSONFUNCTIONS_H
#define JSONFUNCTIONS_H
#pragma once
#include "MException.h"
#include <iostream>
...
...
@@ -18,44 +17,74 @@ 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"
;
}
if
(
itr
==
parent
.
MemberEnd
()
&&
!
exist
)
{
if
(
loaded
)
{
*
loaded
=
false
;
}
return
false
;
}
else
if
(
itr
==
parent
.
MemberEnd
()
&&
!
exist
)
{
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
r
=
a
.
Begin
();
itr
!=
a
.
End
();
++
itr
)
{
if
(
it
r
->
IsString
())
{
std
::
string
str
=
it
r
->
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
(
it
r
->
IsInt
())
{
else
if
(
it
->
IsInt
())
{
std
::
ostringstream
ss
;
ss
<<
it
r
->
GetInt
();
ss
<<
it
->
GetInt
();
vec
.
push_back
(
ss
.
str
());
}
else
if
(
it
r
->
IsBool
())
{
else
if
(
it
->
IsBool
())
{
std
::
ostringstream
ss
;
ss
<<
it
r
->
GetBool
();
ss
<<
it
->
GetBool
();
vec
.
push_back
(
ss
.
str
());
}
else
if
(
it
r
->
IsDouble
())
{
else
if
(
it
->
IsDouble
())
{
std
::
ostringstream
ss
;
ss
<<
it
r
->
GetDouble
();
ss
<<
it
->
GetDouble
();
vec
.
push_back
(
ss
.
str
());
}
else
{
...
...
@@ -63,60 +92,38 @@ 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
;
}
const
jsonval
&
a
=
itr
->
value
;
if
(
!
a
.
IsArray
())
{
throw
Exp
()
<<
"error: element is not an array"
;
}
for
(
jsonval
::
ConstValueIterator
it
r
=
a
.
Begin
();
itr
!=
a
.
End
();
++
itr
)
{
if
(
it
r
->
IsInt
())
{
vec
.
push_back
(
it
r
->
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,38 +133,27 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
throw
Exp
()
<<
"error: element is not an array"
;
}
for
(
jsonval
::
ConstValueIterator
it
r
=
a
.
Begin
();
itr
!=
a
.
End
();
++
itr
)
{
if
(
it
r
->
IsDouble
())
{
vec
.
push_back
(
it
r
->
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
;
}
const
jsonval
&
a
=
itr
->
value
;
if
(
!
a
.
IsArray
())
{
throw
Exp
()
<<
"error: element is not an array"
;
}
...
...
@@ -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
;
}
...
...
@@ -200,93 +185,43 @@ template <typename T, typename Tid> bool loadjson(const jsonval& parent, std::st
loadjson
(
a
[
i
],
item
);
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,36 +267,19 @@ 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
;
}
const
jsonval
&
a
=
itr
->
value
;
if
(
a
.
IsNumber
())
{
value
=
a
.
GetDouble
();
}
...
...
@@ -382,30 +300,19 @@ static bool loadjson(const jsonval& parent, const std::string& key, double& valu
else
{
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
;
}
const
jsonval
&
a
=
itr
->
value
;
if
(
a
.
IsNumber
())
{
value
=
a
.
GetInt
();
}
...
...
@@ -427,29 +334,18 @@ 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
;
}
const
jsonval
&
a
=
itr
->
value
;
if
(
a
.
IsInt
())
{
value
=
a
.
GetInt
();
}
...
...
@@ -474,29 +370,18 @@ 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
;
}
const
jsonval
&
a
=
itr
->
value
;
if
(
a
.
IsString
())
{
value
=
a
.
GetString
();
}
...
...
@@ -508,11 +393,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::string&
else
{
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
());
}
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