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__
39 virtual void run(yajl_gen_t* handle) const = 0;
42 struct NullGen : public Gen
44 virtual void run(yajl_gen_t* handle) const;
47 class BoolGen : public Gen
50 explicit BoolGen(bool value) : m_value(value) {}
51 virtual void run(yajl_gen_t* handle) const;
56 class StringGen : public Gen
59 explicit StringGen(const std::string& value) : m_value(value) {}
60 virtual void run(yajl_gen_t* handle) const;
65 class NumberGen : public Gen
68 explicit NumberGen(const std::string& value) : m_value(value) {}
70 explicit NumberGen(const T& value) : m_value(std::to_string(value)) {}
71 virtual void run(yajl_gen_t* handle) const;
76 class MapGen : public Gen
82 for (Value::iterator it = m_value.begin(); it != m_value.end(); ++it)
83 if (it->second.second)
84 delete it->second.first;
86 MapGen& add(const std::string& key, Gen* value) { m_value[key] = std::make_pair(value, true); return *this; }
87 MapGen& add(const std::string& key, Gen& value) { m_value[key] = std::make_pair(&value, false); return *this; }
88 virtual void run(yajl_gen_t* handle) const;
90 typedef std::pair<Gen*, bool> SmartGen;
91 typedef std::map<std::string, SmartGen> Value;
95 class ArrayGen : public Gen
101 for (Value::iterator it = m_value.begin(); it != m_value.end(); ++it)
105 void add(Gen* value) { m_value.push_back(std::make_pair(value, true)); }
106 void add(Gen& value) { m_value.push_back(std::make_pair(&value, false)); }
107 virtual void run(yajl_gen_t* handle) const;
109 typedef std::pair<Gen*, bool> SmartGen;
110 typedef std::vector<SmartGen> Value;
114 typedef bool (*Callback)(void* /*data*/, const char* /*buf*/, size_t /*size*/);
115 bool generate(Gen& gen, Callback callback, void* data);