Add missing libxml constants
[hiphop-php.git] / hphp / compiler / json.h
blob397c966b68cd360e6c0347006a8fe05a3279476f
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_JSON_H_
18 #define incl_HPHP_JSON_H_
20 #include "hphp/util/deprecated/base.h"
21 #include <map>
22 #include <memory>
23 #include <set>
24 #include <vector>
25 #include "hphp/util/hash-map-typedefs.h"
27 namespace HPHP {
28 struct AnalysisResult;
29 namespace JSON {
30 ///////////////////////////////////////////////////////////////////////////////
32 template <typename T> class _OutputStream;
33 template <typename T> class _MapStream;
34 template <typename T> class _ListStream;
35 template <typename T> class _ISerializable;
37 #define DEFINE_JSON_OUTPUT_TYPE(type) \
38 struct type { \
39 typedef _OutputStream<type> OutputStream; \
40 typedef _MapStream<type> MapStream; \
41 typedef _ListStream<type> ListStream; \
42 typedef _ISerializable<type> ISerializable; \
45 DEFINE_JSON_OUTPUT_TYPE(CodeError);
46 DEFINE_JSON_OUTPUT_TYPE(DocTarget);
48 std::string Escape(const char *s);
50 template <typename T>
51 class _ISerializable {
52 public:
53 virtual ~_ISerializable() {}
55 /**
56 * Generate JSON output of this data structure.
58 virtual void serialize(_OutputStream<T> &out) const = 0;
61 class Name {
62 public:
63 explicit Name(const char *name) {
64 assert(name && *name);
65 m_name = name;
67 explicit Name(const std::string &name) {
68 assert(!name.empty());
69 m_name = name;
71 const std::string &getName() const { return m_name; }
72 private:
73 std::string m_name;
76 // struct _Null {};
77 // _Null Null;
78 enum class Null {};
80 template <typename Type>
81 class _OutputStream {
82 public:
83 _OutputStream(std::ostream &out,
84 std::shared_ptr<AnalysisResult> ar) : m_out(out), m_ar(ar) {}
86 _OutputStream &operator<< (unsigned int v) { m_out << v; return *this; }
88 _OutputStream &operator<< (int v) { m_out << v; return *this; }
90 _OutputStream &operator<< (bool v) {
91 m_out << (v ? "true" : "false");
92 return *this;
95 _OutputStream &operator<< (const char *v) {
96 m_out << "\"" << Escape(v) << "\"";
97 return *this;
100 _OutputStream &operator<< (const std::string &v) {
101 m_out << "\"" << Escape(v.c_str()) << "\"";
102 return *this;
105 _OutputStream &operator<< (const Name &n) {
106 m_out << "\"" << n.getName() << "\":";
107 return *this;
110 _OutputStream &operator<< (const Null &n) {
111 m_out << "null";
112 return *this;
115 _OutputStream &operator<< (const _ISerializable<Type> &v) {
116 v.serialize(*this);
117 return *this;
120 template<typename T>
121 _OutputStream &operator<< (const std::shared_ptr<T> &v) {
122 if (v) {
123 *this << *v;
124 } else {
125 *this << Null();
127 return *this;
130 template<typename T>
131 _OutputStream &operator<< (const std::vector<T> &v) {
132 m_out << "[";
133 for (unsigned int i = 0; i < v.size(); i++) {
134 if (i > 0) m_out << ',';
135 *this << v[i];
137 m_out << "]";
138 return *this;
141 template<typename T>
142 _OutputStream &operator<< (const std::set<T> &v) {
143 m_out << "[";
144 bool first = true;
145 BOOST_FOREACH(T el, v) {
146 if (first) {
147 first = false;
148 } else {
149 m_out << ',';
151 *this << el;
153 m_out << "]";
154 return *this;
157 // TODO: std::map and __gnu_cxx::hash_map should share
158 // the same function...
160 template<typename K, typename T, typename C>
161 _OutputStream &operator<< (const std::map<K, T, C> &v) {
162 m_out << "{";
163 for (typename std::map<K, T, C>::const_iterator iter = v.begin();
164 iter != v.end(); ++iter) {
165 if (iter != v.begin()) m_out << ',';
166 *this << Name(iter->first);
167 *this << iter->second;
169 m_out << "}\n";
170 return *this;
173 template<typename K, typename T, typename C>
174 _OutputStream &operator<<
175 (const hphp_hash_map<K, T, C> &v) {
176 m_out << "{";
177 for (typename hphp_hash_map<K, T, C>::const_iterator
178 iter = v.begin(); iter != v.end(); ++iter) {
179 if (iter != v.begin()) m_out << ',';
180 *this << Name(iter->first);
181 *this << iter->second;
183 m_out << "}\n";
184 return *this;
187 std::shared_ptr<AnalysisResult> analysisResult() const { return m_ar; }
189 private:
190 std::ostream &m_out;
191 std::shared_ptr<AnalysisResult> m_ar;
193 std::ostream &raw() { return m_out;}
195 friend class _MapStream<Type>;
196 friend class _ListStream<Type>;
199 template <typename Type>
200 class _MapStream {
201 public:
202 explicit _MapStream(_OutputStream<Type> &jout)
203 : m_out(jout.raw()), m_jout(jout), m_first(true) {}
205 template<typename T>
206 _MapStream &add(const std::string &n, T v) {
207 init(n);
208 m_jout << v;
209 return *this;
212 _MapStream &add(const std::string &n) {
213 init(n);
214 return *this;
217 void done() {
218 if (m_first) {
219 m_out << "{";
221 m_out << "}\n";
224 private:
225 std::ostream &m_out;
226 _OutputStream<Type> &m_jout;
227 bool m_first;
229 void init(const std::string &n) {
230 if (m_first) {
231 m_out << "{";
232 m_first = false;
233 } else {
234 m_out << ",";
236 m_jout << Name(n);
240 template <typename Type>
241 class _ListStream {
242 public:
243 explicit _ListStream(_OutputStream<Type> &jout)
244 : m_out(jout.raw()), m_jout(jout), m_first(true) {}
246 void next() {
247 if (m_first) {
248 m_out << "[";
249 m_first = false;
250 } else {
251 m_out << ",";
255 template<typename T>
256 _ListStream &operator<< (T &v) {
257 next();
258 m_jout << v;
259 return *this;
262 void done() {
263 if (m_first) {
264 m_out << "[";
266 m_out << "]\n";
269 private:
270 std::ostream &m_out;
271 _OutputStream<Type> &m_jout;
272 bool m_first;
275 ///////////////////////////////////////////////////////////////////////////////
278 #endif // incl_HPHP_JSON_H_