JSON Voorhees
Killer JSON for C++
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 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 **/
12 #ifndef __JSONV_PATH_HPP_INCLUDED__
13 #define __JSONV_PATH_HPP_INCLUDED__
14 
15 #include <jsonv/config.hpp>
17 #include <jsonv/string_view.hpp>
18 
19 #include <iosfwd>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 namespace jsonv
25 {
26 
27 enum class path_element_kind : unsigned char
28 {
29  array_index,
30  object_key,
31 };
32 
33 JSONV_PUBLIC std::ostream& operator<<(std::ostream&, const path_element_kind&);
34 
35 JSONV_PUBLIC std::string to_string(const path_element_kind&);
36 
38 {
39 public:
40  path_element(std::size_t idx);
41  path_element(int idx);
42  path_element(std::string key);
44  path_element(const char* key);
45  path_element(const path_element&);
46  path_element& operator=(const path_element&);
47  path_element(path_element&&) noexcept;
48  path_element& operator=(path_element&&) noexcept;
49 
50  ~path_element() noexcept;
51 
52  path_element_kind kind() const;
53 
54  std::size_t index() const;
55 
56  const std::string& key() const;
57 
58  bool operator==(const path_element&) const;
59  bool operator!=(const path_element&) const;
60 
61 private:
62  union JSONV_PUBLIC storage
63  {
64  std::size_t index;
65  std::string key;
66 
67  storage(std::size_t idx);
68  storage(std::string&& key);
69  ~storage() noexcept;
70  };
71 
72 private:
73  path_element_kind _kind;
74  storage _data;
75 };
76 
77 JSONV_PUBLIC std::ostream& operator<<(std::ostream&, const path_element&);
78 
79 JSONV_PUBLIC std::string to_string(const path_element&);
80 
81 /** Represents an exact path in some JSON structure. **/
83  public detail::generic_container<std::vector<path_element>>
84 {
85 public:
86  /** Creates a new, empty path. **/
87  path();
88 
89  /** Creates a path with the provided \a elements. **/
90  path(storage_type elements);
91 
92  /** Create a \c path from a string definition. The syntax of this is ECMAScript's syntax for selecting elements, so
93  * <tt>path::create(".foo.bar[1]")</tt> is equivalent to <tt>path({ "foo", "bar", 1 })</tt>.
94  *
95  * \throws std::invalid_argument if the \a specification is not valid.
96  **/
97  static path create(string_view specification);
98 
99  path(const path&);
100  path& operator=(const path&);
101  path(path&&) noexcept;
102  path& operator=(path&&) noexcept;
103  ~path() noexcept;
104 
105  /** Return a new path with the given \a subpath appended to the back. **/
106  path operator+(const path& subpath) const;
107  path& operator+=(const path& subpath);
108 
109  /** Return a new path with the given \a elem appended to the back. **/
110  path operator+(path_element elem) const;
111  path& operator+=(path_element elem);
112 };
113 
114 JSONV_PUBLIC std::ostream& operator<<(std::ostream&, const path&);
115 
116 JSONV_PUBLIC std::string to_string(const path&);
117 
118 }
119 
120 #endif/*__JSONV_PATH_HPP_INCLUDED__*/
Copyright (c) 2014-2019 by Travis Gockel.
Represents an exact path in some JSON structure.
Definition: path.hpp:82
JSONV_PUBLIC std::string to_string(const parse_error::problem &p)
Get a string representation of a problem.
kind
Describes the kind of data a value holds.
Definition: value.hpp:69
Pulls in an implementation of string_view.
Copyright (c) 2014 by Travis Gockel.
A generic container that exposes the traversal and modification operations externally.
#define JSONV_PUBLIC
This function or class is part of the public API for JsonVoorhees.
Definition: config.hpp:104
JSONV_STRING_VIEW_TYPE string_view
A non-owning reference to a string.
Definition: string_view.hpp:52