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
74e024b9
Commit
74e024b9
authored
Jun 15, 2021
by
Alexander Lapshin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
+ get_ts, json loading data types, refactoring
parent
c309791e
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
279 additions
and
34 deletions
+279
-34
json_functions.h
json/json_functions.h
+205
-28
json_functions_bno.h
json/json_functions_bno.h
+36
-1
Converter.h
utils/Converter.h
+6
-0
TimeFunctions.h
utils/TimeFunctions.h
+32
-5
No files found.
json/json_functions.h
View file @
74e024b9
...
...
@@ -55,6 +55,30 @@ inline bool extract_value(const jsonval& parent, const std::string& key, bool ex
return
true
;
}
inline
bool
extract_value
(
const
std
::
map
<
std
::
string
,
std
::
string
>&
parent
,
const
std
::
string
&
key
,
bool
exist
,
bool
*
loaded
,
std
::
map
<
std
::
string
,
std
::
string
>::
const_iterator
&
result
)
{
const
auto
itr
=
parent
.
find
(
key
);
if
(
itr
==
parent
.
end
()
&&
exist
)
{
throw
Exp
()
<<
"error: element '"
<<
key
<<
"' not found"
;
}
if
(
itr
==
parent
.
end
()
&&
!
exist
)
{
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
,
const
bool
exist
=
true
,
bool
*
loaded
=
nullptr
)
{
jsonval
::
ConstMemberIterator
itr
;
...
...
@@ -162,7 +186,8 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
for
(
jsonval
::
ConstValueIterator
it
=
a
.
Begin
();
it
!=
a
.
End
();
++
it
)
{
if
(
it
->
IsDouble
())
{
vec
.
push_back
(
it
->
GetDouble
());
}
else
if
(
it
->
IsInt
())
{
}
else
if
(
it
->
IsInt
())
{
vec
.
push_back
(
it
->
GetInt
());
}
else
{
...
...
@@ -173,8 +198,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::vector<
return
true
;
}
template
<
typename
T
>
bool
loadjson
(
const
jsonval
&
parent
,
std
::
string
key
,
std
::
vector
<
T
>&
vec
,
const
bool
exist
=
true
,
bool
*
loaded
=
nullptr
)
template
<
typename
T
>
bool
loadjson
(
const
jsonval
&
parent
,
std
::
string
key
,
std
::
vector
<
T
>&
vec
,
const
bool
exist
=
true
,
bool
*
loaded
=
nullptr
)
{
jsonval
::
ConstMemberIterator
itr
;
if
(
!
extract_value
(
parent
,
key
,
exist
,
loaded
,
itr
))
{
...
...
@@ -196,8 +220,7 @@ bool loadjson(const jsonval& parent, std::string key, std::vector<T>& vec, const
return
true
;
}
template
<
typename
T
,
typename
Tid
>
bool
loadjson
(
const
jsonval
&
parent
,
const
std
::
string
key
,
std
::
map
<
Tid
,
T
>&
map
,
const
bool
exist
=
true
,
bool
*
loaded
=
nullptr
)
template
<
typename
T
,
typename
Tid
>
bool
loadjson
(
const
jsonval
&
parent
,
const
std
::
string
key
,
std
::
map
<
Tid
,
T
>&
map
,
const
bool
exist
=
true
,
bool
*
loaded
=
nullptr
)
{
jsonval
::
ConstMemberIterator
itr
;
if
(
!
extract_value
(
parent
,
key
,
exist
,
loaded
,
itr
))
{
...
...
@@ -219,8 +242,7 @@ bool loadjson(const jsonval& parent, const std::string key, std::map<Tid, T>& ma
return
true
;
}
template
<
typename
T
>
bool
loadjson
(
const
jsonval
&
parent
,
std
::
string
key
,
T
&
item
,
void
(
*
f
)(
const
jsonval
&
,
T
&
),
const
bool
exist
=
true
,
bool
*
loaded
=
nullptr
)
template
<
typename
T
>
bool
loadjson
(
const
jsonval
&
parent
,
std
::
string
key
,
T
&
item
,
void
(
*
f
)(
const
jsonval
&
,
T
&
),
const
bool
exist
=
true
,
bool
*
loaded
=
nullptr
)
{
jsonval
::
ConstMemberIterator
itr
;
if
(
!
extract_value
(
parent
,
key
,
exist
,
loaded
,
itr
))
{
...
...
@@ -238,8 +260,7 @@ bool loadjson(const jsonval& parent, std::string key, T& item, void (*f)(const j
return
true
;
}
template
<
typename
T
>
bool
loadjson
(
const
jsonval
&
parent
,
const
std
::
string
&
key
,
T
&
item
,
const
bool
exist
=
true
,
bool
*
loaded
=
nullptr
)
template
<
typename
T
>
bool
loadjson
(
const
jsonval
&
parent
,
const
std
::
string
&
key
,
T
&
item
,
const
bool
exist
=
true
,
bool
*
loaded
=
nullptr
)
{
jsonval
::
ConstMemberIterator
itr
;
if
(
!
extract_value
(
parent
,
key
,
exist
,
loaded
,
itr
))
{
...
...
@@ -257,8 +278,7 @@ bool loadjson(const jsonval& parent, const std::string& key, T& item, const bool
return
true
;
}
template
<
typename
T
>
void
loadjson
(
const
jsonval
&
parent
,
std
::
vector
<
T
>&
vec
)
template
<
typename
T
>
void
loadjson
(
const
jsonval
&
parent
,
std
::
vector
<
T
>&
vec
)
{
if
(
parent
.
IsArray
())
{
for
(
rapidjson
::
SizeType
i
=
0
;
i
<
parent
.
Size
();
i
++
)
{
...
...
@@ -279,8 +299,7 @@ void loadjson(const jsonval& parent, std::vector<T>& vec)
}
}
template
<
typename
T
,
typename
T2
>
void
loadjson
(
const
jsonval
&
parent
,
std
::
map
<
T2
,
T
>&
map
)
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
++
)
{
...
...
@@ -526,8 +545,7 @@ static bool loadjson(const jsonval& parent, const std::string& key, std::string&
return
true
;
}
template
<
typename
T
>
void
PutValue
(
jsonalloc
&
alc
,
jsonval
&
ss
,
const
std
::
string
&
conName
,
const
T
&
object
,
void
(
*
f
)(
rapidjson
::
Document
::
AllocatorType
&
,
jsonval
&
,
const
T
&
))
template
<
typename
T
>
void
PutValue
(
jsonalloc
&
alc
,
jsonval
&
ss
,
const
std
::
string
&
conName
,
const
T
&
object
,
void
(
*
f
)(
rapidjson
::
Document
::
AllocatorType
&
,
jsonval
&
,
const
T
&
))
{
jsonval
obj
(
rapidjson
::
kObjectType
);
f
(
alc
,
obj
,
object
);
...
...
@@ -536,8 +554,7 @@ void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const T&
ss
.
AddMember
(
jname
,
obj
,
alc
);
}
template
<
typename
T
>
void
PutValue
(
jsonalloc
&
alc
,
jsonval
&
ss
,
const
std
::
string
&
conName
,
const
std
::
vector
<
T
>&
vec
,
bool
(
*
f
)(
rapidjson
::
Document
::
AllocatorType
&
,
jsonval
&
,
const
T
&
))
template
<
typename
T
>
void
PutValue
(
jsonalloc
&
alc
,
jsonval
&
ss
,
const
std
::
string
&
conName
,
const
std
::
vector
<
T
>&
vec
,
bool
(
*
f
)(
rapidjson
::
Document
::
AllocatorType
&
,
jsonval
&
,
const
T
&
))
{
jsonval
jsvec
(
rapidjson
::
kArrayType
);
for
(
auto
it
=
vec
.
begin
();
it
!=
vec
.
end
();
++
it
)
{
...
...
@@ -551,8 +568,7 @@ void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const std
ss
.
AddMember
(
jname
,
jsvec
,
alc
);
}
template
<
typename
T
>
void
PutValue
(
jsonalloc
&
alc
,
jsonval
&
ss
,
const
std
::
string
conName
,
const
std
::
vector
<
T
*>&
vec
,
bool
(
*
f
)(
rapidjson
::
Document
::
AllocatorType
&
,
jsonval
&
,
const
T
&
))
template
<
typename
T
>
void
PutValue
(
jsonalloc
&
alc
,
jsonval
&
ss
,
const
std
::
string
conName
,
const
std
::
vector
<
T
*>&
vec
,
bool
(
*
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
)
{
...
...
@@ -566,8 +582,7 @@ void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const std:
ss
.
AddMember
(
jname
,
jsvec
,
alc
);
}
template
<
typename
T
,
typename
TK
>
void
PutValue
(
jsonalloc
&
alc
,
jsonval
&
ss
,
const
std
::
string
conName
,
const
std
::
map
<
TK
,
T
>&
vec
,
bool
(
*
f
)(
rapidjson
::
Document
::
AllocatorType
&
,
jsonval
&
,
const
T
&
))
template
<
typename
T
,
typename
TK
>
void
PutValue
(
jsonalloc
&
alc
,
jsonval
&
ss
,
const
std
::
string
conName
,
const
std
::
map
<
TK
,
T
>&
vec
,
bool
(
*
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
)
{
...
...
@@ -586,8 +601,7 @@ void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const std:
ss
.
AddMember
(
jname
,
jsvec
,
alc
);
}
template
<
typename
T
,
typename
TK
>
void
PutMapAsVec
(
jsonalloc
&
alc
,
jsonval
&
ss
,
const
std
::
string
conName
,
const
std
::
map
<
TK
,
T
>&
vec
,
bool
(
*
f
)(
rapidjson
::
Document
::
AllocatorType
&
,
jsonval
&
,
const
T
&
))
template
<
typename
T
,
typename
TK
>
void
PutMapAsVec
(
jsonalloc
&
alc
,
jsonval
&
ss
,
const
std
::
string
conName
,
const
std
::
map
<
TK
,
T
>&
vec
,
bool
(
*
f
)(
rapidjson
::
Document
::
AllocatorType
&
,
jsonval
&
,
const
T
&
))
{
jsonval
jsvec
(
rapidjson
::
kArrayType
);
for
(
typename
std
::
map
<
TK
,
T
>::
const_iterator
it
=
vec
.
begin
();
it
!=
vec
.
end
();
++
it
)
{
...
...
@@ -650,8 +664,7 @@ inline void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, co
ss
.
AddMember
(
jname
,
jsvec
,
alc
);
}
template
<
typename
T
>
void
PutValue
(
jsonalloc
&
alc
,
jsonval
&
ss
,
const
std
::
string
conName
,
const
std
::
vector
<
T
>&
vec
)
template
<
typename
T
>
void
PutValue
(
jsonalloc
&
alc
,
jsonval
&
ss
,
const
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
)
{
...
...
@@ -664,8 +677,7 @@ void PutValue(jsonalloc& alc, jsonval& ss, const std::string conName, const std:
ss
.
AddMember
(
jname
,
jsvec
,
alc
);
}
template
<
typename
T
>
void
PutValue
(
jsonalloc
&
alc
,
jsonval
&
ss
,
const
std
::
string
conName
,
const
T
&
object
)
template
<
typename
T
>
void
PutValue
(
jsonalloc
&
alc
,
jsonval
&
ss
,
const
std
::
string
conName
,
const
T
&
object
)
{
jsonval
obj
(
rapidjson
::
kObjectType
);
PutValue
(
alc
,
obj
,
object
);
...
...
@@ -890,3 +902,168 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<int>&
return
true
;
}
static
bool
loadjson
(
const
std
::
map
<
std
::
string
,
std
::
string
>&
parent
,
const
std
::
string
&
key
,
double
&
value
,
const
bool
exist
=
true
,
bool
*
loaded
=
nullptr
)
{
std
::
map
<
std
::
string
,
std
::
string
>::
const_iterator
itr
;
if
(
!
extract_value
(
parent
,
key
,
exist
,
loaded
,
itr
))
{
return
false
;
}
const
std
::
string
&
str
=
itr
->
second
;
if
(
str
.
empty
()
&&
!
exist
)
{
if
(
loaded
)
{
*
loaded
=
false
;
}
return
false
;
}
if
(
!
Converter
::
ToNumber
(
str
,
value
))
{
throw
(
Exp
()
<<
"bad value format '"
<<
str
<<
"'"
<<
" in container '"
);
}
return
true
;
}
static
bool
loadjson
(
const
std
::
map
<
std
::
string
,
std
::
string
>&
parent
,
const
std
::
string
&
key
,
int
&
value
,
const
bool
exist
=
true
,
bool
*
loaded
=
nullptr
)
{
std
::
map
<
std
::
string
,
std
::
string
>::
const_iterator
itr
;
if
(
!
extract_value
(
parent
,
key
,
exist
,
loaded
,
itr
))
{
return
false
;
}
const
std
::
string
&
str
=
itr
->
second
;
if
(
str
.
empty
()
&&
!
exist
)
{
if
(
loaded
)
{
*
loaded
=
false
;
}
return
false
;
}
if
(
!
Converter
::
ToNumber
(
str
,
value
))
{
throw
(
Exp
()
<<
"bad value format '"
<<
str
<<
"'"
<<
" in container '"
);
}
return
true
;
}
static
bool
loadjson
(
const
std
::
map
<
std
::
string
,
std
::
string
>&
parent
,
const
std
::
string
&
key
,
long
long
&
value
,
const
bool
exist
=
true
,
bool
*
loaded
=
nullptr
)
{
std
::
map
<
std
::
string
,
std
::
string
>::
const_iterator
itr
;
if
(
!
extract_value
(
parent
,
key
,
exist
,
loaded
,
itr
))
{
return
false
;
}
const
std
::
string
&
str
=
itr
->
second
;
if
(
str
.
empty
()
&&
!
exist
)
{
if
(
loaded
)
{
*
loaded
=
false
;
}
return
false
;
}
if
(
!
Converter
::
ToNumber
(
str
,
value
))
{
throw
(
Exp
()
<<
"bad value format '"
<<
str
<<
"'"
<<
" in container '"
);
}
return
true
;
}
static
bool
loadjson
(
const
std
::
map
<
std
::
string
,
std
::
string
>&
parent
,
const
std
::
string
&
key
,
bool
&
value
,
const
bool
exist
=
true
,
bool
*
loaded
=
nullptr
)
{
std
::
map
<
std
::
string
,
std
::
string
>::
const_iterator
itr
;
if
(
!
extract_value
(
parent
,
key
,
exist
,
loaded
,
itr
))
{
return
false
;
}
const
std
::
string
&
str
=
itr
->
second
;
if
(
str
.
empty
()
&&
!
exist
)
{
if
(
loaded
)
{
*
loaded
=
false
;
}
return
false
;
}
if
(
!
Converter
::
ToNumber
(
str
,
value
))
{
throw
(
Exp
()
<<
"bad value format '"
<<
str
<<
"'"
<<
" in container '"
);
}
return
true
;
}
static
bool
loadjson
(
const
std
::
map
<
std
::
string
,
std
::
string
>&
parent
,
const
std
::
string
&
key
,
std
::
string
&
value
,
const
bool
exist
=
true
,
bool
*
loaded
=
nullptr
)
{
std
::
map
<
std
::
string
,
std
::
string
>::
const_iterator
itr
;
if
(
!
extract_value
(
parent
,
key
,
exist
,
loaded
,
itr
))
{
return
false
;
}
value
=
itr
->
second
;
return
true
;
}
static
bool
loadjson
(
const
std
::
map
<
std
::
string
,
std
::
string
>&
parent
,
const
std
::
string
&
key
,
svalue
<
double
>&
value
,
const
bool
exist
=
true
)
{
std
::
map
<
std
::
string
,
std
::
string
>::
const_iterator
itr
;
bool
*
loaded
=
nullptr
;
if
(
!
extract_value
(
parent
,
key
,
exist
,
loaded
,
itr
))
{
return
false
;
}
const
std
::
string
&
str
=
itr
->
second
;
if
(
str
.
empty
()
&&
!
exist
)
{
if
(
loaded
)
{
*
loaded
=
false
;
}
return
false
;
}
double
val
;
if
(
!
Converter
::
ToNumber
(
str
,
val
))
{
throw
(
Exp
()
<<
"bad value format '"
<<
str
<<
"'"
<<
" in container '"
);
}
value
=
val
;
return
true
;
}
static
bool
loadjson
(
const
std
::
map
<
std
::
string
,
std
::
string
>&
parent
,
const
std
::
string
&
key
,
svalue
<
int
>&
value
,
const
bool
exist
=
true
)
{
std
::
map
<
std
::
string
,
std
::
string
>::
const_iterator
itr
;
bool
*
loaded
=
nullptr
;
if
(
!
extract_value
(
parent
,
key
,
exist
,
loaded
,
itr
))
{
return
false
;
}
const
std
::
string
&
str
=
itr
->
second
;
if
(
str
.
empty
()
&&
!
exist
)
{
if
(
loaded
)
{
*
loaded
=
false
;
}
return
false
;
}
int
val
;
if
(
!
Converter
::
ToNumber
(
str
,
val
))
{
throw
(
Exp
()
<<
"bad value format '"
<<
str
<<
"'"
<<
" in container '"
);
}
value
=
val
;
return
true
;
}
json/json_functions_bno.h
View file @
74e024b9
...
...
@@ -112,3 +112,38 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<TimeJ
return
true
;
}
static
bool
loadjson
(
const
std
::
map
<
std
::
string
,
std
::
string
>&
parent
,
const
std
::
string
&
key
,
svalue
<
TimeJD
>&
value
,
const
bool
exist
=
true
)
{
std
::
map
<
std
::
string
,
std
::
string
>::
const_iterator
itr
;
bool
*
loaded
=
nullptr
;
if
(
!
extract_value
(
parent
,
key
,
exist
,
loaded
,
itr
))
{
return
false
;
}
value
=
StrToDate
(
itr
->
second
);
return
true
;
}
static
bool
loadjson
(
const
std
::
map
<
std
::
string
,
std
::
string
>&
parent
,
const
std
::
string
&
key
,
TimeJD
&
value
,
const
bool
exist
=
true
,
bool
*
loaded
=
nullptr
)
{
std
::
map
<
std
::
string
,
std
::
string
>::
const_iterator
itr
;
if
(
!
extract_value
(
parent
,
key
,
exist
,
loaded
,
itr
))
{
return
false
;
}
const
std
::
string
&
str
=
itr
->
second
;
if
(
str
.
empty
()
&&
!
exist
)
{
if
(
loaded
)
{
*
loaded
=
false
;
}
return
false
;
}
value
=
StrToDate
(
str
);
return
true
;
}
utils/Converter.h
View file @
74e024b9
...
...
@@ -81,4 +81,10 @@ public:
val
=
std
::
stoi
(
str
,
nullptr
);
return
true
;
}
static
bool
ToNumber
(
const
std
::
string
&
str
,
long
long
&
val
)
{
val
=
std
::
stoll
(
str
,
nullptr
);
return
true
;
}
};
utils/TimeFunctions.h
View file @
74e024b9
...
...
@@ -144,7 +144,7 @@ static std::string DateToStr(int year, int month, int day, int hour, int minute,
std
::
ostringstream
ss
;
if
(
!
year_first
)
{
if
(
!
year_first
)
{
ss
<<
std
::
setfill
(
'0'
);
ss
<<
std
::
setw
(
2
)
<<
day
<<
"/"
;
ss
<<
std
::
setw
(
2
)
<<
month
<<
"/"
;
...
...
@@ -152,7 +152,8 @@ static std::string DateToStr(int year, int month, int day, int hour, int minute,
ss
<<
std
::
setw
(
2
)
<<
d
<<
":"
;
ss
<<
std
::
setw
(
2
)
<<
m
<<
":"
;;
ss
<<
std
::
setw
(
2
)
<<
s
;
}
else
{
}
else
{
ss
<<
std
::
setfill
(
'0'
);
ss
<<
std
::
setw
(
4
)
<<
year
<<
"/"
;
ss
<<
std
::
setw
(
2
)
<<
month
<<
"/"
;
...
...
@@ -503,3 +504,29 @@ inline TimeJD get_current_time()
return
now
;
}
inline
long
long
get_ts
()
{
return
std
::
chrono
::
duration_cast
<
std
::
chrono
::
microseconds
>
(
std
::
chrono
::
system_clock
::
now
().
time_since_epoch
()).
count
()
/
1000
;
}
inline
long
long
get_ts
(
const
TimeJD
&
date
)
{
const
TimeGD
gd
=
date
;
int
jd
;
double
jf
;
convertTimeGJ
(
gd
.
Year
,
gd
.
Month
,
gd
.
Day
,
gd
.
Hour
,
gd
.
Min
,
gd
.
Sec
,
gd
.
Fraction
,
jd
,
jf
);
return
(
jd
+
jf
-
2440587
.
5
)
*
86400
.
0
*
1000
;
}
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