Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Simox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Florian Leander Singer
Simox
Commits
af7a2a9e
Commit
af7a2a9e
authored
5 years ago
by
Rainer Kartmann
Browse files
Options
Downloads
Patches
Plain Diff
Add read/write_json for i/ostream.
parent
13b9086b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
VirtualRobot/Util/json/io.cpp
+18
-4
18 additions, 4 deletions
VirtualRobot/Util/json/io.cpp
VirtualRobot/Util/json/io.h
+24
-8
24 additions, 8 deletions
VirtualRobot/Util/json/io.h
with
42 additions
and
12 deletions
VirtualRobot/Util/json/io.cpp
+
18
−
4
View file @
af7a2a9e
...
...
@@ -4,6 +4,7 @@
#include
<filesystem>
#include
<fstream>
namespace
fs
=
std
::
filesystem
;
...
...
@@ -28,9 +29,7 @@ namespace nlohmann
try
{
ifs
.
open
(
filename
);
json
j
;
ifs
>>
j
;
return
j
;
return
read_json
(
ifs
);
}
catch
(
const
std
::
ios_base
::
failure
&
e
)
{
...
...
@@ -42,6 +41,15 @@ namespace nlohmann
}
json
read_json
(
std
::
istream
&
is
)
{
json
j
;
is
>>
j
;
return
j
;
}
void
write_json
(
const
std
::
string
&
filename
,
const
json
&
j
,
const
int
indent
,
const
char
indent_char
)
{
...
...
@@ -52,7 +60,7 @@ namespace nlohmann
try
{
ofs
.
open
(
filename
);
ofs
<<
j
.
dump
(
indent
,
indent_char
);
write_json
(
ofs
,
j
,
indent
,
indent_char
);
}
catch
(
const
std
::
ios_base
::
failure
&
e
)
{
...
...
@@ -62,6 +70,12 @@ namespace nlohmann
}
}
void
write_json
(
std
::
ostream
&
os
,
const
json
&
j
,
const
int
indent
,
const
char
indent_char
)
{
os
<<
j
.
dump
(
indent
,
indent_char
);
}
}
This diff is collapsed.
Click to expand it.
VirtualRobot/Util/json/io.h
+
24
−
8
View file @
af7a2a9e
...
...
@@ -8,27 +8,43 @@ namespace nlohmann
/**
* @brief Read a JSON document from the given file.
*
*
* @param filename The name of the file to read from.
* @return The JSON document.
*
*
* @throw std::ios_base::failure If IO access fails.
*/
json
read_json
(
const
std
::
string
&
filename
);
/**
* @brief Write a JSON document to the given file.
*
* @param j The JSON document.
* @brief Read a JSON document from the given in-stream.
* @param is The in-stream.
* @return The JSON document.
*/
json
read_json
(
std
::
istream
&
is
);
/**
* @brief Write a JSON document to the given file.
*
* @param filename The name of the file to write to.
* @param j The JSON document.
* @param indent See nlohmann::json::dump().
* @param indent_char See nlohmann::json::dump().
*
*
* @throw std::ios_base::failure If IO access fails.
*/
void
write_json
(
const
std
::
string
&
filename
,
const
json
&
j
,
const
int
indent
=
-
1
,
const
char
indent_char
=
' '
);
/**
* @brief Write a JSON document to the given out-stream.
* @param os The out-stream.
* @param j The JSON document.
* @param indent See nlohmann::json::dump().
* @param indent_char See nlohmann::json::dump().
*/
void
write_json
(
std
::
ostream
&
os
,
const
json
&
j
,
const
int
indent
=
-
1
,
const
char
indent_char
=
' '
);
}
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