JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
path.hpp
Go to the documentation of this file.
1/// \file jsonv/path.hpp
2/// Support for [JSONPath](http://goessner.net/articles/JsonPath/).
3///
4/// Copyright (c) 2014-2020 by Travis Gockel. All rights reserved.
5///
6/// This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
7/// as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
8/// version.
9///
10/// \author Travis Gockel (travis@gockelhut.com)
11#ifndef __JSONV_PATH_HPP_INCLUDED__
12#define __JSONV_PATH_HPP_INCLUDED__
13
14#include <jsonv/config.hpp>
16#include <string_view>
17
18#include <iosfwd>
19#include <memory>
20#include <string>
21#include <vector>
22
23namespace jsonv
24{
25
26enum class path_element_kind : unsigned char
27{
28 array_index,
29 object_key,
30};
31
32JSONV_PUBLIC std::ostream& operator<<(std::ostream&, const path_element_kind&);
33
34JSONV_PUBLIC std::string to_string(const path_element_kind&);
35
37{
38public:
39 path_element(std::size_t idx);
40 path_element(int idx);
41 path_element(std::string key);
42 path_element(std::string_view key);
43 path_element(const char* key);
45 path_element& operator=(const path_element&);
46 path_element(path_element&&) noexcept;
47 path_element& operator=(path_element&&) noexcept;
48
49 ~path_element() noexcept;
50
51 path_element_kind kind() const;
52
53 std::size_t index() const;
54
55 const std::string& key() const;
56
57 bool operator==(const path_element&) const;
58 bool operator!=(const path_element&) const;
59
60private:
61 union storage
62 {
63 std::size_t index;
64 std::string key;
65
66 storage(std::size_t idx);
67 storage(std::string&& key);
68 ~storage() noexcept;
69 };
70
71private:
72 path_element_kind _kind;
73 storage _data;
74};
75
76JSONV_PUBLIC std::ostream& operator<<(std::ostream&, const path_element&);
77
78JSONV_PUBLIC std::string to_string(const path_element&);
79
80/// Represents an exact path in some JSON structure.
82 public detail::generic_container<std::vector<path_element>>
83{
84public:
85 /// Creates a new, empty path.
87
88 /// Creates a path with the provided \a elements.
89 path(storage_type elements);
90
91 /// Create a \c path from a string definition. The syntax of this is ECMAScript's syntax for selecting elements, so
92 /// <tt>path::create(".foo.bar[1]")</tt> is equivalent to <tt>path({ "foo", "bar", 1 })</tt>.
93 ///
94 /// \throws std::invalid_argument if the \a specification is not valid.
95 static path create(std::string_view specification);
96
97 path(const path&);
98 path& operator=(const path&);
101 ~path() noexcept;
102
103 /// Return a new path with the given \a subpath appended to the back.
106
107 /// Return a new path with the given \a elem appended to the back.
110};
111
112JSONV_PUBLIC std::ostream& operator<<(std::ostream&, const path&);
113
114JSONV_PUBLIC std::string to_string(const path&);
115
116}
117
118#endif/*__JSONV_PATH_HPP_INCLUDED__*/
An adapter for enumeration types.
Represents an exact path in some JSON structure.
Definition path.hpp:83
path()
Creates a new, empty path.
path(storage_type elements)
Creates a path with the provided elements.
static path create(std::string_view specification)
Create a path from a string definition.
Copyright (c) 2014-2020 by Travis Gockel.
#define JSONV_PUBLIC
This function or class is part of the public API for JSON Voorhees.
Definition config.hpp:102
Copyright (c) 2014 by Travis Gockel.
kind
Describes the kind of data a value holds.
Definition kind.hpp:31
STL namespace.