FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
abacus-develop/source/source_io/json_output/abacusjson.cpp at develop · pplab/abacus-develop · GitHub
pplab
/
abacus-develop
Public
forked from
deepmodeling/abacus-develop
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
abacus-develop
/
source
/
source_io
/
json_output
/
abacusjson.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
152 lines (136 loc) · 4.68 KB
Breadcrumbs
abacus-develop
/
source
/
source_io
/
json_output
/
abacusjson.cpp
Copy path
File metadata and controls
152 lines (136 loc) · 4.68 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#
include
"
abacusjson.h
"
#
include
<
fstream
>
#
include
<
iostream
>
#
include
<
string
>
#
include
<
vector
>
#
include
<
sstream
>
namespace
Json
{
#
ifdef
__RAPIDJSON
rapidjson::Document AbacusJson::doc;
bool
isNum
(std::string str)
{
std::stringstream sin;
sin<<str;
double
d;
char
c;
if
(!(sin >> d))
return
false
;
if
(sin >> c)
return
false
;
return
true
;
}
void
AbacusJson::add_nested_member
(std::vector<jsonKeyNode>::iterator begin,
std::vector<jsonKeyNode>::iterator end,
rapidjson::Value& val,
rapidjson::Value& parent,
rapidjson::Document::AllocatorType& allocator,
bool
IsArray
)
{
if
(begin != end)
{
jsonKeyNode keyNode = *begin;
rapidjson::Value
key
((*begin).
key
.
c_str
(), allocator);
if
(begin +
1
== end)
{
if
( keyNode.
key
.
empty
() && parent.
IsArray
()){
int
index = keyNode.
i
;
if
(index>=
0
){
parent[index] = val;
}
else
{
int
arr_size = parent.
Size
();
parent[arr_size+index] = val;
}
}
//
if key exists, then overwrite it
else
if
(parent.
HasMember
(key))
{
if
(parent[key].
IsArray
()){
parent[key].
PushBack
(val, allocator);
}
else
{
//
if key is an object, then warn the user
if
(parent[key].
IsObject
())
{
std::cout <<
"
Warning: write to json, key
"
<< (*begin).
key
<<
"
exist and is an object, and abacus will overwrite it with a value.
"
<< std::endl;
}
parent[key] = val;
}
}
else
{
if
(IsArray==
true
){
rapidjson::Value
arr
(rapidjson::
kArrayType
);
arr.
PushBack
(val, allocator);
parent.
AddMember
(key, arr, allocator);
}
else
{
parent.
AddMember
(key, val, allocator);
}
}
}
else
{
if
( keyNode.
key
.
empty
()&&parent.
IsArray
()){
int
index = keyNode.
i
;
if
(index>=
0
){
add_nested_member
(begin +
1
, end, val, parent[index], allocator,IsArray);
}
else
{
int
arr_size = parent.
Size
();
add_nested_member
(begin +
1
, end, val, parent[arr_size+index], allocator,IsArray);
}
}
//
need to check if the key exists
else
if
(parent.
HasMember
(key))
{
//
this key should be an object
if
(!parent[key].
IsObject
()&&!parent[key].
IsArray
())
{
std::cout <<
"
Warning: write to json, key
"
<< (*begin).
key
<<
"
exist and is not an object or array, and abacus will add it as a middle node.
"
<< std::endl;
}
add_nested_member
(begin +
1
, end, val, parent[key], allocator,IsArray);
}
else
{
rapidjson::Value
paraent_val
(rapidjson::
kObjectType
);
add_nested_member
(begin +
1
, end, val, paraent_val, allocator,IsArray);
parent.
AddMember
(key, paraent_val, allocator);
}
}
}
}
//
Output the json to a file
void
AbacusJson::write_to_json
(std::string filename)
{
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer>
writer
(buffer);
doc.
Accept
(writer);
std::ofstream
ofs
(filename);
ofs << buffer.
GetString
();
ofs.
close
();
};
template
<>
void
AbacusJson::add_json
(std::vector<jsonKeyNode> keys,
const
std::string& value,
bool
IsArray)
{
if
(!doc.
IsObject
())
{
doc.
SetObject
();
}
rapidjson::Value
val
(value.
c_str
(), doc.
GetAllocator
());
add_nested_member
(keys.
begin
(), keys.
end
(), val, doc, doc.
GetAllocator
(),IsArray);
}
//
Overloaded template functions for json class objects
template
<>
void
AbacusJson::add_json
(std::vector<jsonKeyNode> keys,
const
rapidjson::Value& value,
bool
IsArray)
{
if
(!doc.
IsObject
())
{
doc.
SetObject
();
}
rapidjson::Value
val
(value,doc.
GetAllocator
());
add_nested_member
(keys.
begin
(), keys.
end
(), val, doc, doc.
GetAllocator
(),IsArray);
}
#
endif
}
//
namespace Json
Back
|
FazBrowse Home
|
New Git URL