Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
json
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
json
Commits
a5188b08
Commit
a5188b08
authored
Feb 04, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
strings are escaped
parent
5ec43360
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
1 deletion
+80
-1
json.hpp
src/json.hpp
+67
-1
unit.cpp
test/unit.cpp
+13
-0
No files found.
src/json.hpp
View file @
a5188b08
...
@@ -1322,6 +1322,72 @@ class basic_json
...
@@ -1322,6 +1322,72 @@ class basic_json
}
}
/*!
/*!
Escape a string by replacing special characters by a sequence of an
escape character (backslash) and another character.
*/
static
string_t
escape_string
(
const
string_t
&
s
)
{
// create a result string of at least the size than s
string_t
result
;
result
.
reserve
(
s
.
size
());
for
(
auto
c
:
s
)
{
switch
(
c
)
{
// quotation mark
case
'"'
:
{
result
.
append
(
"
\\\"
"
,
2
);
break
;
}
// reverse solidus
case
'\\'
:
{
result
.
append
(
"
\\\\
"
,
2
);
break
;
}
// backspace
case
'\b'
:
{
result
.
append
(
"
\\
b"
,
2
);
break
;
}
// formfeed
case
'\f'
:
{
result
.
append
(
"
\\
f"
,
2
);
break
;
}
// newline
case
'\n'
:
{
result
.
append
(
"
\\
n"
,
2
);
break
;
}
// carriage return
case
'\r'
:
{
result
.
append
(
"
\\
r"
,
2
);
break
;
}
// horizontal tab
case
'\t'
:
{
result
.
append
(
"
\\
t"
,
2
);
break
;
}
default
:
{
result
.
append
(
1
,
c
);
}
}
}
return
result
;
}
/*!
Internal implementation of the serialization function.
Internal implementation of the serialization function.
@param prettyPrint whether the output shall be pretty-printed
@param prettyPrint whether the output shall be pretty-printed
...
@@ -1417,7 +1483,7 @@ class basic_json
...
@@ -1417,7 +1483,7 @@ class basic_json
case
(
value_t
:
:
string
)
:
case
(
value_t
:
:
string
)
:
{
{
return
string_t
(
"
\"
"
)
+
*
m_value
.
string
+
"
\"
"
;
return
string_t
(
"
\"
"
)
+
escape_string
(
*
m_value
.
string
)
+
"
\"
"
;
}
}
case
(
value_t
:
:
boolean
)
:
case
(
value_t
:
:
boolean
)
:
...
...
test/unit.cpp
View file @
a5188b08
...
@@ -11,6 +11,19 @@ using nlohmann::json;
...
@@ -11,6 +11,19 @@ using nlohmann::json;
TEST_CASE
()
TEST_CASE
()
{
{
CHECK
(
json
::
escape_string
(
"
\\
"
)
==
"
\\\\
"
);
CHECK
(
json
::
escape_string
(
"
\"
"
)
==
"
\\\"
"
);
CHECK
(
json
::
escape_string
(
"
\n
"
)
==
"
\\
n"
);
CHECK
(
json
::
escape_string
(
"
\r
"
)
==
"
\\
r"
);
CHECK
(
json
::
escape_string
(
"
\f
"
)
==
"
\\
f"
);
CHECK
(
json
::
escape_string
(
"
\b
"
)
==
"
\\
b"
);
CHECK
(
json
::
escape_string
(
"
\t
"
)
==
"
\\
t"
);
{
json
j
=
"AC/DC"
;
CHECK
(
j
.
dump
()
==
"
\"
AC/DC
\"
"
);
}
{
{
json
j
=
{
1
,
2
,
3
,
4
};
json
j
=
{
1
,
2
,
3
,
4
};
std
::
cerr
<<
j
<<
std
::
endl
;
std
::
cerr
<<
j
<<
std
::
endl
;
...
...
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