Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
Project
Project
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
capnproto
Commits
c662e628
Commit
c662e628
authored
Nov 12, 2015
by
Kamal Marhubi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reject trailing commas in JSON arrays and objects
Per spec.
parent
88ac1058
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
8 deletions
+19
-8
json-test.c++
c++/src/capnp/compat/json-test.c++
+2
-0
json.c++
c++/src/capnp/compat/json.c++
+17
-8
No files found.
c++/src/capnp/compat/json-test.c++
View file @
c662e628
...
...
@@ -415,10 +415,12 @@ KJ_TEST("basic json decoding") {
KJ_EXPECT_THROW_MESSAGE
(
"Unexpected input"
,
json
.
decodeRaw
(
"[}]"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"Unexpected input"
,
json
.
decodeRaw
(
"[1, , ]"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"Unexpected input"
,
json
.
decodeRaw
(
"[,]"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"Unexpected input"
,
json
.
decodeRaw
(
"[true,]"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"Unexpected input"
,
json
.
decodeRaw
(
"[, 1]"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"Unexpected input"
,
json
.
decodeRaw
(
"[1
\"\"
]"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"Unexpected input"
,
json
.
decodeRaw
(
"[1,,
\"\"
]"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"Unexpected input"
,
json
.
decodeRaw
(
"{
\"
a
\"
1: 0}"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"Unexpected input"
,
json
.
decodeRaw
(
R"({"some": null,})"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"Input remains"
,
json
.
decodeRaw
(
"11a"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"Invalid escape"
,
json
.
decodeRaw
(
R"("\z")"
,
root
));
KJ_EXPECT_THROW_MESSAGE
(
"Invalid escape"
,
json
.
decodeRaw
(
R"("\z")"
,
root
));
...
...
c++/src/capnp/compat/json.c++
View file @
c662e628
...
...
@@ -432,6 +432,7 @@ public:
// Cap'n Proto message. This also applies to parseObject below.
kj
::
Vector
<
Orphan
<
JsonValue
>>
values
;
auto
orphanage
=
Orphanage
::
getForMessageContaining
(
output
);
bool
expectComma
=
false
;
consume
(
'['
);
KJ_REQUIRE
(
++
nestingDepth_
<=
maxNestingDepth_
,
"JSON message nested too deeply."
);
...
...
@@ -440,13 +441,17 @@ public:
while
(
consumeWhitespace
(),
nextChar
()
!=
']'
)
{
auto
orphan
=
orphanage
.
newOrphan
<
JsonValue
>
();
auto
builder
=
orphan
.
get
();
parseValue
(
builder
);
values
.
add
(
kj
::
mv
(
orphan
));
if
(
consumeWhitespace
(),
nextChar
()
!=
']'
)
{
// TODO(someday): The JSON spec forbids a trailing comma, but we allow it.
if
(
expectComma
)
{
consumeWhitespace
();
consume
(
','
);
consumeWhitespace
();
}
parseValue
(
builder
);
values
.
add
(
kj
::
mv
(
orphan
));
expectComma
=
true
;
}
output
.
initArray
(
values
.
size
());
...
...
@@ -462,6 +467,7 @@ public:
void
parseObject
(
JsonValue
::
Builder
&
output
)
{
kj
::
Vector
<
Orphan
<
JsonValue
::
Field
>>
fields
;
auto
orphanage
=
Orphanage
::
getForMessageContaining
(
output
);
bool
expectComma
=
false
;
consume
(
'{'
);
KJ_REQUIRE
(
++
nestingDepth_
<=
maxNestingDepth_
,
"JSON message nested too deeply."
);
...
...
@@ -471,6 +477,12 @@ public:
auto
orphan
=
orphanage
.
newOrphan
<
JsonValue
::
Field
>
();
auto
builder
=
orphan
.
get
();
if
(
expectComma
)
{
consumeWhitespace
();
consume
(
','
);
consumeWhitespace
();
}
builder
.
setName
(
consumeQuotedString
());
consumeWhitespace
();
...
...
@@ -482,10 +494,7 @@ public:
fields
.
add
(
kj
::
mv
(
orphan
));
if
(
consumeWhitespace
(),
nextChar
()
!=
'}'
)
{
// TODO(someday): The JSON spec forbids a trailing comma, but we allow it.
consume
(
','
);
}
expectComma
=
true
;
}
output
.
initObject
(
fields
.
size
());
...
...
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