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
d3e7f9da
Commit
d3e7f9da
authored
Oct 10, 2016
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
code cleanup for #323
parent
97280bbc
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
68 deletions
+54
-68
json.hpp
src/json.hpp
+27
-34
json.hpp.re2c
src/json.hpp.re2c
+27
-34
No files found.
src/json.hpp
View file @
d3e7f9da
...
@@ -9457,10 +9457,27 @@ basic_json_parser_63:
...
@@ -9457,10 +9457,27 @@ basic_json_parser_63:
{
{
for
(
const
auto
&
reference_token
:
reference_tokens
)
for
(
const
auto
&
reference_token
:
reference_tokens
)
{
{
//
error condition (cf. RFC 6901, Sect. 4)
//
convert null values to arrays or objects before continuing
if
(
reference_token
.
size
()
>
1
and
reference_token
[
0
]
==
'0'
)
if
(
ptr
->
m_type
==
value_t
::
null
)
{
{
throw
std
::
domain_error
(
"array index must not begin with '0'"
);
// check if reference token is a number
const
bool
nums
=
std
::
all_of
(
reference_token
.
begin
(),
reference_token
.
end
(),
[](
const
char
x
)
{
return
std
::
isdigit
(
x
);
});
// change value to array for numbers or "-" or to object
// otherwise
if
(
nums
or
reference_token
==
"-"
)
{
*
ptr
=
value_t
::
array
;
}
else
{
*
ptr
=
value_t
::
object
;
}
}
}
switch
(
ptr
->
m_type
)
switch
(
ptr
->
m_type
)
...
@@ -9474,6 +9491,13 @@ basic_json_parser_63:
...
@@ -9474,6 +9491,13 @@ basic_json_parser_63:
case
value_t
:
:
array
:
case
value_t
:
:
array
:
{
{
// error condition (cf. RFC 6901, Sect. 4)
if
(
reference_token
.
size
()
>
1
and
reference_token
[
0
]
==
'0'
)
{
throw
std
::
domain_error
(
"array index must not begin with '0'"
);
}
if
(
reference_token
==
"-"
)
if
(
reference_token
==
"-"
)
{
{
// explicityly treat "-" as index beyond the end
// explicityly treat "-" as index beyond the end
...
@@ -9487,37 +9511,6 @@ basic_json_parser_63:
...
@@ -9487,37 +9511,6 @@ basic_json_parser_63:
break
;
break
;
}
}
// null values are converted to arrays or objects
case
value_t
:
:
null
:
{
// check if reference token is a number
const
bool
nums
=
std
::
all_of
(
reference_token
.
begin
(),
reference_token
.
end
(),
[](
const
char
x
)
{
return
std
::
isdigit
(
x
);
});
if
(
nums
)
{
// if reference token consists solely of numbers
// use it as array index -> create array
ptr
=
&
ptr
->
operator
[](
static_cast
<
size_type
>
(
std
::
stoi
(
reference_token
)));
}
else
if
(
reference_token
==
"-"
)
{
// explicityly treat "-" as index beyond the end
// which is 0 for an empty array -> create array
ptr
=
&
ptr
->
operator
[](
0
);
}
else
{
// treat reference token as key -> create object
ptr
=
&
ptr
->
operator
[](
reference_token
);
}
break
;
}
default
:
default
:
{
{
throw
std
::
out_of_range
(
"unresolved reference token '"
+
reference_token
+
"'"
);
throw
std
::
out_of_range
(
"unresolved reference token '"
+
reference_token
+
"'"
);
...
...
src/json.hpp.re2c
View file @
d3e7f9da
...
@@ -8754,10 +8754,27 @@ class basic_json
...
@@ -8754,10 +8754,27 @@ class basic_json
{
{
for (const auto& reference_token : reference_tokens)
for (const auto& reference_token : reference_tokens)
{
{
//
error condition (cf. RFC 6901, Sect. 4)
//
convert null values to arrays or objects before continuing
if (
reference_token.size() > 1 and reference_token[0] == '0'
)
if (
ptr->m_type == value_t::null
)
{
{
throw std::domain_error("array index must not begin with '0'");
// check if reference token is a number
const bool nums = std::all_of(reference_token.begin(),
reference_token.end(),
[](const char x)
{
return std::isdigit(x);
});
// change value to array for numbers or "-" or to object
// otherwise
if (nums or reference_token == "-")
{
*ptr = value_t::array;
}
else
{
*ptr = value_t::object;
}
}
}
switch (ptr->m_type)
switch (ptr->m_type)
...
@@ -8771,6 +8788,13 @@ class basic_json
...
@@ -8771,6 +8788,13 @@ class basic_json
case value_t::array:
case value_t::array:
{
{
// error condition (cf. RFC 6901, Sect. 4)
if (reference_token.size() > 1 and reference_token[0] == '0')
{
throw std::domain_error("array index must not begin with '0'");
}
if (reference_token == "-")
if (reference_token == "-")
{
{
// explicityly treat "-" as index beyond the end
// explicityly treat "-" as index beyond the end
...
@@ -8784,37 +8808,6 @@ class basic_json
...
@@ -8784,37 +8808,6 @@ class basic_json
break;
break;
}
}
// null values are converted to arrays or objects
case value_t::null:
{
// check if reference token is a number
const bool nums = std::all_of(reference_token.begin(),
reference_token.end(),
[](const char x)
{
return std::isdigit(x);
});
if (nums)
{
// if reference token consists solely of numbers
// use it as array index -> create array
ptr = &ptr->operator[](static_cast<size_type>(std::stoi(reference_token)));
}
else if (reference_token == "-")
{
// explicityly treat "-" as index beyond the end
// which is 0 for an empty array -> create array
ptr = &ptr->operator[](0);
}
else
{
// treat reference token as key -> create object
ptr = &ptr->operator[](reference_token);
}
break;
}
default:
default:
{
{
throw std::out_of_range("unresolved reference token '" + reference_token + "'");
throw std::out_of_range("unresolved reference token '" + reference_token + "'");
...
...
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