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
5d280143
Commit
5d280143
authored
Feb 11, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixes
parent
8a4e127a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
266 additions
and
217 deletions
+266
-217
json.hpp
src/json.hpp
+228
-176
json.hpp.re2c
src/json.hpp.re2c
+32
-19
unit.cpp
test/unit.cpp
+6
-22
No files found.
src/json.hpp
View file @
5d280143
...
...
@@ -2415,15 +2415,27 @@ class basic_json
inline
lexer
()
=
default
;
#define YYMAXFILL 5
/*!
This function implements a scanner for JSON. It is specified using
regular expressions that try to follow RFC 7159 and ECMA-404 as close
as possible. These regular expressions are then translated into a
deterministic finite automaton (DFA) by the tool RE2C. As a result, the
translated code for this function consists of a large block of code
with goto jumps.
@return the class of the next token read from the buffer
@todo Unicode support needs to be checked.
*/
inline
token_type
scan
()
{
#define YYFILL(n)
m_start
=
m_cursor
;
{
char
yych
;
static
const
unsigned
char
yybm
[]
=
{
static
const
unsigned
char
yybm
[]
=
{
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
128
,
...
...
@@ -2467,24 +2479,24 @@ class basic_json
{
if
(
yych
<=
0x00
)
{
goto
yy
25
;
goto
json_parser_
25
;
}
if
(
yych
>=
'"'
)
{
goto
yy
23
;
goto
json_parser_
23
;
}
}
else
{
if
(
yych
<=
'+'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
if
(
yych
<=
','
)
{
goto
yy
11
;
goto
json_parser_
11
;
}
goto
yy
18
;
goto
json_parser_
18
;
}
}
else
...
...
@@ -2493,189 +2505,210 @@ class basic_json
{
if
(
yych
<=
'/'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
if
(
yych
<=
'0'
)
{
goto
yy
19
;
goto
json_parser_
19
;
}
goto
yy
21
;
goto
json_parser_
21
;
}
else
{
if
(
yych
<=
':'
)
{
goto
yy
13
;
goto
json_parser_
13
;
}
if
(
yych
>=
'['
)
{
goto
yy
3
;
goto
json_parser_
3
;
}
}
}
}
else
{
else
{
if
(
yych
<=
'n'
)
{
if
(
yych
<=
'e'
)
{
if
(
yych
==
']'
)
{
goto
yy
5
;
goto
json_parser_
5
;
}
}
else
{
if
(
yych
<=
'f'
)
{
goto
yy
17
;
goto
json_parser_
17
;
}
if
(
yych
>=
'n'
)
{
goto
yy
15
;
goto
json_parser_
15
;
}
}
}
else
{
else
{
if
(
yych
<=
'z'
)
{
if
(
yych
==
't'
)
{
goto
yy
16
;
goto
json_parser_
16
;
}
}
else
{
else
{
if
(
yych
<=
'{'
)
{
goto
yy
7
;
goto
json_parser_
7
;
}
if
(
yych
==
'}'
)
{
goto
yy
9
;
goto
json_parser_
9
;
}
}
}
}
yy
2:
json_parser_
2:
m_cursor
=
m_marker
;
goto
yy
20
;
yy
3:
goto
json_parser_
20
;
json_parser_
3:
++
m_cursor
;
{
return
token_type
::
begin_array
;
}
yy5:
{
return
token_type
::
begin_array
;
}
json_parser_5:
++
m_cursor
;
{
return
token_type
::
end_array
;
}
yy7:
{
return
token_type
::
end_array
;
}
json_parser_7:
++
m_cursor
;
{
return
token_type
::
begin_object
;
}
yy9:
{
return
token_type
::
begin_object
;
}
json_parser_9:
++
m_cursor
;
{
return
token_type
::
end_object
;
}
yy11:
{
return
token_type
::
end_object
;
}
json_parser_11:
++
m_cursor
;
{
return
token_type
::
value_separator
;
}
yy13:
{
return
token_type
::
value_separator
;
}
json_parser_13:
++
m_cursor
;
{
return
token_type
::
name_separator
;
}
yy15:
{
return
token_type
::
name_separator
;
}
json_parser_15:
yych
=
*++
m_cursor
;
if
(
yych
==
'u'
)
{
goto
yy
50
;
goto
json_parser_
50
;
}
goto
yy
2
;
yy
16:
goto
json_parser_
2
;
json_parser_
16:
yych
=
*++
m_cursor
;
if
(
yych
==
'r'
)
{
goto
yy
46
;
goto
json_parser_
46
;
}
goto
yy
2
;
yy
17:
goto
json_parser_
2
;
json_parser_
17:
yych
=
*++
m_cursor
;
if
(
yych
==
'a'
)
{
goto
yy
41
;
goto
json_parser_
41
;
}
goto
yy
2
;
yy
18:
goto
json_parser_
2
;
json_parser_
18:
yych
=
*++
m_cursor
;
if
(
yych
<=
'/'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
if
(
yych
<=
'0'
)
{
goto
yy
19
;
goto
json_parser_
19
;
}
if
(
yych
<=
'9'
)
{
goto
yy
21
;
goto
json_parser_
21
;
}
goto
yy
2
;
yy
19:
goto
json_parser_
2
;
json_parser_
19:
yych
=
*
(
m_marker
=
++
m_cursor
);
if
(
yych
<=
'D'
)
{
if
(
yych
==
'.'
)
{
goto
yy
34
;
goto
json_parser_
34
;
}
}
else
{
else
{
if
(
yych
<=
'E'
)
{
goto
yy
35
;
goto
json_parser_
35
;
}
if
(
yych
==
'e'
)
{
goto
yy
35
;
goto
json_parser_
35
;
}
}
yy20:
{
return
token_type
::
value_number
;
}
yy21:
json_parser_20:
{
return
token_type
::
value_number
;
}
json_parser_21:
m_marker
=
++
m_cursor
;
yych
=
*
m_cursor
;
if
(
yybm
[
0
+
yych
]
&
64
)
{
goto
yy
21
;
goto
json_parser_
21
;
}
if
(
yych
<=
'D'
)
{
if
(
yych
==
'.'
)
{
goto
yy
34
;
goto
json_parser_
34
;
}
goto
yy
20
;
goto
json_parser_
20
;
}
else
{
else
{
if
(
yych
<=
'E'
)
{
goto
yy
35
;
goto
json_parser_
35
;
}
if
(
yych
==
'e'
)
{
goto
yy
35
;
goto
json_parser_
35
;
}
goto
yy
20
;
goto
json_parser_
20
;
}
yy
23:
json_parser_
23:
++
m_cursor
;
yych
=
*
m_cursor
;
if
(
yybm
[
0
+
yych
]
&
128
)
{
goto
yy
23
;
goto
json_parser_
23
;
}
if
(
yych
<=
'"'
)
{
goto
yy
28
;
goto
json_parser_
28
;
}
goto
yy
27
;
yy
25:
goto
json_parser_
27
;
json_parser_
25:
++
m_cursor
;
{
return
token_type
::
end_of_input
;
}
yy27:
{
return
token_type
::
end_of_input
;
}
json_parser_27:
++
m_cursor
;
yych
=
*
m_cursor
;
if
(
yych
<=
'e'
)
...
...
@@ -2684,13 +2717,13 @@ yy27:
{
if
(
yych
==
'"'
)
{
goto
yy
23
;
goto
json_parser_
23
;
}
if
(
yych
<=
'.'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
goto
yy
23
;
goto
json_parser_
23
;
}
else
{
...
...
@@ -2698,299 +2731,316 @@ yy27:
{
if
(
yych
<=
'['
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
goto
yy
23
;
goto
json_parser_
23
;
}
else
{
if
(
yych
==
'b'
)
{
goto
yy
23
;
goto
json_parser_
23
;
}
goto
yy
2
;
goto
json_parser_
2
;
}
}
}
else
{
else
{
if
(
yych
<=
'q'
)
{
if
(
yych
<=
'f'
)
{
goto
yy
23
;
goto
json_parser_
23
;
}
if
(
yych
==
'n'
)
{
goto
yy
23
;
goto
json_parser_
23
;
}
goto
yy
2
;
goto
json_parser_
2
;
}
else
{
else
{
if
(
yych
<=
's'
)
{
if
(
yych
<=
'r'
)
{
goto
yy
23
;
goto
json_parser_
23
;
}
goto
yy
2
;
goto
json_parser_
2
;
}
else
{
else
{
if
(
yych
<=
't'
)
{
goto
yy
23
;
goto
json_parser_
23
;
}
if
(
yych
<=
'u'
)
{
goto
yy
30
;
goto
json_parser_
30
;
}
goto
yy
2
;
goto
json_parser_
2
;
}
}
}
yy
28:
json_parser_
28:
++
m_cursor
;
{
return
token_type
::
value_string
;
}
yy30:
{
return
token_type
::
value_string
;
}
json_parser_30:
++
m_cursor
;
yych
=
*
m_cursor
;
if
(
yych
<=
'@'
)
{
if
(
yych
<=
'/'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
if
(
yych
>=
':'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
}
else
{
else
{
if
(
yych
<=
'F'
)
{
goto
yy
31
;
goto
json_parser_
31
;
}
if
(
yych
<=
'`'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
if
(
yych
>=
'g'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
}
yy
31:
json_parser_
31:
++
m_cursor
;
yych
=
*
m_cursor
;
if
(
yych
<=
'@'
)
{
if
(
yych
<=
'/'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
if
(
yych
>=
':'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
}
else
{
else
{
if
(
yych
<=
'F'
)
{
goto
yy
32
;
goto
json_parser_
32
;
}
if
(
yych
<=
'`'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
if
(
yych
>=
'g'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
}
yy
32:
json_parser_
32:
++
m_cursor
;
yych
=
*
m_cursor
;
if
(
yych
<=
'@'
)
{
if
(
yych
<=
'/'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
if
(
yych
>=
':'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
}
else
{
else
{
if
(
yych
<=
'F'
)
{
goto
yy
33
;
goto
json_parser_
33
;
}
if
(
yych
<=
'`'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
if
(
yych
>=
'g'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
}
yy
33:
json_parser_
33:
++
m_cursor
;
yych
=
*
m_cursor
;
if
(
yych
<=
'@'
)
{
if
(
yych
<=
'/'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
if
(
yych
<=
'9'
)
{
goto
yy
23
;
goto
json_parser_
23
;
}
goto
yy
2
;
goto
json_parser_
2
;
}
else
{
else
{
if
(
yych
<=
'F'
)
{
goto
yy
23
;
goto
json_parser_
23
;
}
if
(
yych
<=
'`'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
if
(
yych
<=
'f'
)
{
goto
yy
23
;
goto
json_parser_
23
;
}
goto
yy
2
;
goto
json_parser_
2
;
}
yy
34:
json_parser_
34:
yych
=
*++
m_cursor
;
if
(
yych
<=
'/'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
if
(
yych
<=
'9'
)
{
goto
yy
39
;
goto
json_parser_
39
;
}
goto
yy
2
;
yy
35:
goto
json_parser_
2
;
json_parser_
35:
yych
=
*++
m_cursor
;
if
(
yych
<=
','
)
{
if
(
yych
!=
'+'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
}
else
{
else
{
if
(
yych
<=
'-'
)
{
goto
yy
36
;
goto
json_parser_
36
;
}
if
(
yych
<=
'/'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
if
(
yych
<=
'9'
)
{
goto
yy
37
;
goto
json_parser_
37
;
}
goto
yy
2
;
goto
json_parser_
2
;
}
yy
36:
json_parser_
36:
yych
=
*++
m_cursor
;
if
(
yych
<=
'/'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
if
(
yych
>=
':'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
yy
37:
json_parser_
37:
++
m_cursor
;
yych
=
*
m_cursor
;
if
(
yych
<=
'/'
)
{
goto
yy
20
;
goto
json_parser_
20
;
}
if
(
yych
<=
'9'
)
{
goto
yy
37
;
goto
json_parser_
37
;
}
goto
yy
20
;
yy
39:
goto
json_parser_
20
;
json_parser_
39:
m_marker
=
++
m_cursor
;
yych
=
*
m_cursor
;
if
(
yych
<=
'D'
)
{
if
(
yych
<=
'/'
)
{
goto
yy
20
;
goto
json_parser_
20
;
}
if
(
yych
<=
'9'
)
{
goto
yy
39
;
goto
json_parser_
39
;
}
goto
yy
20
;
goto
json_parser_
20
;
}
else
{
else
{
if
(
yych
<=
'E'
)
{
goto
yy
35
;
goto
json_parser_
35
;
}
if
(
yych
==
'e'
)
{
goto
yy
35
;
goto
json_parser_
35
;
}
goto
yy
20
;
goto
json_parser_
20
;
}
yy
41:
json_parser_
41:
yych
=
*++
m_cursor
;
if
(
yych
!=
'l'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
yych
=
*++
m_cursor
;
if
(
yych
!=
's'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
yych
=
*++
m_cursor
;
if
(
yych
!=
'e'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
++
m_cursor
;
{
return
token_type
::
literal_false
;
}
yy46:
{
return
token_type
::
literal_false
;
}
json_parser_46:
yych
=
*++
m_cursor
;
if
(
yych
!=
'u'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
yych
=
*++
m_cursor
;
if
(
yych
!=
'e'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
++
m_cursor
;
{
return
token_type
::
literal_true
;
}
yy50:
{
return
token_type
::
literal_true
;
}
json_parser_50:
yych
=
*++
m_cursor
;
if
(
yych
!=
'l'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
yych
=
*++
m_cursor
;
if
(
yych
!=
'l'
)
{
goto
yy
2
;
goto
json_parser_
2
;
}
++
m_cursor
;
{
return
token_type
::
literal_null
;
}
{
return
token_type
::
literal_null
;
}
}
}
...
...
@@ -3001,11 +3051,11 @@ yy50:
}
/*!
The pointer m_
begin
points to the opening quote of the string, and
m_cursor past the closing quote of the string. We create a std::string
from
the character after the opening quotes (m_begin+1) until the character
before the closing quotes (hence subtracting 2 characters from the pointer
difference of the two pointers).
The pointer m_
start
points to the opening quote of the string, and
m_cursor past the closing quote of the string. We create a std::string
from the character after the opening quotes (m_begin+1) until the
character before the closing quotes (hence subtracting 2 characters
from the pointer
difference of the two pointers).
@return string value of current token without opening and closing quotes
...
...
@@ -3018,14 +3068,13 @@ yy50:
inline
number_float_t
get_number
()
const
{
// The pointer m_begin points to the beginning of the
// parsed number. We pass this pointer to std::strtod which
// sets endptr to the first character past the converted
// number. If this pointer is not the same as m_cursor,
// then either more or less characters have been used
// during the comparison. This can happen for inputs like
// "01" which will be treated like number 0 followed by
// number 1.
// The pointer m_begin points to the beginning of the parsed
// number. We pass this pointer to std::strtod which sets endptr to
// the first character past the converted number. If this pointer is
// not the same as m_cursor, then either more or less characters
// have been used during the comparison. This can happen for inputs
// like "01" which will be treated like number 0 followed by number
// 1.
// conversion
char
*
endptr
;
...
...
@@ -3044,13 +3093,16 @@ yy50:
}
private
:
/// the buffer
const
char
*
m_content
=
nullptr
;
/// pointer to he beginning of the current symbol
const
char
*
m_start
=
nullptr
;
/// pointer to the current symbol
const
char
*
m_cursor
=
nullptr
;
/// pointer to the end of the buffer
const
char
*
m_limit
=
nullptr
;
/// pointer for backtracking information
const
char
*
m_marker
=
nullptr
;
const
char
*
m_ctxmarker
=
nullptr
;
};
class
parser
...
...
src/json.hpp.re2c
View file @
5d280143
...
...
@@ -2415,19 +2415,30 @@ class basic_json
inline lexer() = default;
/*!max:re2c */
/*!
This function implements a scanner for JSON. It is specified using
regular expressions that try to follow RFC 7159 and ECMA-404 as close
as possible. These regular expressions are then translated into a
deterministic finite automaton (DFA) by the tool RE2C. As a result, the
translated code for this function consists of a large block of code
with goto jumps.
@return the class of the next token read from the buffer
@todo Unicode support needs to be checked.
*/
inline token_type scan()
{
#define YYFILL(n)
m_start = m_cursor;
/*!re2c
re2c:define:YYCURSOR = m_cursor;
re2c:define:YYLIMIT = m_limit;
re2c:define:YYCTYPE = char;
re2c:define:YYCTXMARKER = m_ctxmarker;
re2c:define:YYMARKER = m_marker;
re2c:indent:top = 1;
re2c:yyfill:enable = 0;
re2c:labelprefix = "json_parser_";
// structural characters
"[" { return token_type::begin_array; }
...
...
@@ -2466,7 +2477,7 @@ class basic_json
string { return token_type::value_string; }
// end of file
'\000'
{ return token_type::end_of_input; }
'\000'
{ return token_type::end_of_input; }
*/
}
...
...
@@ -2476,11 +2487,11 @@ class basic_json
}
/*!
The pointer m_
begin
points to the opening quote of the string, and
m_cursor past the closing quote of the string. We create a std::string
from
the character after the opening quotes (m_begin+1) until the character
before the closing quotes (hence subtracting 2 characters from the pointer
difference of the two pointers).
The pointer m_
start
points to the opening quote of the string, and
m_cursor past the closing quote of the string. We create a std::string
from the character after the opening quotes (m_begin+1) until the
character before the closing quotes (hence subtracting 2 characters
from the pointer
difference of the two pointers).
@return string value of current token without opening and closing quotes
...
...
@@ -2493,14 +2504,13 @@ class basic_json
inline number_float_t get_number() const
{
// The pointer m_begin points to the beginning of the
// parsed number. We pass this pointer to std::strtod which
// sets endptr to the first character past the converted
// number. If this pointer is not the same as m_cursor,
// then either more or less characters have been used
// during the comparison. This can happen for inputs like
// "01" which will be treated like number 0 followed by
// number 1.
// The pointer m_begin points to the beginning of the parsed
// number. We pass this pointer to std::strtod which sets endptr to
// the first character past the converted number. If this pointer is
// not the same as m_cursor, then either more or less characters
// have been used during the comparison. This can happen for inputs
// like "01" which will be treated like number 0 followed by number
// 1.
// conversion
char* endptr;
...
...
@@ -2519,13 +2529,16 @@ class basic_json
}
private:
/// the buffer
const char* m_content = nullptr;
/// pointer to he beginning of the current symbol
const char* m_start = nullptr;
/// pointer to the current symbol
const char* m_cursor = nullptr;
/// pointer to the end of the buffer
const char* m_limit = nullptr;
/// pointer for backtracking information
const char* m_marker = nullptr;
const char* m_ctxmarker = nullptr;
};
class parser
...
...
test/unit.cpp
View file @
5d280143
...
...
@@ -3892,43 +3892,27 @@ TEST_CASE("deserialization")
{
SECTION
(
"string"
)
{
// auto s = "[\"foo\",1,2,3,false,{\"one\":1}]";
// json j = json::parse(s);
// CHECK(j == json({"foo", 1, 2, 3, false, {{"one", 1}}}));
auto
s
=
"null"
;
auto
s
=
"[
\"
foo
\"
,1,2,3,false,{
\"
one
\"
:1}]"
;
json
j
=
json
::
parse
(
s
);
CHECK
(
j
==
json
());
CHECK
(
j
==
json
(
{
"foo"
,
1
,
2
,
3
,
false
,
{{
"one"
,
1
}}}
));
}
SECTION
(
"operator<<"
)
{
// std::stringstream ss;
// ss << "[\"foo\",1,2,3,false,{\"one\":1}]";
// json j;
// j << ss;
// CHECK(j == json({"foo", 1, 2, 3, false, {{"one", 1}}}));
std
::
stringstream
ss
;
ss
<<
"
null
"
;
ss
<<
"
[
\"
foo
\"
,1,2,3,false,{
\"
one
\"
:1}]
"
;
json
j
;
j
<<
ss
;
CHECK
(
j
==
json
());
CHECK
(
j
==
json
(
{
"foo"
,
1
,
2
,
3
,
false
,
{{
"one"
,
1
}}}
));
}
SECTION
(
"operator>>"
)
{
// std::stringstream ss;
// ss << "[\"foo\",1,2,3,false,{\"one\":1}]";
// json j;
// ss >> j;
// CHECK(j == json({"foo", 1, 2, 3, false, {{"one", 1}}}));
std
::
stringstream
ss
;
ss
<<
"
null
"
;
ss
<<
"
[
\"
foo
\"
,1,2,3,false,{
\"
one
\"
:1}]
"
;
json
j
;
ss
>>
j
;
CHECK
(
j
==
json
());
CHECK
(
j
==
json
(
{
"foo"
,
1
,
2
,
3
,
false
,
{{
"one"
,
1
}}}
));
}
}
...
...
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