Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
726051e9
Commit
726051e9
authored
Apr 11, 2016
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
very first draft of a JSON pointer API
parent
507322e6
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
175 additions
and
0 deletions
+175
-0
src/json.hpp
src/json.hpp
+73
-0
src/json.hpp.re2c
src/json.hpp.re2c
+73
-0
test/unit.cpp
test/unit.cpp
+29
-0
No files found.
src/json.hpp
View file @
726051e9
...
...
@@ -8844,6 +8844,79 @@ basic_json_parser_64:
/// the lexer
lexer
m_lexer
;
};
public:
class
json_pointer
{
public:
/// empty reference token
json_pointer
()
=
default
;
/// nonempty reference token
json_pointer
(
const
std
::
string
&
s
)
{
split
(
s
);
}
/// return referenced value
reference
get
(
reference
j
)
{
reference
result
=
j
;
for
(
const
auto
&
reference_token
:
reference_tokens
)
{
switch
(
result
.
m_type
)
{
case
value_t
:
:
object
:
result
=
result
[
reference_token
];
continue
;
case
value_t
:
:
array
:
result
=
result
[
std
::
stoi
(
reference_token
)];
continue
;
default:
throw
std
::
domain_error
(
"unresolved reference token '"
+
reference_token
+
"'"
);
}
}
return
result
;
}
private:
/// the reference tokens
std
::
vector
<
std
::
string
>
reference_tokens
{};
/// split the string input to reference tokens
void
split
(
std
::
string
reference_string
)
{
// special case: empty reference string -> no reference tokens
if
(
reference_string
.
empty
())
{
return
;
}
// check if nonempty reference string begins with slash
if
(
reference_string
[
0
]
!=
'/'
)
{
throw
std
::
domain_error
(
"JSON pointer must be empty or begin with '/'"
);
}
// tokenize reference string
auto
ptr
=
std
::
strtok
(
&
reference_string
[
0
],
"/"
);
while
(
ptr
!=
nullptr
)
{
reference_tokens
.
push_back
(
ptr
);
ptr
=
std
::
strtok
(
NULL
,
"/"
);
}
// special case: reference string was just "/"
if
(
reference_tokens
.
empty
())
{
reference_tokens
.
push_back
(
""
);
}
}
};
};
...
...
src/json.hpp.re2c
View file @
726051e9
...
...
@@ -8123,6 +8123,79 @@ class basic_json
/// the lexer
lexer m_lexer;
};
public:
class json_pointer
{
public:
/// empty reference token
json_pointer() = default;
/// nonempty reference token
json_pointer(const std::string& s)
{
split(s);
}
/// return referenced value
reference get(reference j)
{
reference result = j;
for (const auto& reference_token : reference_tokens)
{
switch (result.m_type)
{
case value_t::object:
result = result[reference_token];
continue;
case value_t::array:
result = result[std::stoi(reference_token)];
continue;
default:
throw std::domain_error("unresolved reference token '" + reference_token + "'");
}
}
return result;
}
private:
/// the reference tokens
std::vector<std::string> reference_tokens {};
/// split the string input to reference tokens
void split(std::string reference_string)
{
// special case: empty reference string -> no reference tokens
if (reference_string.empty())
{
return;
}
// check if nonempty reference string begins with slash
if (reference_string[0] != '/')
{
throw std::domain_error("JSON pointer must be empty or begin with '/'");
}
// tokenize reference string
auto ptr = std::strtok(&reference_string[0], "/");
while (ptr != nullptr)
{
reference_tokens.push_back(ptr);
ptr = std::strtok(NULL, "/");
}
// special case: reference string was just "/"
if (reference_tokens.empty())
{
reference_tokens.push_back("");
}
}
};
};
...
...
test/unit.cpp
View file @
726051e9
...
...
@@ -12052,6 +12052,35 @@ TEST_CASE("Unicode", "[hide]")
}
}
TEST_CASE
(
"JSON pointers"
)
{
SECTION
(
"examples from RFC 6901"
)
{
json
j
=
R"(
{
"foo": ["bar", "baz"],
"": 0,
"a/b": 1,
"c%d": 2,
"e^f": 3,
"g|h": 4,
"i\\j": 5,
"k\"l": 6,
" ": 7,
"m~n": 8
}
)"
_json
;
json
::
json_pointer
jp0
(
""
);
json
::
json_pointer
jp1
(
"/foo"
);
//json::json_pointer jp2("/foo/0");
auto
jp0_
=
jp0
.
get
(
j
);
auto
jp1_
=
jp1
.
get
(
j
);
//auto jp2_ = jp2.get(j);
}
}
TEST_CASE
(
"regression tests"
)
{
SECTION
(
"issue #60 - Double quotation mark is not parsed correctly"
)
...
...
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