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
2cb925c1
Commit
2cb925c1
authored
9 years ago
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
adding support for escaped reference tokens
parent
726051e9
develop
bon8
coverity_scan
feature/optional
gcc_warning_flags
icpc
issue2228
issue2615
issue2932
issue3232_use_catch
master
release/3.10.2
release/3.10.3
release/3.10.4
release/3.10.5
string_view
string_view2
update_doctest
3.6.1
3.3.0
3.2.0
3.1.2
3.1.0
3.0.1
3.0.0
2.1.1
2.1.0
2.0.10
2.0.9
2.0.8
v3.10.5
v3.10.4
v3.10.3
v3.10.2
v3.10.1
v3.10.0
v3.9.1
v3.9.0
v3.8.0
v3.7.3
v3.7.2
v3.7.1
v3.7.0
v3.6.1
v3.6.0
v3.5.0
v3.4.0
v3.3.0
v3.2.0
v3.1.2
v3.1.1
v3.1.0
v3.0.1
v3.0.0
v2.1.1
v2.1.0
v2.0.10
v2.0.9
v2.0.8
v2.0.7
v2.0.6
v2.0.5
v2.0.4
v2.0.3
v2.0.2
v2.0.1
v2.0.0
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
150 additions
and
17 deletions
+150
-17
src/json.hpp
src/json.hpp
+52
-5
src/json.hpp.re2c
src/json.hpp.re2c
+52
-5
test/unit.cpp
test/unit.cpp
+46
-7
No files found.
src/json.hpp
View file @
2cb925c1
...
...
@@ -8861,18 +8861,18 @@ basic_json_parser_64:
/// return referenced value
reference
get
(
reference
j
)
{
reference
result
=
j
;
pointer
result
=
&
j
;
for
(
const
auto
&
reference_token
:
reference_tokens
)
{
switch
(
result
.
m_type
)
switch
(
result
->
m_type
)
{
case
value_t
:
:
object
:
result
=
result
[
reference_token
]
;
result
=
&
result
->
at
(
reference_token
)
;
continue
;
case
value_t
:
:
array
:
result
=
result
[
std
::
stoi
(
reference_token
)]
;
result
=
&
result
->
at
(
static_cast
<
size_t
>
(
std
::
stoi
(
reference_token
)))
;
continue
;
default:
...
...
@@ -8880,13 +8880,52 @@ basic_json_parser_64:
}
}
return
result
;
return
*
result
;
}
const_reference
get
(
const_reference
j
)
const
{
const_pointer
result
=
&
j
;
for
(
const
auto
&
reference_token
:
reference_tokens
)
{
switch
(
result
->
m_type
)
{
case
value_t
:
:
object
:
result
=
&
result
->
at
(
reference_token
);
continue
;
case
value_t
:
:
array
:
result
=
&
result
->
at
(
static_cast
<
size_t
>
(
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
{};
/// replace all occurrences of a substring by another string
void
replace_substring
(
std
::
string
&
s
,
const
std
::
string
&
f
,
const
std
::
string
&
t
)
{
assert
(
not
f
.
empty
());
for
(
size_t
pos
=
s
.
find
(
f
);
// find first occurrence of f
pos
!=
std
::
string
::
npos
;
// make sure f was found
s
.
replace
(
pos
,
f
.
size
(),
t
),
// replace with t
pos
=
s
.
find
(
f
,
pos
+
t
.
size
())
// find next occurrence of f
);
}
/// split the string input to reference tokens
void
split
(
std
::
string
reference_string
)
{
...
...
@@ -8915,6 +8954,14 @@ basic_json_parser_64:
{
reference_tokens
.
push_back
(
""
);
}
for
(
auto
&
reference_token
:
reference_tokens
)
{
// first transform any occurrence of the sequence '~1' to '/'
replace_substring
(
reference_token
,
"~1"
,
"/"
);
// then transform any occurrence of the sequence '~0' to '~'
replace_substring
(
reference_token
,
"~0"
,
"~"
);
}
}
};
};
...
...
This diff is collapsed.
Click to expand it.
src/json.hpp.re2c
View file @
2cb925c1
...
...
@@ -8140,18 +8140,18 @@ class basic_json
/// return referenced value
reference get(reference j)
{
reference result =
j;
pointer result = &
j;
for (const auto& reference_token : reference_tokens)
{
switch (result
.
m_type)
switch (result
->
m_type)
{
case value_t::object:
result =
result[reference_token]
;
result =
&result->at(reference_token)
;
continue;
case value_t::array:
result =
result[std::stoi(reference_token)]
;
result =
&result->at(static_cast<size_t>(std::stoi(reference_token)))
;
continue;
default:
...
...
@@ -8159,13 +8159,52 @@ class basic_json
}
}
return result;
return *result;
}
const_reference get(const_reference j) const
{
const_pointer result = &j;
for (const auto& reference_token : reference_tokens)
{
switch (result->m_type)
{
case value_t::object:
result = &result->at(reference_token);
continue;
case value_t::array:
result = &result->at(static_cast<size_t>(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 {};
/// replace all occurrences of a substring by another string
void replace_substring(std::string& s,
const std::string& f,
const std::string& t)
{
assert(not f.empty());
for (
size_t pos = s.find(f); // find first occurrence of f
pos != std::string::npos; // make sure f was found
s.replace(pos, f.size(), t), // replace with t
pos = s.find(f, pos + t.size()) // find next occurrence of f
);
}
/// split the string input to reference tokens
void split(std::string reference_string)
{
...
...
@@ -8194,6 +8233,14 @@ class basic_json
{
reference_tokens.push_back("");
}
for (auto& reference_token : reference_tokens)
{
// first transform any occurrence of the sequence '~1' to '/'
replace_substring(reference_token, "~1", "/");
// then transform any occurrence of the sequence '~0' to '~'
replace_substring(reference_token, "~0", "~");
}
}
};
};
...
...
This diff is collapsed.
Click to expand it.
test/unit.cpp
View file @
2cb925c1
...
...
@@ -12071,13 +12071,53 @@ TEST_CASE("JSON pointers")
}
)"
_json
;
json
::
json_pointer
jp0
(
""
);
json
::
json_pointer
jp1
(
"/foo"
);
//json::json_pointer jp2("/foo/0");
const
json
j_const
=
j
;
SECTION
(
"nonconst access"
)
{
// the whole document
CHECK
(
json
::
json_pointer
().
get
(
j
)
==
j
);
CHECK
(
json
::
json_pointer
(
""
).
get
(
j
)
==
j
);
// array access
CHECK
(
json
::
json_pointer
(
"/foo"
).
get
(
j
)
==
j
[
"foo"
]);
CHECK
(
json
::
json_pointer
(
"/foo/0"
).
get
(
j
)
==
j
[
"foo"
][
0
]);
CHECK
(
json
::
json_pointer
(
"/foo/1"
).
get
(
j
)
==
j
[
"foo"
][
1
]);
// empty string access
CHECK
(
json
::
json_pointer
(
"/"
).
get
(
j
)
==
j
[
""
]);
// other cases
CHECK
(
json
::
json_pointer
(
"/ "
).
get
(
j
)
==
j
[
" "
]);
CHECK
(
json
::
json_pointer
(
"/c%d"
).
get
(
j
)
==
j
[
"c%d"
]);
CHECK
(
json
::
json_pointer
(
"/e^f"
).
get
(
j
)
==
j
[
"e^f"
]);
CHECK
(
json
::
json_pointer
(
"/g|h"
).
get
(
j
)
==
j
[
"g|h"
]);
CHECK
(
json
::
json_pointer
(
"/i
\\
j"
).
get
(
j
)
==
j
[
"i
\\
j"
]);
CHECK
(
json
::
json_pointer
(
"/k
\"
l"
).
get
(
j
)
==
j
[
"k
\"
l"
]);
// escaped access
CHECK
(
json
::
json_pointer
(
"/a~1b"
).
get
(
j
)
==
j
[
"a/b"
]);
CHECK
(
json
::
json_pointer
(
"/m~0n"
).
get
(
j
)
==
j
[
"m~n"
]);
auto
jp0_
=
jp0
.
get
(
j
);
auto
jp1_
=
jp1
.
get
(
j
);
//auto jp2_ = jp2.get(j);
// unescaped access
CHECK_THROWS_AS
(
json
::
json_pointer
(
"/a/b"
).
get
(
j
),
std
::
out_of_range
);
CHECK_THROWS_WITH
(
json
::
json_pointer
(
"/a/b"
).
get
(
j
),
"key 'a' not found"
);
// "/a/b" works for JSON {"a": {"b": 42}}
CHECK
(
json
::
json_pointer
(
"/a/b"
).
get
({{
"a"
,
{{
"b"
,
42
}}}})
==
json
(
42
));
}
SECTION
(
"const access"
)
{
CHECK
(
j_const
==
json
::
json_pointer
().
get
(
j_const
));
CHECK
(
j_const
==
json
::
json_pointer
(
""
).
get
(
j_const
));
CHECK
(
j_const
[
"foo"
]
==
json
::
json_pointer
(
"/foo"
).
get
(
j_const
));
CHECK
(
j_const
[
"foo"
][
0
]
==
json
::
json_pointer
(
"/foo/0"
).
get
(
j_const
));
CHECK
(
j_const
[
"foo"
][
1
]
==
json
::
json_pointer
(
"/foo/1"
).
get
(
j_const
));
CHECK
(
j_const
[
""
]
==
json
::
json_pointer
(
"/"
).
get
(
j_const
));
CHECK
(
j_const
[
" "
]
==
json
::
json_pointer
(
"/ "
).
get
(
j_const
));
}
}
}
...
...
@@ -12437,4 +12477,3 @@ TEST_CASE("regression tests")
CHECK
(
j3c
.
dump
()
==
"1e04"
);
}
}
This diff is collapsed.
Click to expand it.
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