Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
jsonpointer.h
1#ifndef SOURCEMETA_CORE_JSONPOINTER_H_
2#define SOURCEMETA_CORE_JSONPOINTER_H_
3
4#ifndef SOURCEMETA_CORE_JSONPOINTER_EXPORT
5#include <sourcemeta/core/jsonpointer_export.h>
6#endif
7
8#include <sourcemeta/core/json.h>
9#include <sourcemeta/core/uri.h>
10
11// NOLINTBEGIN(misc-include-cleaner)
12#include <sourcemeta/core/jsonpointer_error.h>
13#include <sourcemeta/core/jsonpointer_pointer.h>
14#include <sourcemeta/core/jsonpointer_position.h>
15#include <sourcemeta/core/jsonpointer_walker.h>
16// NOLINTEND(misc-include-cleaner)
17
18#include <cassert> // assert
19#include <concepts> // std::same_as
20#include <functional> // std::reference_wrapper
21#include <memory> // std::allocator
22#include <optional> // std::optional
23#include <ostream> // std::basic_ostream
24#include <string> // std::basic_string
25#include <string_view> // std::string_view
26#include <type_traits> // std::remove_const_t
27
36
37namespace sourcemeta::core {
38
42
47 // We use this instead of a string view as the latter occupies more memory
48 std::reference_wrapper<const std::string>, PropertyHashJSON<JSON::String>>;
49
53
57
78SOURCEMETA_CORE_JSONPOINTER_EXPORT
79auto get(const JSON &document, const Pointer::Token &token) -> const JSON &;
80
102SOURCEMETA_CORE_JSONPOINTER_EXPORT
103auto get(const JSON &document, const WeakPointer::Token &token) -> const JSON &;
104
125SOURCEMETA_CORE_JSONPOINTER_EXPORT
126auto get(JSON &document, const Pointer::Token &token) -> JSON &;
127
149SOURCEMETA_CORE_JSONPOINTER_EXPORT
150auto get(JSON &document, const WeakPointer::Token &token) -> JSON &;
151
171SOURCEMETA_CORE_JSONPOINTER_EXPORT
172auto try_get(const JSON &document, const Pointer::Token &token) -> const JSON *;
173
192SOURCEMETA_CORE_JSONPOINTER_EXPORT
193auto try_get(const JSON &document, const WeakPointer::Token &token)
194 -> const JSON *;
195
216template <typename V, typename PointerT>
217 requires std::same_as<std::remove_const_t<V>, JSON> &&
218 std::same_as<typename PointerT::Token::Value, JSON>
219auto get(V &document, const PointerT &pointer) -> V & {
220 V *current{&document};
221
222 // Evaluation begins with a reference to the root value of the document and
223 // every reference token is evaluated sequentially from there
224 // See https://www.rfc-editor.org/rfc/rfc6901#section-4
225 for (const auto &token : pointer) {
226 if (token.is_property()) {
227 current = &current->at(token.to_property(), token.property_hash());
228 } else if (current->is_object()) {
229 // A token holding an index still names an object member when the
230 // referenced value is an object, as the token is compared against the
231 // member name
232 // See https://www.rfc-editor.org/rfc/rfc6901#section-4
233 current = &get(*current, token);
234 } else {
235 current = &current->at(token.to_index());
236 }
237 }
238
239 return *current;
240}
241
242// Constant reference parameters can accept xvalues which will be destructed
243// after the call. When the function returns such a parameter also as constant
244// reference, then the returned reference can be used after the object it refers
245// to has been destroyed.
246// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/return-const-ref-from-parameter.html
247// These overloads avoid mis-uses of retuning const reference parameter as
248// constant reference.
249auto get(JSON &&document, const Pointer &pointer) -> const JSON & = delete;
250auto get(JSON &&document, const WeakPointer &pointer) -> const JSON & = delete;
251
269template <typename PointerT>
270 requires std::same_as<typename PointerT::Token::Value, JSON>
271auto try_get(const JSON &document, const PointerT &pointer) -> const JSON * {
272 const JSON *current{&document};
273
274 for (const auto &token : pointer) {
275 const auto type{current->type()};
276 const auto is_object{type == JSON::Type::Object};
277
278 if (token.is_property()) {
279 if (!is_object) {
280 return nullptr;
281 }
282
283 const auto *json_value{
284 current->try_at(token.to_property(), token.property_hash())};
285 if (json_value) {
286 current = json_value;
287 } else {
288 return nullptr;
289 }
290 } else if (type != JSON::Type::Array && !is_object) {
291 return nullptr;
292 } else if (is_object) {
293 const auto *json_value{try_get(*current, token)};
294 if (json_value) {
295 current = json_value;
296 } else {
297 return nullptr;
298 }
299 } else {
300 const auto index{token.to_index()};
301 if (index < current->size()) {
302 current = &current->at(index);
303 } else {
304 return nullptr;
305 }
306 }
307 }
308
309 return current;
310}
311
335SOURCEMETA_CORE_JSONPOINTER_EXPORT
336auto set(JSON &document, const Pointer &pointer, const JSON &value) -> void;
337
360SOURCEMETA_CORE_JSONPOINTER_EXPORT
361auto set(JSON &document, const Pointer &pointer, JSON &&value) -> void;
362
385SOURCEMETA_CORE_JSONPOINTER_EXPORT
386auto remove(JSON &document, const Pointer &pointer) -> bool;
387
411SOURCEMETA_CORE_JSONPOINTER_EXPORT
412auto remove(JSON &document, const WeakPointer &pointer) -> bool;
413
428SOURCEMETA_CORE_JSONPOINTER_EXPORT
429auto to_pointer(const JSON &document) -> Pointer;
430
442SOURCEMETA_CORE_JSONPOINTER_EXPORT
444 const std::basic_string_view<JSON::Char, JSON::CharTraits> input)
445 -> Pointer;
446
464SOURCEMETA_CORE_JSONPOINTER_EXPORT
465auto fragment_to_pointer(const URI &uri) -> std::optional<Pointer>;
466
482SOURCEMETA_CORE_JSONPOINTER_EXPORT
483auto to_pointer(const WeakPointer &pointer) -> Pointer;
484
497SOURCEMETA_CORE_JSONPOINTER_EXPORT
498auto is_pointer(std::string_view input) noexcept -> bool;
499
517SOURCEMETA_CORE_JSONPOINTER_EXPORT
518auto is_relative_pointer(std::string_view input) noexcept -> bool;
519
537SOURCEMETA_CORE_JSONPOINTER_EXPORT
538auto to_weak_pointer(const Pointer &pointer) -> WeakPointer;
539
555SOURCEMETA_CORE_JSONPOINTER_EXPORT
556auto stringify(const Pointer &pointer,
557 std::basic_ostream<JSON::Char, JSON::CharTraits> &stream)
558 -> void;
559
576SOURCEMETA_CORE_JSONPOINTER_EXPORT
577auto stringify(const WeakPointer &pointer,
578 std::basic_ostream<JSON::Char, JSON::CharTraits> &stream)
579 -> void;
580
594SOURCEMETA_CORE_JSONPOINTER_EXPORT
595auto to_string(const Pointer &pointer)
596 -> std::basic_string<JSON::Char, JSON::CharTraits,
597 std::allocator<JSON::Char>>;
598
614SOURCEMETA_CORE_JSONPOINTER_EXPORT
615auto to_string(const WeakPointer &pointer)
616 -> std::basic_string<JSON::Char, JSON::CharTraits,
617 std::allocator<JSON::Char>>;
618
635SOURCEMETA_CORE_JSONPOINTER_EXPORT
636auto to_uri(const Pointer &pointer) -> URI;
637
655SOURCEMETA_CORE_JSONPOINTER_EXPORT
656auto to_uri(const Pointer &pointer, const URI &base) -> URI;
657
660SOURCEMETA_CORE_JSONPOINTER_EXPORT
661auto to_uri(const WeakPointer &pointer) -> URI;
662
666SOURCEMETA_CORE_JSONPOINTER_EXPORT
667auto to_uri(const WeakPointer &pointer, const URI &base) -> URI;
668
672SOURCEMETA_CORE_JSONPOINTER_EXPORT
673auto to_uri(const WeakPointer &pointer, const std::string_view base) -> URI;
674
705
706} // namespace sourcemeta::core
707
708#endif
SOURCEMETA_CORE_DIFF_EXPORT auto stringify(const Diff &document, std::ostream &stream, const Diff::Format format, const Diff::FormatOptions &options={}) -> void
SOURCEMETA_FORCEINLINE auto type() const noexcept -> Type
Definition json_value.h:660
char Char
The character type used by the JSON document.
Definition json_value.h:42
std::char_traits< Char > CharTraits
The character traits used by the JSON document.
Definition json_value.h:44
@ Array
The JSON array type.
Definition json_value.h:81
@ Object
The JSON object type.
Definition json_value.h:83
SOURCEMETA_FORCEINLINE auto at(const Array::size_type index) const -> const JSON &
Definition json_value.h:954
SOURCEMETA_FORCEINLINE auto try_at(const String &key) const -> const JSON *
Definition json_value.h:1419
Definition json_value.h:39
Definition json_hash.h:38
GenericToken< JSON::String, PropertyHashJSON< JSON::String > > Token
Definition jsonpointer_pointer.h:28
Definition jsonpointer_pointer.h:25
Definition jsonpointer_walker.h:16
SOURCEMETA_CORE_JSONPOINTER_EXPORT auto set(JSON &document, const Pointer &pointer, const JSON &value) -> void
SOURCEMETA_CORE_JSONPOINTER_EXPORT auto to_uri(const Pointer &pointer) -> URI
GenericPointerWalker< WeakPointer > PointerWalker
Definition jsonpointer.h:704
SOURCEMETA_CORE_JSONPOINTER_EXPORT auto remove(JSON &document, const Pointer &pointer) -> bool
const WeakPointer EMPTY_WEAK_POINTER
Definition jsonpointer.h:56
SOURCEMETA_CORE_JSONPOINTER_EXPORT auto is_relative_pointer(std::string_view input) noexcept -> bool
SOURCEMETA_CORE_JSONPOINTER_EXPORT auto try_get(const JSON &document, const Pointer::Token &token) -> const JSON *
GenericPointer< JSON::String, PropertyHashJSON< JSON::String > > Pointer
Definition jsonpointer.h:41
SOURCEMETA_CORE_JSONPOINTER_EXPORT auto fragment_to_pointer(const URI &uri) -> std::optional< Pointer >
SOURCEMETA_CORE_JSONPOINTER_EXPORT auto to_pointer(const JSON &document) -> Pointer
SOURCEMETA_CORE_JSONPOINTER_EXPORT auto to_weak_pointer(const Pointer &pointer) -> WeakPointer
SOURCEMETA_CORE_JSONPOINTER_EXPORT auto to_string(const Pointer &pointer) -> std::basic_string< JSON::Char, JSON::CharTraits, std::allocator< JSON::Char > >
GenericPointer< std::reference_wrapper< const std::string >, PropertyHashJSON< JSON::String > > WeakPointer
Definition jsonpointer.h:46
SOURCEMETA_CORE_JSONPOINTER_EXPORT auto is_pointer(std::string_view input) noexcept -> bool
const Pointer EMPTY_POINTER
Definition jsonpointer.h:52
SOURCEMETA_CORE_JSONPOINTER_EXPORT auto get(const JSON &document, const Pointer::Token &token) -> const JSON &
Definition uri.h:44