jsonserializer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef CXXTOOLS_JSONSERIALIZER_H
30 #define CXXTOOLS_JSONSERIALIZER_H
31 
32 #include <cxxtools/textstream.h>
33 #include <cxxtools/decomposer.h>
34 #include <cxxtools/jsonformatter.h>
35 #include <sstream>
36 #include <stdexcept>
37 
38 namespace cxxtools
39 {
102  {
103  // make non copyable
104  JsonSerializer(const JsonSerializer&) { }
105  JsonSerializer& operator=(const JsonSerializer&) { return *this; }
106 
107  public:
109  : _ts(0),
110  _inObject(false)
111  {
112  }
113 
114  explicit JsonSerializer(std::basic_ostream<cxxtools::Char>& ts)
115  : _ts(0),
116  _inObject(false)
117  {
118  _formatter.begin(ts);
119  }
120 
121  explicit JsonSerializer(std::ostream& os,
123 
125  {
126  delete _ts;
127  }
128 
129  JsonSerializer& begin(std::basic_ostream<cxxtools::Char>& ts)
130  {
131  _formatter.begin(ts);
132  return *this;
133  }
134 
135  JsonSerializer& begin(std::ostream& os,
137 
138  void finish()
139  {
140  if (_inObject)
141  {
142  _formatter.finishObject();
143  _inObject = false;
144  }
145  _formatter.finish();
146  if (_ts)
147  _ts->flush();
148  }
149 
150  template <typename T>
151  JsonSerializer& serialize(const T& v, const std::string& name)
152  {
153  Decomposer<T> s;
154  s.begin(v);
155  s.setName(name);
156 
157  if (!_inObject)
158  {
159  _formatter.beginObject(std::string(), std::string());
160  _inObject = true;
161  }
162 
163  s.format(_formatter);
164  return *this;
165  }
166 
167  template <typename T>
169  {
170  if (_inObject)
171  throw std::logic_error("can't serialize object without name into another object");
172 
173  Decomposer<T> s;
174  s.begin(v);
175  s.format(_formatter);
176  _ts->flush();
177  return *this;
178  }
179 
180  void setObject()
181  {
182  _formatter.beginObject(std::string(), std::string());
183  _inObject = true;
184  }
185 
186  bool object() const { return _inObject; }
187 
188  bool beautify() const { return _formatter.beautify(); }
189 
190  void beautify(bool sw) { _formatter.beautify(sw); }
191 
192  bool plainkey() const { return _formatter.plainkey(); }
193 
194  void plainkey(bool sw) { _formatter.plainkey(sw); }
195 
196  template <typename T>
197  static std::string toString(const T& type, const std::string& name, bool beautify = false)
198  {
199  std::ostringstream os;
200  JsonSerializer s;
201  if (beautify)
202  s.beautify(true);
203  s.begin(os);
204  s.serialize(type, name);
205  s.finish();
206  return os.str();
207  }
208 
209  private:
210  JsonFormatter _formatter;
211  std::basic_ostream<cxxtools::Char>* _ts;
212  bool _inObject;
213  };
214 
215 }
216 
217 #endif // CXXTOOLS_JSONSERIALIZER_H
218