json_pointer.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2011-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/json_pointer.h>
  17. #include <folly/String.h>
  18. namespace folly {
  19. // static, public
  20. Expected<json_pointer, json_pointer::parse_error> json_pointer::try_parse(
  21. StringPiece const str) {
  22. // pointer describes complete document
  23. if (str.empty()) {
  24. return json_pointer{};
  25. }
  26. if (str.at(0) != '/') {
  27. return makeUnexpected(parse_error::invalid_first_character);
  28. }
  29. std::vector<std::string> tokens;
  30. splitTo<std::string>("/", str, std::inserter(tokens, tokens.begin()));
  31. tokens.erase(tokens.begin());
  32. for (auto& token : tokens) {
  33. if (!unescape(token)) {
  34. return makeUnexpected(parse_error::invalid_escape_sequence);
  35. }
  36. }
  37. return json_pointer(std::move(tokens));
  38. }
  39. // static, public
  40. json_pointer json_pointer::parse(StringPiece const str) {
  41. auto res = try_parse(str);
  42. if (res.hasValue()) {
  43. return std::move(res.value());
  44. }
  45. switch (res.error()) {
  46. case parse_error::invalid_first_character:
  47. throw json_pointer::parse_exception(
  48. "non-empty JSON pointer string does not start with '/'");
  49. case parse_error::invalid_escape_sequence:
  50. throw json_pointer::parse_exception(
  51. "Invalid escape sequence in JSON pointer string");
  52. default:
  53. assume_unreachable();
  54. }
  55. }
  56. bool json_pointer::is_prefix_of(json_pointer const& other) const noexcept {
  57. auto const& other_tokens = other.tokens();
  58. if (tokens_.size() > other_tokens.size()) {
  59. return false;
  60. }
  61. auto const other_begin = other_tokens.cbegin();
  62. auto const other_end = other_tokens.cbegin() + tokens_.size();
  63. return std::equal(tokens_.cbegin(), tokens_.cend(), other_begin, other_end);
  64. }
  65. std::vector<std::string> const& json_pointer::tokens() const {
  66. return tokens_;
  67. }
  68. // private
  69. json_pointer::json_pointer(std::vector<std::string> tokens) noexcept
  70. : tokens_{std::move(tokens)} {}
  71. // private, static
  72. bool json_pointer::unescape(std::string& str) {
  73. char const* end = &str[str.size()];
  74. char* out = &str.front();
  75. char const* decode = out;
  76. while (decode < end) {
  77. if (*decode != '~') {
  78. *out++ = *decode++;
  79. continue;
  80. }
  81. if (decode + 1 == end) {
  82. return false;
  83. }
  84. switch (decode[1]) {
  85. case '1':
  86. *out++ = '/';
  87. break;
  88. case '0':
  89. *out++ = '~';
  90. break;
  91. default:
  92. return false;
  93. }
  94. decode += 2;
  95. }
  96. str.resize(out - &str.front());
  97. return true;
  98. }
  99. } // namespace folly