Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
7b97ec88
Commit
7b97ec88
authored
Jan 10, 2015
by
Raphael Isemann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed dumping of strings
parent
0cd2ecf4
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
2 deletions
+59
-2
src/json.cc
src/json.cc
+44
-1
src/json.h
src/json.h
+4
-1
test/json_unit.cc
test/json_unit.cc
+11
-0
No files found.
src/json.cc
View file @
7b97ec88
...
...
@@ -492,7 +492,7 @@ std::string json::dump(const bool prettyPrint, const unsigned int indentStep,
{
case
(
value_type
:
:
string
)
:
{
return
std
::
string
(
"
\"
"
)
+
*
value_
.
string
+
"
\"
"
;
return
std
::
string
(
"
\"
"
)
+
escapeString
(
*
value_
.
string
)
+
"
\"
"
;
}
case
(
value_type
:
:
boolean
)
:
...
...
@@ -589,6 +589,49 @@ std::string json::dump(const bool prettyPrint, const unsigned int indentStep,
}
}
/*!
Internal function to replace all occurrences of a character in a given string
with another string.
\param str the string that contains tokens to replace
\param c the character that needs to be replaced
\param replacement the string that is the replacement for the character
*/
void
json
::
replaceChar
(
std
::
string
&
str
,
char
c
,
const
std
::
string
&
replacement
)
const
{
size_t
start_pos
=
0
;
while
((
start_pos
=
str
.
find
(
c
,
start_pos
))
!=
std
::
string
::
npos
)
{
str
.
replace
(
start_pos
,
1
,
replacement
);
start_pos
+=
replacement
.
length
();
}
}
/*!
Escapes all special characters in the given string according to ECMA-404.
Necessary as some characters such as quotes, backslashes and so on
can't be used as is when dumping a string value.
\param str the string that should be escaped.
\return a copy of the given string with all special characters escaped.
*/
std
::
string
json
::
escapeString
(
const
std
::
string
&
str
)
const
{
std
::
string
result
(
str
);
// we first need to escape the backslashes as all other methods will insert
// legitimate backslashes into the result.
replaceChar
(
result
,
'\\'
,
"
\\\\
"
);
// replace all other characters
replaceChar
(
result
,
'"'
,
"
\\\"
"
);
replaceChar
(
result
,
'\n'
,
"
\\
n"
);
replaceChar
(
result
,
'\r'
,
"
\\
r"
);
replaceChar
(
result
,
'\f'
,
"
\\
f"
);
replaceChar
(
result
,
'\b'
,
"
\\
b"
);
replaceChar
(
result
,
'\t'
,
"
\\
t"
);
return
result
;
}
/*!
Serialization function for JSON objects. The function tries to mimick Python's
\p json.dumps() function, and currently supports its \p indent parameter.
...
...
src/json.h
View file @
7b97ec88
...
...
@@ -165,7 +165,10 @@ class json
/// dump the object (with pretty printer)
std
::
string
dump
(
const
bool
,
const
unsigned
int
,
unsigned
int
=
0
)
const
noexcept
;
/// replaced a character in a string with another string
void
replaceChar
(
std
::
string
&
str
,
char
c
,
const
std
::
string
&
replacement
)
const
;
/// escapes special characters to safely dump the string
std
::
string
escapeString
(
const
std
::
string
&
)
const
;
public:
/// explicit value conversion
template
<
typename
T
>
...
...
test/json_unit.cc
View file @
7b97ec88
...
...
@@ -819,6 +819,17 @@ TEST_CASE("string")
j1
.
clear
();
CHECK
(
j1
.
get
<
std
::
string
>
()
==
""
);
}
SECTION
(
"Dumping"
)
{
CHECK
(
json
(
"
\"
"
).
dump
(
0
)
==
"
\"\\\"\"
"
);
CHECK
(
json
(
"
\\
"
).
dump
(
0
)
==
"
\"\\\\\"
"
);
CHECK
(
json
(
"
\n
"
).
dump
(
0
)
==
"
\"\\
n
\"
"
);
CHECK
(
json
(
"
\t
"
).
dump
(
0
)
==
"
\"\\
t
\"
"
);
CHECK
(
json
(
"
\b
"
).
dump
(
0
)
==
"
\"\\
b
\"
"
);
CHECK
(
json
(
"
\f
"
).
dump
(
0
)
==
"
\"\\
f
\"
"
);
CHECK
(
json
(
"
\r
"
).
dump
(
0
)
==
"
\"\\
r
\"
"
);
}
}
TEST_CASE
(
"boolean"
)
...
...
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