json.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 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_JSON_H
30 #define CXXTOOLS_JSON_H
31 
34 #include <iostream>
35 
36 namespace cxxtools
37 {
55  template <typename ObjectType>
57  {
58  const ObjectType& _constObject;
59  bool _beautify;
60  bool _plainkey;
61 
62  public:
65  explicit JsonOObject(const ObjectType& object, bool beautify = false)
66  : _constObject(object),
67  _beautify(beautify),
68  _plainkey(false)
69  { }
70 
75  { _beautify = sw; return *this; }
76 
77  bool beautify() const
78  { return _beautify; }
79 
81  { _plainkey = sw; return *this; }
82 
83  bool plainkey() const
84  { return _plainkey; }
85 
86  const ObjectType& object() const
87  { return _constObject; }
88  };
89 
91  template <typename CharType, typename ObjectType>
92  std::basic_ostream<CharType>& operator<< (std::basic_ostream<CharType>& out, const JsonOObject<ObjectType>& object)
93  {
94  try
95  {
96  JsonSerializer serializer(out);
97  serializer.beautify(object.beautify());
98  serializer.plainkey(object.plainkey());
99  serializer.serialize(object.object())
100  .finish();
101  }
102  catch (const std::exception&)
103  {
104  out.setstate(std::ios::failbit);
105  throw;
106  }
107  return out;
108  }
109 
113  template <typename ObjectType>
114  JsonOObject<ObjectType> Json(const ObjectType& object, bool beautify = false)
115  {
116  return JsonOObject<ObjectType>(object, beautify);
117  }
118 
141  template <typename ObjectType>
142  class JsonIOObject : public JsonOObject<ObjectType>
143  {
144  ObjectType& _object;
145 
146  public:
147  explicit JsonIOObject(ObjectType& object)
148  : JsonOObject<ObjectType>(object),
149  _object(object)
150  { }
151 
152  ObjectType& object()
153  { return _object; }
154  };
155 
157  template <typename CharType, typename ObjectType>
158  std::basic_istream<CharType>& operator>> (std::basic_istream<CharType>& in, JsonIOObject<ObjectType> object)
159  {
160  try
161  {
162  JsonDeserializer deserializer(in);
163  deserializer.deserialize(object.object());
164  }
165  catch (const std::exception&)
166  {
167  in.setstate(std::ios::failbit);
168  throw;
169  }
170  return in;
171  }
172 
175  template <typename ObjectType>
176  JsonIOObject<ObjectType> Json(ObjectType& object)
177  {
178  return JsonIOObject<ObjectType>(object);
179  }
180 
181 }
182 
183 #endif // CXXTOOLS_JSON_H
184