Skip to content

Commit

Permalink
adding support for escaped reference tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Apr 13, 2016
1 parent 726051e commit 2cb925c
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 17 deletions.
57 changes: 52 additions & 5 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8861,32 +8861,71 @@ 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:
throw std::domain_error("unresolved reference token '" + reference_token + "'");
}
}

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)
{
Expand Down Expand Up @@ -8915,6 +8954,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", "~");
}
}
};
};
Expand Down
57 changes: 52 additions & 5 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -8140,32 +8140,71 @@ 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:
throw std::domain_error("unresolved reference token '" + reference_token + "'");
}
}

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)
{
Expand Down Expand Up @@ -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", "~");
}
}
};
};
Expand Down
53 changes: 46 additions & 7 deletions test/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}

Expand Down Expand Up @@ -12437,4 +12477,3 @@ TEST_CASE("regression tests")
CHECK(j3c.dump() == "1e04");
}
}

0 comments on commit 2cb925c

Please sign in to comment.