/usr/include/Wt/Dbo/Json is in libwtdbo-dev 3.3.3+dfsg-4.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | // This may look like C code, but it's really -*- C++ -*-
/*
* Copyright (C) 2014 Emweb bvba, Leuven, Belgium.
*
* See the LICENSE file for terms of use.
*/
#ifndef WT_DBO_JSON_H_
#define WT_DBO_JSON_H_
#include <ostream>
#include <sstream>
#include <Wt/Dbo/ptr>
#include <Wt/Dbo/weak_ptr>
#include <Wt/Dbo/collection>
#include <Wt/Dbo/Field>
#include <Wt/Dbo/Session>
#include <boost/type_traits/is_enum.hpp>
#include <boost/utility/enable_if.hpp>
namespace Wt {
namespace Dbo {
namespace Wt {
class EscapeOStream;
}
/*! \class JsonSerializer Wt/Dbo/Json Wt/Dbo/Json
* \brief An action to serialize objects to JSON.
*
* This class is an \p Action that serializes objects to an ostream.
* These objects must implement the \p persist() method. It also has
* support for serializing \link ptr ptrs\endlink to these objects,
* std::vectors of \link ptr ptrs\endlink, and
* \link collection collections\endlink of \link ptr ptrs\endlink.
*
* It will follow one-to-one and \link Wt::Dbo::ManyToOne ManyToOne\endlink
* relations in one way: weak_ptr and \ref collection fields are followed
* and serialized, for \ref ptr fields only the \link Wt::Dbo::ptr::id() id\endlink
* is output.
*
* No extraneous whitespace is output.
*
* \ingroup dbo
*/
class WTDBO_API JsonSerializer
{
public:
/*! \brief Creates a JsonSerializer that writes to an std::ostream.
*
* Note that the std::ostream is not flushed to automatically. The
* flush will happen automatically when this JsonSerializer is
* destructed.
*/
JsonSerializer(std::ostream& out);
/*! \brief Destructor
*/
virtual ~JsonSerializer();
Session *session() { return session_; }
template<typename T>
typename boost::disable_if< boost::is_enum<T>, void>::type
act(FieldRef<T> field);
template<typename T>
typename boost::enable_if< boost::is_enum<T>, void>::type
act(FieldRef<T> field) {
writeFieldName(field.name());
out(static_cast<int>(field.value()));
}
void act(FieldRef<std::string> field);
void act(FieldRef<int> field);
void act(FieldRef<long long> field);
void act(FieldRef<bool> field);
template<typename T>
void actId(T& value, const std::string& name, int size) {
field(*this, value, name, size);
}
template<typename T>
void actId(ptr<T>& value, const std::string& name, int size, int fkConstraints) {
field(*this, value, name, size);
}
template<typename T>
void actPtr(const PtrRef<T>& field) {
writeFieldName(field.name());
if (field.value())
outputId(field.id());
else
out("null");
}
template<typename T>
void actWeakPtr(const WeakPtrRef<T>& field) {
writeFieldName(session_->tableName<T>() + std::string("_") + field.joinName());
ptr<T> v = field.value().query();
if (v) {
serialize(v);
} else {
out("null");
}
}
template<typename T>
void actCollection(const CollectionRef<T>& collec) {
if (collec.type() == ManyToOne) {
collection<ptr<T> > c = collec.value();
writeFieldName(session_->tableName<T>() + std::string("s_") + collec.joinName());
out('[');
bool first = true;
for (typename collection<ptr<T> >::const_iterator i = c.begin(); i != c.end(); ++i) {
if (first)
first = false;
else
out(',');
serialize(*i);
}
out(']');
}
}
bool getsValue() const {
return true;
}
bool setsValue() const {
return false;
}
bool isSchema() const {
return false;
}
/*! \brief Serialize the given object.
*
* Serializes a plain object that implements the \p persist() method.
*/
template<typename T>
void serialize(const T& t) {
session_ = NULL;
out('{');
const_cast<T&>(t).persist(*this);
out('}');
}
/*! \brief Serialize the object that is pointed to by the given \ref ptr.
*
* This method does the same as the plain object serializer, but
* also adds an extra \link Wt::Dbo::ptr::id() id\endlink field.
*/
template<typename T>
void serialize(const ptr<T>& t) {
session_ = t.session();
out('{');
first_ = true;
if (dbo_traits<T>::surrogateIdField()) {
out('"');
out(dbo_traits<T>::surrogateIdField());
out("\":");
outputId(t.id());
first_ = false;
}
const_cast<T&>(*t).persist(*this);
out('}');
}
/*! \brief Serialize an std::vector of \link ptr ptrs\endlink.
*
* Serializes each \ref ptr in the vector individually,
* and puts it in an \p Array.
*/
template<typename T>
void serialize(const std::vector<ptr<T> >& v) {
out('[');
for (typename std::vector<ptr<T> >::const_iterator i = v.begin(); i != v.end(); ++i) {
if (i != v.begin())
out(',');
else
session_ = (*i).session();
serialize(*i);
}
out(']');
}
/*! \brief Serialize a \ref collection of \link ptr ptrs\endlink.
*
* Serializes each \ref ptr in the \ref collection individually,
* and puts it in an \p Array.
*
* The typical usage scenario of this method is to serialize
* the results of a query to JSON.
*/
template<typename T>
void serialize(const collection<ptr<T> >& c) {
session_ = c.session();
out('[');
bool first = true;
for (typename collection<ptr<T> >::const_iterator i = c.begin(); i != c.end(); ++i) {
if (first)
first = false;
else
out(',');
serialize(*i);
}
out(']');
session_ = NULL;
}
private:
std::ostream &out_;
Wt::EscapeOStream *escapeOut_, *stringLiteral_;
bool first_;
Session *session_;
void out(char);
void out(const char *);
void out(int);
void out(long long);
template<typename T>
void outputId(T id) {
std::stringstream ss;
ss << id;
fastJsStringLiteral(ss.str());
}
void outputId(long long id) {
out(id);
}
void writeFieldName(const std::string& fieldName);
void fastJsStringLiteral(const std::string& s);
};
/*! \brief Serialize the given object to the given ostream.
*
* \sa JsonSerializer::serialize()
*/
template<typename C>
void jsonSerialize(const C& c, std::ostream& out) {
JsonSerializer serializer(out);
serializer.serialize(c);
}
/*! \brief Serialize the object pointed to by the given \ref ptr
* to the given ostream.
*
* \sa JsonSerializer::serialize()
*/
template<typename C>
void jsonSerialize(const ptr<C>& c, std::ostream& out) {
JsonSerializer serializer(out);
serializer.serialize(c);
}
/*! \brief Serialize a vector of \link ptr ptrs\endlink to the given
* ostream.
*
* \sa JsonSerializer::serialize()
*/
template<typename C>
void jsonSerialize(const std::vector<ptr<C> >& v, std::ostream& out) {
JsonSerializer serializer(out);
serializer.serialize(v);
}
/*! \brief Serialize a \ref collection of \link ptr ptrs\endlink to the given
* ostream.
*
* \sa JsonSerializer::serialize()
*/
template<typename C>
void jsonSerialize(const collection<C>& c, std::ostream& out) {
JsonSerializer serializer(out);
serializer.serialize(c);
}
}
}
#endif // WT_DBO_JSON_H_
|