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
764ed26c
Commit
764ed26c
authored
Jan 03, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cleanup
parent
18364aac
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
35 deletions
+41
-35
JSON.cc
src/JSON.cc
+39
-33
JSON.h
src/JSON.h
+2
-2
No files found.
src/JSON.cc
View file @
764ed26c
...
@@ -242,6 +242,7 @@ JSON::JSON(JSON&& o) noexcept
...
@@ -242,6 +242,7 @@ JSON::JSON(JSON&& o) noexcept
:
_type
(
std
::
move
(
o
.
_type
)),
_value
(
std
::
move
(
o
.
_value
))
:
_type
(
std
::
move
(
o
.
_type
)),
_value
(
std
::
move
(
o
.
_value
))
{
{
// invalidate payload
// invalidate payload
o
.
_type
=
value_type
::
null
;
o
.
_value
=
{};
o
.
_value
=
{};
}
}
...
@@ -1806,13 +1807,14 @@ JSON JSON::Parser::parse()
...
@@ -1806,13 +1807,14 @@ JSON JSON::Parser::parse()
do
do
{
{
// key
// key
const
auto
key
=
parseString
();
auto
key
=
parseString
();
// colon
// colon
expect
(
':'
);
expect
(
':'
);
// value
// value
result
[
std
::
move
(
key
)]
=
parse
();
result
[
std
::
move
(
key
)]
=
parse
();
key
.
clear
();
}
}
while
(
_current
==
','
and
next
());
while
(
_current
==
','
and
next
());
}
}
...
@@ -1870,49 +1872,53 @@ JSON JSON::Parser::parse()
...
@@ -1870,49 +1872,53 @@ JSON JSON::Parser::parse()
return
JSON
();
return
JSON
();
}
}
default
:
case
(
'-'
):
case
(
'0'
):
case
(
'1'
):
case
(
'2'
):
case
(
'3'
):
case
(
'4'
):
case
(
'5'
):
case
(
'6'
):
case
(
'7'
):
case
(
'8'
):
case
(
'9'
):
{
{
if
(
std
::
isdigit
(
_current
)
||
_current
==
'-'
)
// remember position of number's first character
const
auto
_first_pos
=
_pos
-
1
;
while
(
next
()
and
(
std
::
isdigit
(
_current
)
||
_current
==
'.'
||
_current
==
'e'
||
_current
==
'E'
||
_current
==
'+'
||
_current
==
'-'
));
try
{
{
// collect number in tmp string
const
auto
float_val
=
std
::
stod
(
_buffer
.
substr
(
_first_pos
,
_pos
-
_first_pos
));
std
::
string
tmp
;
const
auto
int_val
=
static_cast
<
int
>
(
float_val
);
do
{
tmp
+=
_current
;
}
while
(
next
()
and
(
std
::
isdigit
(
_current
)
||
_current
==
'.'
||
_current
==
'e'
||
_current
==
'E'
||
_current
==
'+'
||
_current
==
'-'
));
try
// check if conversion loses precision
if
(
float_val
==
int_val
)
{
{
const
auto
float_val
=
std
::
stod
(
tmp
);
// we would not lose precision -> int
const
auto
int_val
=
static_cast
<
int
>
(
float_val
);
return
JSON
(
int_val
);
// check if conversion loses precision
if
(
float_val
==
int_val
)
{
// we would not lose precision -> int
return
JSON
(
int_val
);
}
else
{
// we would lose precision -> float
return
JSON
(
float_val
);
}
}
}
catch
(...)
else
{
{
error
(
"error while translating "
+
tmp
+
" to number"
);
// we would lose precision -> float
return
JSON
(
float_val
);
}
}
break
;
}
}
else
catch
(...)
{
{
error
(
"unexpected character"
);
error
(
"error translating "
+
_buffer
.
substr
(
_first_pos
,
_pos
-
_first_pos
)
+
" to number"
);
}
}
}
}
default
:
{
error
(
"unexpected character"
);
}
}
}
}
}
...
...
src/JSON.h
View file @
764ed26c
...
@@ -415,7 +415,7 @@ class JSON
...
@@ -415,7 +415,7 @@ class JSON
/// raise an exception with an error message
/// raise an exception with an error message
inline
void
error
(
const
std
::
string
&
)
__attribute__
((
noreturn
));
inline
void
error
(
const
std
::
string
&
)
__attribute__
((
noreturn
));
/// parse a quoted string
/// parse a quoted string
std
::
string
parseString
();
inline
std
::
string
parseString
();
/// parse a Boolean "true"
/// parse a Boolean "true"
inline
void
parseTrue
();
inline
void
parseTrue
();
/// parse a Boolean "false"
/// parse a Boolean "false"
...
@@ -423,7 +423,7 @@ class JSON
...
@@ -423,7 +423,7 @@ class JSON
/// parse a null object
/// parse a null object
inline
void
parseNull
();
inline
void
parseNull
();
/// a helper function to expect a certain character
/// a helper function to expect a certain character
void
expect
(
const
char
);
inline
void
expect
(
const
char
);
private
:
private
:
/// a buffer of the input
/// a buffer of the input
...
...
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