2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
21 #ifndef __STG_STGLIBS_JSON_GENERATOR_H__
22 #define __STG_STGLIBS_JSON_GENERATOR_H__
29 #include <boost/scoped_ptr.hpp>
41 virtual void run(yajl_gen_t* handle) const = 0;
44 struct NullGen : public Gen
46 virtual void run(yajl_gen_t* handle) const;
49 class BoolGen : public Gen
52 BoolGen(bool value) : m_value(value) {}
53 virtual void run(yajl_gen_t* handle) const;
58 class StringGen : public Gen
61 StringGen(const std::string& value) : m_value(value) {}
62 virtual void run(yajl_gen_t* handle) const;
67 class NumberGen : public Gen
70 NumberGen(const std::string& value) : m_value(value) {}
72 NumberGen(const T& value) : m_value(x2str(value)) {}
73 virtual void run(yajl_gen_t* handle) const;
78 class MapGen : public Gen
84 for (Value::iterator it = m_value.begin(); it != m_value.end(); ++it)
85 if (it->second.second)
86 delete it->second.first;
88 MapGen& add(const std::string& key, Gen* value) { m_value[key] = std::make_pair(value, true); return *this; }
89 MapGen& add(const std::string& key, Gen& value) { m_value[key] = std::make_pair(&value, false); return *this; }
90 virtual void run(yajl_gen_t* handle) const;
92 typedef std::pair<Gen*, bool> SmartGen;
93 typedef std::map<std::string, SmartGen> Value;
97 class ArrayGen : public Gen
103 for (Value::iterator it = m_value.begin(); it != m_value.end(); ++it)
107 void add(Gen* value) { m_value.push_back(std::make_pair(value, true)); }
108 void add(Gen& value) { m_value.push_back(std::make_pair(&value, false)); }
109 virtual void run(yajl_gen_t* handle) const;
111 typedef std::pair<Gen*, bool> SmartGen;
112 typedef std::vector<SmartGen> Value;
116 typedef bool (*Callback)(void* /*data*/, const char* /*buf*/, size_t /*size*/);
117 bool generate(Gen& gen, Callback callback, void* data);