FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cppjson/cppjson/toString.cpp at main · StephenChips/cppjson · GitHub
StephenChips
/
cppjson
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
6
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cppjson
/
cppjson
/
toString.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
120 lines (111 loc) · 3.21 KB
Breadcrumbs
cppjson
/
cppjson
/
toString.cpp
Copy path
File metadata and controls
120 lines (111 loc) · 3.21 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#
include
"
cppjson.hpp
"
#
include
<
sstream
>
#
include
<
iomanip
>
#
include
<
cmath
>
#
include
<
ios
>
std::string
toString
(
const
JSON
&json);
std::string
escapeCharacters
(
const
std::string &str);
std::string
toStringArray
(
const
JSON
&json);
std::string
toString
(
const
JSON
&json)
{
switch
(json.
type
())
{
case
JSON
::Bool:
return
json.
getBool
() ?
"
true
"
:
"
false
"
;
case
JSON
::String:
return
escapeCharacters
(json.
getString
());
case
JSON
::Null:
return
"
null
"
;
case
JSON
::Number:
{
double
val = json.
getNumber
();
if
(
std::isnan
(val) ||
std::isinf
(val))
{
return
"
null
"
;
}
else
{
std::ostringstream oss;
oss << val;
return
oss.
str
();
}
}
case
JSON
::Array:
{
auto
s =
std::string
(
"
[
"
);
auto
&array = json.
getArray
();
for
(
auto
it = array.
begin
(); it != array.
end
(); it++)
{
s +=
toString
(*it);
if
(it != array.
end
() -
1
)
s +=
'
,
'
;
}
s +=
'
]
'
;
return
s;
}
case
JSON
::Object:
{
auto
s =
std::string
(
"
{
"
);
auto
&object = json.
getObject
();
for
(
auto
it = object.
begin
(); it != object.
end
(); it++)
{
s +=
'
"
'
;
s +=
escapeCharacters
(it->
first
);
s +=
"
\"
:
\"
"
;
s+=
toString
(it->
second
);
s +=
'
"
'
;
if
(it !=
std::prev
(object.
end
()))
s +=
'
,
'
;
}
s +=
'
}
'
;
return
s;
}
default
:
return
"
"
;
}
}
std::string
escapeCharacters
(
const
std::string &str)
{
std::ostringstream oss;
for
(
auto
it = str.
cbegin
(); it != str.
cend
(); it++)
{
switch
(*it)
{
case
'
\n
'
:
oss <<
"
\\
n
"
;
break
;
case
'
\r
'
:
oss <<
"
\\
r
"
;
break
;
case
'
\f
'
:
oss <<
"
\\
f
"
;
break
;
case
'
\t
'
:
oss <<
"
\\
t
"
;
break
;
case
'
\b
'
:
oss <<
"
\\
b
"
;
break
;
default
:
//
The char type's signedness depends on the compiler and the platform, in ARM and PowerPC it is unsigned
//
but it is signed in x86 and x64. We must cast it to unsigned char before comparing, otherwise if the
//
value is larger than 127, the result will probably be wrong (if it is a signed char).
//
//
Here the value is implicitly casted to unsigned since 0x20u is a unsigned integer literal, according to
//
the specification, it one operand is unsigned, the other should be casted to unsigned too, before doing
//
the comparison.
//
//
0x20u is the smallest representable Unicode value, which is a space. Characters smaller than it are control
//
characters and should be escaped, this is browser's function `JSON.stringify(...)`;'s behaviour.
if
(*it <
0x20u
)
{
oss <<
"
\\
u
"
<<
std::setfill
(
'
0
'
) <<
std::setw
(
4
) << std::hex <<
static_cast
<
unsigned
long
>(*it);
}
else
{
oss << *it;
}
}
}
return
oss.
str
();
}
Back
|
FazBrowse Home
|
New Git URL