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/noncopyable.h>
35 #include <cxxtools/jsonformatter.h>
36 #include <sstream>
37 #include <stdexcept>
38 
39 namespace cxxtools
40 {
41  class JsonSerializer : private NonCopyable
42  {
43  public:
45  : _ts(0),
46  _inObject(false)
47  {
48  }
49 
50  explicit JsonSerializer(std::basic_ostream<cxxtools::Char>& ts)
51  : _ts(0),
52  _inObject(false)
53  {
54  _formatter.begin(ts);
55  }
56 
57  explicit JsonSerializer(std::ostream& os,
59 
61  {
62  delete _ts;
63  }
64 
65  JsonSerializer& begin(std::basic_ostream<cxxtools::Char>& ts)
66  {
67  _formatter.begin(ts);
68  return *this;
69  }
70 
71  JsonSerializer& begin(std::ostream& os,
73 
74  void finish()
75  {
76  if (_inObject)
77  {
78  _formatter.finishObject();
79  _inObject = false;
80  }
81  _formatter.finish();
82  if (_ts)
83  _ts->flush();
84  }
85 
86  template <typename T>
87  JsonSerializer& serialize(const T& v, const std::string& name)
88  {
89  Decomposer<T> s;
90  s.begin(v);
91  s.setName(name);
92 
93  if (!_inObject)
94  {
95  _formatter.beginObject(std::string(), std::string());
96  _inObject = true;
97  }
98 
99  s.format(_formatter);
100  return *this;
101  }
102 
103  template <typename T>
105  {
106  if (_inObject)
107  throw std::logic_error("can't serialize object without name into another object");
108 
109  Decomposer<T> s;
110  s.begin(v);
111  s.format(_formatter);
112  _ts->flush();
113  return *this;
114  }
115 
116  void setObject()
117  {
118  _formatter.beginObject(std::string(), std::string());
119  _inObject = true;
120  }
121 
122  bool object() const { return _inObject; }
123 
124  bool beautify() const { return _formatter.beautify(); }
125 
126  void beautify(bool sw) { _formatter.beautify(sw); }
127 
128  template <typename T>
129  static std::string toString(const T& type, const std::string& name, bool beautify = false)
130  {
131  std::ostringstream os;
132  JsonSerializer s;
133  if (beautify)
134  s.beautify(true);
135  s.begin(os);
136  s.serialize(type, name);
137  s.finish();
138  return os.str();
139  }
140 
141  private:
142  JsonFormatter _formatter;
143  std::basic_ostream<cxxtools::Char>* _ts;
144  bool _inObject;
145  };
146 
147 }
148 
149 #endif // CXXTOOLS_JSONSERIALIZER_H
150