1#ifndef SOURCEMETA_CORE_JSONPOINTER_H_
2#define SOURCEMETA_CORE_JSONPOINTER_H_
4#ifndef SOURCEMETA_CORE_JSONPOINTER_EXPORT
5#include <sourcemeta/core/jsonpointer_export.h>
8#include <sourcemeta/core/json.h>
9#include <sourcemeta/core/uri.h>
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>
37namespace sourcemeta::core {
78SOURCEMETA_CORE_JSONPOINTER_EXPORT
102SOURCEMETA_CORE_JSONPOINTER_EXPORT
125SOURCEMETA_CORE_JSONPOINTER_EXPORT
149SOURCEMETA_CORE_JSONPOINTER_EXPORT
171SOURCEMETA_CORE_JSONPOINTER_EXPORT
192SOURCEMETA_CORE_JSONPOINTER_EXPORT
216template <
typename V,
typename Po
interT>
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};
225 for (
const auto &token : pointer) {
226 if (token.is_property()) {
227 current = ¤t->at(token.to_property(), token.property_hash());
228 }
else if (current->is_object()) {
233 current = &
get(*current, token);
235 current = ¤t->at(token.to_index());
249auto get(JSON &&document,
const Pointer &pointer) ->
const JSON & =
delete;
250auto get(JSON &&document,
const WeakPointer &pointer) ->
const JSON & =
delete;
269template <
typename Po
interT>
270 requires std::same_as<typename PointerT::Token::Value, JSON>
272 const JSON *current{&document};
274 for (
const auto &token : pointer) {
275 const auto type{current->
type()};
278 if (token.is_property()) {
283 const auto *json_value{
284 current->
try_at(token.to_property(), token.property_hash())};
286 current = json_value;
292 }
else if (is_object) {
293 const auto *json_value{
try_get(*current, token)};
295 current = json_value;
300 const auto index{token.to_index()};
301 if (index < current->size()) {
302 current = ¤t->
at(index);
335SOURCEMETA_CORE_JSONPOINTER_EXPORT
360SOURCEMETA_CORE_JSONPOINTER_EXPORT
385SOURCEMETA_CORE_JSONPOINTER_EXPORT
411SOURCEMETA_CORE_JSONPOINTER_EXPORT
428SOURCEMETA_CORE_JSONPOINTER_EXPORT
442SOURCEMETA_CORE_JSONPOINTER_EXPORT
444 const std::basic_string_view<JSON::Char, JSON::CharTraits> input)
464SOURCEMETA_CORE_JSONPOINTER_EXPORT
482SOURCEMETA_CORE_JSONPOINTER_EXPORT
497SOURCEMETA_CORE_JSONPOINTER_EXPORT
517SOURCEMETA_CORE_JSONPOINTER_EXPORT
537SOURCEMETA_CORE_JSONPOINTER_EXPORT
555SOURCEMETA_CORE_JSONPOINTER_EXPORT
557 std::basic_ostream<JSON::Char, JSON::CharTraits> &stream)
576SOURCEMETA_CORE_JSONPOINTER_EXPORT
578 std::basic_ostream<JSON::Char, JSON::CharTraits> &stream)
594SOURCEMETA_CORE_JSONPOINTER_EXPORT
597 std::allocator<JSON::Char>>;
614SOURCEMETA_CORE_JSONPOINTER_EXPORT
617 std::allocator<JSON::Char>>;
635SOURCEMETA_CORE_JSONPOINTER_EXPORT
655SOURCEMETA_CORE_JSONPOINTER_EXPORT
660SOURCEMETA_CORE_JSONPOINTER_EXPORT
666SOURCEMETA_CORE_JSONPOINTER_EXPORT
672SOURCEMETA_CORE_JSONPOINTER_EXPORT
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
GenericToken< JSON::String, PropertyHashJSON< JSON::String > > Token
Definition jsonpointer_pointer.h:28
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 &