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
2cb925c1
Commit
2cb925c1
authored
Apr 13, 2016
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
adding support for escaped reference tokens
parent
726051e9
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
150 additions
and
17 deletions
+150
-17
json.hpp
src/json.hpp
+52
-5
json.hpp.re2c
src/json.hpp.re2c
+52
-5
unit.cpp
test/unit.cpp
+46
-7
No files found.
src/json.hpp
View file @
2cb925c1
...
@@ -8861,18 +8861,18 @@ basic_json_parser_64:
...
@@ -8861,18 +8861,18 @@ basic_json_parser_64:
/// return referenced value
/// return referenced value
reference
get
(
reference
j
)
reference
get
(
reference
j
)
{
{
reference
result
=
j
;
pointer
result
=
&
j
;
for
(
const
auto
&
reference_token
:
reference_tokens
)
for
(
const
auto
&
reference_token
:
reference_tokens
)
{
{
switch
(
result
.
m_type
)
switch
(
result
->
m_type
)
{
{
case
value_t
:
:
object
:
case
value_t
:
:
object
:
result
=
result
[
reference_token
]
;
result
=
&
result
->
at
(
reference_token
)
;
continue
;
continue
;
case
value_t
:
:
array
:
case
value_t
:
:
array
:
result
=
result
[
std
::
stoi
(
reference_token
)]
;
result
=
&
result
->
at
(
static_cast
<
size_t
>
(
std
::
stoi
(
reference_token
)))
;
continue
;
continue
;
default
:
default
:
...
@@ -8880,13 +8880,52 @@ basic_json_parser_64:
...
@@ -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
:
private
:
/// the reference tokens
/// the reference tokens
std
::
vector
<
std
::
string
>
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
/// split the string input to reference tokens
void
split
(
std
::
string
reference_string
)
void
split
(
std
::
string
reference_string
)
{
{
...
@@ -8915,6 +8954,14 @@ basic_json_parser_64:
...
@@ -8915,6 +8954,14 @@ basic_json_parser_64:
{
{
reference_tokens
.
push_back
(
""
);
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"
,
"~"
);
}
}
}
};
};
};
};
...
...
src/json.hpp.re2c
View file @
2cb925c1
...
@@ -8140,18 +8140,18 @@ class basic_json
...
@@ -8140,18 +8140,18 @@ class basic_json
/// return referenced value
/// return referenced value
reference get(reference j)
reference get(reference j)
{
{
reference result =
j;
pointer result = &
j;
for (const auto& reference_token : reference_tokens)
for (const auto& reference_token : reference_tokens)
{
{
switch (result
.
m_type)
switch (result
->
m_type)
{
{
case value_t::object:
case value_t::object:
result =
result[reference_token]
;
result =
&result->at(reference_token)
;
continue;
continue;
case value_t::array:
case value_t::array:
result =
result[std::stoi(reference_token)]
;
result =
&result->at(static_cast<size_t>(std::stoi(reference_token)))
;
continue;
continue;
default:
default:
...
@@ -8159,13 +8159,52 @@ class basic_json
...
@@ -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:
private:
/// the reference tokens
/// the reference tokens
std::vector<std::string> 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
/// split the string input to reference tokens
void split(std::string reference_string)
void split(std::string reference_string)
{
{
...
@@ -8194,6 +8233,14 @@ class basic_json
...
@@ -8194,6 +8233,14 @@ class basic_json
{
{
reference_tokens.push_back("");
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", "~");
}
}
}
};
};
};
};
...
...
test/unit.cpp
View file @
2cb925c1
...
@@ -12071,13 +12071,53 @@ TEST_CASE("JSON pointers")
...
@@ -12071,13 +12071,53 @@ TEST_CASE("JSON pointers")
}
}
)"
_json
;
)"
_json
;
json
::
json_pointer
jp0
(
""
);
const
json
j_const
=
j
;
json
::
json_pointer
jp1
(
"/foo"
);
//json::json_pointer jp2("/foo/0");
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
);
// unescaped access
auto
jp1_
=
jp1
.
get
(
j
);
CHECK_THROWS_AS
(
json
::
json_pointer
(
"/a/b"
).
get
(
j
),
std
::
out_of_range
);
//auto jp2_ = jp2.get(j);
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")
...
@@ -12437,4 +12477,3 @@ TEST_CASE("regression tests")
CHECK
(
j3c
.
dump
()
==
"1e04"
);
CHECK
(
j3c
.
dump
()
==
"1e04"
);
}
}
}
}
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