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
13b9086b
Commit
13b9086b
authored
5 years ago
by
Rainer Kartmann
Browse files
Options
Downloads
Patches
Plain Diff
Add more useful exception messages in read_json/write_json
parent
70dc1907
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
VirtualRobot/Util/json/io.cpp
+41
-9
41 additions, 9 deletions
VirtualRobot/Util/json/io.cpp
with
41 additions
and
9 deletions
VirtualRobot/Util/json/io.cpp
+
41
−
9
View file @
13b9086b
#include
"io.h"
#include
<iostream>
#include
<filesystem>
#include
<fstream>
namespace
fs
=
std
::
filesystem
;
namespace
nlohmann
{
static
void
checkExists
(
const
std
::
string
&
filename
,
const
std
::
string
&
prefix
=
""
)
{
if
(
!
fs
::
exists
(
filename
))
{
throw
std
::
ios_base
::
failure
(
prefix
+
"File
\"
"
+
filename
+
"
\"
does not exist."
);
}
}
json
read_json
(
const
std
::
string
&
filename
)
{
std
::
ifstream
ifs
;
// Allow throwing std::ios_base::failure.
ifs
.
exceptions
(
std
::
ifstream
::
failbit
|
std
::
ifstream
::
badbit
);
ifs
.
open
(
filename
);
json
j
;
ifs
>>
j
;
return
j
;
try
{
ifs
.
open
(
filename
);
json
j
;
ifs
>>
j
;
return
j
;
}
catch
(
const
std
::
ios_base
::
failure
&
e
)
{
// Add a more useful message.
const
std
::
string
msg
=
"Failed to read file
\"
"
+
filename
+
"
\"
: "
;
checkExists
(
filename
,
msg
);
throw
std
::
ios_base
::
failure
(
msg
+
std
::
string
(
e
.
what
()));
}
}
void
write_json
(
const
std
::
string
&
filename
,
const
json
&
j
,
const
int
indent
,
const
char
indent_char
)
{
std
::
ofstream
ofs
;
// Allow throwing std::ios_base::failure.
ofs
.
exceptions
(
std
::
ifstream
::
failbit
|
std
::
ifstream
::
badbit
);
ofs
.
open
(
filename
);
ofs
<<
j
.
dump
(
indent
,
indent_char
);
try
{
ofs
.
open
(
filename
);
ofs
<<
j
.
dump
(
indent
,
indent_char
);
}
catch
(
const
std
::
ios_base
::
failure
&
e
)
{
// Add a more useful message.
const
std
::
string
msg
=
"Failed to write file
\"
"
+
filename
+
"
\"
: "
;
throw
std
::
ios_base
::
failure
(
msg
+
std
::
string
(
e
.
what
()));
}
}
}
...
...
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