Fawkes API  Fawkes Development Version
rest_array.h
1 
2 /***************************************************************************
3  * reply.h - Web request reply
4  *
5  * Created: Fri Mar 16 17:39:54 2018
6  * Copyright 2006-2018 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #ifndef _LIBS_WEBVIEW_REST_ARRAY_H_
24 #define _LIBS_WEBVIEW_REST_ARRAY_H_
25 
26 #include <boost/property_tree/json_parser.hpp>
27 #include <boost/property_tree/ptree.hpp>
28 #include <sstream>
29 #include <string>
30 
31 /** Container to return array via REST.
32  * @author Tim Niemueller
33  */
34 template <class M>
36 {
37 public:
38  /** Empty array constructor. */
40  {
41  }
42  /** Constructor.
43  * @param items vector of values to copy
44  */
45  WebviewRestArray(std::vector<M> &items) : items_(items)
46  {
47  }
48 
49  /** Constructor.
50  * @param items vector of values to move to this array.
51  */
52  WebviewRestArray(std::vector<M> &&items) : items_(std::move(items))
53  {
54  }
55 
56  /** Render object to JSON.
57  * @param pretty true to enable pretty printing (readable spacing)
58  * @return JSON string
59  */
60  std::string
61  to_json(bool pretty = false) const
62  {
63  std::string rv = "[\n";
64  for (size_t i = 0; i < items_.size(); ++i) {
65  rv += items_[i].to_json(pretty);
66  if (i < items_.size() - 1) {
67  rv += ",";
68  }
69  }
70  rv += "]";
71  return rv;
72  }
73 
74  /** Retrieve data from JSON string.
75  * @param json JSON representation suitable for this object.
76  * Will allow partial assignment and not validate automaticaly.
77  * @see validate()
78  */
79  void
80  from_json(const std::string &json)
81  {
82  std::stringstream ss(json);
83  boost::property_tree::ptree pt;
84  boost::property_tree::read_json(ss, pt);
85  for (auto &c : pt.get_child("")) {
86  std::stringstream os;
87  boost::property_tree::write_json(os, c.second);
88  M m;
89  m.from_json(os.str());
90  items_.push_back(std::move(m));
91  }
92  }
93 
94  /** Validate if all required fields have been set.
95  * @param subcall true if this is called from another class, e.g.,
96  * a sub-class or array holder. Will modify the kind of exception thrown.
97  * @exception std::vector<std::string> thrown if required information is
98  * missing and @p subcall is set to true. Contains a list of missing fields.
99  * @exception std::runtime_error informative message describing the missing
100  * fields
101  */
102  void
103  validate(bool subcall = false)
104  {
105  for (const auto &i : items_) {
106  i.validate(subcall);
107  }
108  }
109 
110  /** Accessor for items.
111  * @return item vector
112  */
113  std::vector<M> &
115  {
116  return items_;
117  }
118 
119  /** Add item at the back of the container.
120  * @param m element to copy
121  */
122  void
123  push_back(M &m)
124  {
125  items_.push_back(m);
126  }
127 
128  /** Add item at the back of the container.
129  * @param m element to move
130  */
131  void
132  push_back(M &&m)
133  {
134  items_.push_back(std::move(m));
135  }
136 
137 private:
138  std::vector<M> items_;
139 };
140 
141 #endif
Container to return array via REST.
Definition: rest_array.h:36
void from_json(const std::string &json)
Retrieve data from JSON string.
Definition: rest_array.h:80
void push_back(M &m)
Add item at the back of the container.
Definition: rest_array.h:123
void validate(bool subcall=false)
Validate if all required fields have been set.
Definition: rest_array.h:103
WebviewRestArray(std::vector< M > &items)
Constructor.
Definition: rest_array.h:45
WebviewRestArray(std::vector< M > &&items)
Constructor.
Definition: rest_array.h:52
WebviewRestArray()
Empty array constructor.
Definition: rest_array.h:39
void push_back(M &&m)
Add item at the back of the container.
Definition: rest_array.h:132
std::string to_json(bool pretty=false) const
Render object to JSON.
Definition: rest_array.h:61
std::vector< M > & items()
Accessor for items.
Definition: rest_array.h:114