mirror of
https://github.com/tristanpenman/valijson.git
synced 2025-03-03 12:58:03 +01:00
Add check for hyphen used as array index in JSON Pointer
This commit is contained in:
parent
5387aaf076
commit
7c4d935f4c
@ -114,6 +114,11 @@ inline AdapterType resolveJsonPointer(
|
|||||||
return resolveJsonPointer(node, jsonPointer, jsonPointerNext);
|
return resolveJsonPointer(node, jsonPointer, jsonPointerNext);
|
||||||
|
|
||||||
} else if (node.isArray()) {
|
} else if (node.isArray()) {
|
||||||
|
if (referenceToken.compare("-") == 0) {
|
||||||
|
throw std::runtime_error("Hyphens cannot be used as array indices "
|
||||||
|
"since the requested array element does not yet exist");
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Fragment must be non-negative integer
|
// Fragment must be non-negative integer
|
||||||
const uint64_t index = boost::lexical_cast<uint64_t>(
|
const uint64_t index = boost::lexical_cast<uint64_t>(
|
||||||
|
@ -172,6 +172,36 @@ std::vector<boost::shared_ptr<JsonPointerTestCase> >
|
|||||||
testCases.push_back(testCase);
|
testCases.push_back(testCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Allow the "-" character is not useful within the context of this library,
|
||||||
|
// there is an explicit check for it, so that a custom error message can
|
||||||
|
// be included in the exception that is thrown.
|
||||||
|
//
|
||||||
|
// From the JSON Pointer specification (RFC 6901, April 2013):
|
||||||
|
//
|
||||||
|
// Note that the use of the "-" character to index an array will always
|
||||||
|
// result in such an error condition because by definition it refers to
|
||||||
|
// a nonexistent array element. Thus, applications of JSON Pointer need
|
||||||
|
// to specify how that character is to be handled, if it is to be
|
||||||
|
// useful.
|
||||||
|
//
|
||||||
|
|
||||||
|
{
|
||||||
|
rapidjson::Value testArray;
|
||||||
|
testArray.SetArray();
|
||||||
|
testArray.PushBack("test0", allocator);
|
||||||
|
testArray.PushBack("test1", allocator);
|
||||||
|
testArray.PushBack("test2", allocator);
|
||||||
|
|
||||||
|
testCase = boost::make_shared<JsonPointerTestCase>(
|
||||||
|
"Resolving '/test/-' in object containing one member containing "
|
||||||
|
"an array with 3 elements should throw an exception");
|
||||||
|
testCase->value.SetNull();
|
||||||
|
testCase->jsonPointer = "/test/-";
|
||||||
|
testCase->expectedValue = NULL;
|
||||||
|
testCases.push_back(testCase);
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// The following tests ensure that escape sequences are handled correctly.
|
// The following tests ensure that escape sequences are handled correctly.
|
||||||
//
|
//
|
||||||
|
Loading…
x
Reference in New Issue
Block a user