Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CxxClient
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TGenBase
CxxClient
Commits
1821c082
Commit
1821c082
authored
4 years ago
by
Evgeny Lavrik
Browse files
Options
Downloads
Patches
Plain Diff
Update jsoncons
Add convenience methods to convert, read and write json objects to files
parent
64ccb862
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
TGenBase/ObjectStore.h
+1
-1
1 addition, 1 deletion
TGenBase/ObjectStore.h
TGenBase/TGenBaseLinkDef.h
+0
-1
0 additions, 1 deletion
TGenBase/TGenBaseLinkDef.h
TGenBase/Utils.h
+74
-10
74 additions, 10 deletions
TGenBase/Utils.h
external/InstallJsoncons.cmake
+2
-1
2 additions, 1 deletion
external/InstallJsoncons.cmake
with
77 additions
and
13 deletions
TGenBase/ObjectStore.h
+
1
−
1
View file @
1821c082
...
@@ -184,8 +184,8 @@ class ObjectStore : public TGenBase::BaseClassVersioned<ObjectStore>
...
@@ -184,8 +184,8 @@ class ObjectStore : public TGenBase::BaseClassVersioned<ObjectStore>
}
// namespace TGenBase
}
// namespace TGenBase
// JsonCons extension for this class
// JsonCons extension for this class
JSONCONS_PROPERTY_TRAITS_DECL
(
TGenBase
::
ObjectStore
::
TestObject
,
Get
,
Set
,
Id
,
Param
,
CreatedAt
);
JSON_FOR
(
TGenBase
::
ObjectStore
);
JSON_FOR
(
TGenBase
::
ObjectStore
);
JSONCONS_N_GETTER_SETTER_TRAITS
(
TGenBase
::
ObjectStore
::
TestObject
,
Get
,
Set
,
0
,
Id
,
Param
,
CreatedAt
);
#endif
/* !OBJECTSTORE_H */
#endif
/* !OBJECTSTORE_H */
This diff is collapsed.
Click to expand it.
TGenBase/TGenBaseLinkDef.h
+
0
−
1
View file @
1821c082
...
@@ -5,7 +5,6 @@
...
@@ -5,7 +5,6 @@
#pragma link off all functions;
#pragma link off all functions;
#pragma link off all namespaces;
#pragma link off all namespaces;
#pragma link C++ class TimeStamp+;
#pragma link C++ class TimeStamp+;
#pragma link C++ class TGenBase::Streamer+;
#pragma link C++ class TGenBase::Streamer+;
#pragma link C++ class TGenBase::Client+;
#pragma link C++ class TGenBase::Client+;
...
...
This diff is collapsed.
Click to expand it.
TGenBase/Utils.h
+
74
−
10
View file @
1821c082
...
@@ -10,6 +10,7 @@
...
@@ -10,6 +10,7 @@
#include
<cxxabi.h>
#include
<cxxabi.h>
#include
<typeinfo>
#include
<typeinfo>
#include
<fstream>
template
<
class
T
>
template
<
class
T
>
...
@@ -22,6 +23,10 @@ std::unique_ptr<T> copy_unique(const std::unique_ptr<T>& source)
...
@@ -22,6 +23,10 @@ std::unique_ptr<T> copy_unique(const std::unique_ptr<T>& source)
#define JSON_FOR(CLS) \
#define JSON_FOR(CLS) \
namespace jsoncons \
namespace jsoncons \
{ \
{ \
template <> \
struct is_json_type_traits_declared<CLS> : public std::true_type \
{}; \
\
template<class Json> \
template<class Json> \
struct json_type_traits<Json, CLS> \
struct json_type_traits<Json, CLS> \
{ \
{ \
...
@@ -74,29 +79,88 @@ static std::string ClassName() {
...
@@ -74,29 +79,88 @@ static std::string ClassName() {
return
result
.
substr
(
idx
+
2
,
result
.
size
()
-
idx
-
2
);
return
result
.
substr
(
idx
+
2
,
result
.
size
()
-
idx
-
2
);
}
}
template
<
class
T
>
template
<
class
T
>
static
inline
T
*
FromJson
(
jsoncons
::
ojson
json
)
{
static
std
::
vector
<
T
>
FromJson
(
jsoncons
::
ojson
json
)
{
if
(
!
json
.
is_object
())
return
nullptr
;
// if (!jsoncons::is_json_type_traits_declared<T>::value) {
return
new
T
(
json
.
as
<
T
>
());
// std::cout << "TGenBase::FromJson<T>: jsoncons type traits are not defined for your type <T>" << std::endl;
// return;
// }
if
(
json
.
is_object
())
return
{
json
.
as
<
T
>
()
};
if
(
json
.
is_array
())
return
json
.
as
<
std
::
vector
<
T
>>
();
std
::
cout
<<
"TGenBase::FromJson<T>: `json` is not an object or array"
<<
std
::
endl
;
return
{};
}
}
template
<
class
T
>
template
<
class
T
>
static
T
*
FromJsonString
(
std
::
string
jsonString
)
{
static
std
::
vector
<
T
>
FromJsonString
(
std
::
string
jsonString
)
{
if
(
!
jsonString
.
size
())
if
(
!
jsonString
.
size
())
return
nullptr
;
return
{}
;
try
{
try
{
return
FromJson
<
T
>
(
jsoncons
::
ojson
::
parse
(
jsonString
));
jsoncons
::
ojson
json
=
jsoncons
::
ojson
::
parse
(
jsonString
);
return
FromJson
<
T
>
(
json
);
}
}
catch
(
const
jsoncons
::
ser_error
&
e
)
{
catch
(
const
jsoncons
::
ser_error
&
e
)
{
std
::
cout
<<
"TGenBase::FromJsonString"
<<
std
::
endl
;
std
::
cout
<<
"TGenBase::FromJsonString
<T> parsing error
"
<<
std
::
endl
;
std
::
cout
<<
e
.
what
()
<<
std
::
endl
;
std
::
cout
<<
e
.
what
()
<<
std
::
endl
;
return
nullptr
;
return
{}
;
}
}
}
}
// namespace TGenBase
template
<
class
T
>
static
std
::
vector
<
T
>
FromJsonFile
(
std
::
string
fileName
)
{
std
::
ifstream
is
(
fileName
.
c_str
());
if
(
!
is
.
good
())
{
std
::
cout
<<
"TGenBase::FromJsonFile<T>: error reading from json file "
<<
fileName
<<
std
::
endl
;
return
{};
}
std
::
stringstream
contents
;
contents
<<
is
.
rdbuf
();
return
FromJsonString
<
T
>
(
contents
.
str
());
}
template
<
class
T
>
static
jsoncons
::
ojson
ToJson
(
T
value
)
{
// if (!jsoncons::is_json_type_traits_declared<T>::value) {
// std::cout << "TGenBase::ToJson<T>: jsoncons type traits are not defined for your type <T>" << std::endl;
// return;
// }
return
value
;
}
}
template
<
class
T
>
static
std
::
string
ToJsonString
(
T
value
,
bool
pretty
=
true
)
{
std
::
ostringstream
s
;
if
(
pretty
)
{
s
<<
jsoncons
::
pretty_print
(
ToJson
(
value
));
}
else
{
s
<<
ToJson
(
value
);
}
return
s
.
str
();
}
template
<
class
T
>
static
void
ToJsonFile
(
std
::
string
fileName
,
T
value
,
bool
pretty
=
true
)
{
std
::
ofstream
os
(
fileName
.
c_str
());
if
(
!
os
.
good
())
{
std
::
cout
<<
"TGenBase::ToJsonFile<T>: error writing to json file "
<<
fileName
<<
std
::
endl
;
return
;
}
if
(
pretty
)
{
os
<<
jsoncons
::
pretty_print
(
ToJson
(
value
));
}
else
{
os
<<
ToJson
(
value
);
}
}
}
// namespace TGenBase
#endif
/* !TGENBASEUTILS_H */
#endif
/* !TGENBASEUTILS_H */
This diff is collapsed.
Click to expand it.
external/InstallJsoncons.cmake
+
2
−
1
View file @
1821c082
set
(
JSONCONS_VERSION
1d81dc242a0ad29123c4fff4470d5d5e5d461b4a
)
# hash is tag 0.1
58.0
set
(
JSONCONS_VERSION
06d10fbca36b049fc7592cce0e80e91c0db4bce9
)
# hash is tag
v
0.1
63.3
set
(
JSONCONS_SRC_URL
"https://github.com/danielaparker/jsoncons.git"
)
set
(
JSONCONS_SRC_URL
"https://github.com/danielaparker/jsoncons.git"
)
set
(
JSONCONS_DESTDIR
"
${
CMAKE_BINARY_DIR
}
/external/JSONCONS-prefix"
)
set
(
JSONCONS_DESTDIR
"
${
CMAKE_BINARY_DIR
}
/external/JSONCONS-prefix"
)
set
(
JSONCONS_LIBNAME
"
${
CMAKE_SHARED_LIBRARY_PREFIX
}
jsoncons
${
CMAKE_SHARED_LIBRARY_SUFFIX
}
"
)
set
(
JSONCONS_LIBNAME
"
${
CMAKE_SHARED_LIBRARY_PREFIX
}
jsoncons
${
CMAKE_SHARED_LIBRARY_SUFFIX
}
"
)
...
@@ -28,6 +28,7 @@ ExternalProject_Add(JSONCONS
...
@@ -28,6 +28,7 @@ ExternalProject_Add(JSONCONS
-DCMAKE_CXX_FLAGS=
${
CMAKE_CXX_FLAGS
}
-DCMAKE_CXX_FLAGS=
${
CMAKE_CXX_FLAGS
}
-DCMAKE_INSTALL_PREFIX=
${
CMAKE_BINARY_DIR
}
-DCMAKE_INSTALL_PREFIX=
${
CMAKE_BINARY_DIR
}
-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON
-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON
-DJSONCONS_BUILD_TESTS:BOOL=OFF
INSTALL_COMMAND
${
CMAKE_COMMAND
}
--build . --target install
INSTALL_COMMAND
${
CMAKE_COMMAND
}
--build . --target install
)
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment