proxygen
json_patch.h
Go to the documentation of this file.
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 
17 #pragma once
18 
19 #include <folly/Expected.h>
20 #include <folly/Optional.h>
21 #include <folly/dynamic.h>
22 #include <folly/json_pointer.h>
23 
24 namespace folly {
25 
26 /*
27  * json_patch
28  *
29  * As described in RFC 6902 "JSON Patch".
30  *
31  * Implements parsing. Application over data structures must be
32  * implemented separately.
33  */
34 class json_patch {
35  public:
36  enum class parse_error_code : uint8_t {
37  undefined,
39  missing_op,
40  unknown_op,
48  };
49 
50  /*
51  * If parsing JSON patch object fails we return err code along with
52  * pointer to part of JSON document that we could not parse
53  */
54  struct parse_error {
55  // one of the above error codes
57  // pointer to object that caused the error
58  dynamic const* obj{};
59  };
60 
62  invalid = 0,
63  test,
64  remove,
65  add,
66  replace,
67  move,
68  copy,
69  };
70 
71  /*
72  * Single JSON patch operation. Argument may vary based on op type
73  */
74  struct patch_operation {
79  friend bool operator==(
80  patch_operation const& lhs,
81  patch_operation const& rhs) {
82  return lhs.op_code == rhs.op_code && lhs.path == rhs.path &&
83  lhs.from == rhs.from && lhs.value == rhs.value;
84  }
85  friend bool operator!=(
86  patch_operation const& lhs,
87  patch_operation const& rhs) {
88  return !(lhs == rhs);
89  }
90  };
91 
92  json_patch() = default;
93  ~json_patch() = default;
94 
96  dynamic const& obj) noexcept;
97 
98  std::vector<patch_operation> const& ops() const;
99 
100  private:
101  std::vector<patch_operation> ops_;
102 };
103 
104 } // namespace folly
auto add
Definition: BaseTest.cpp:70
Optional< json_pointer > from
Definition: json_patch.h:77
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
static folly::Expected< json_patch, parse_error > try_parse(dynamic const &obj) noexcept
Definition: json_patch.cpp:38
json_patch()=default
FOLLY_PUSH_WARNING RHS rhs
Definition: Traits.h:649
void BENCHFUN() replace(size_t iters, size_t arg)
patch_operation_code op_code
Definition: json_patch.h:75
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
Definition: Utility.h:72
std::vector< patch_operation > ops_
Definition: json_patch.h:101
std::vector< patch_operation > const & ops() const
Definition: json_patch.cpp:151
~json_patch()=default
friend bool operator!=(patch_operation const &lhs, patch_operation const &rhs)
Definition: json_patch.h:85
friend bool operator==(patch_operation const &lhs, patch_operation const &rhs)
Definition: json_patch.h:79