2 +----------------------------------------------------------------------+
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"
25 #include "hphp/util/hash-map-typedefs.h"
28 struct AnalysisResult
;
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) \
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
);
51 class _ISerializable
{
53 virtual ~_ISerializable() {}
56 * Generate JSON output of this data structure.
58 virtual void serialize(_OutputStream
<T
> &out
) const = 0;
63 explicit Name(const char *name
) {
64 assert(name
&& *name
);
67 explicit Name(const std::string
&name
) {
68 assert(!name
.empty());
71 const std::string
&getName() const { return m_name
; }
80 template <typename Type
>
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");
95 _OutputStream
&operator<< (const char *v
) {
96 m_out
<< "\"" << Escape(v
) << "\"";
100 _OutputStream
&operator<< (const std::string
&v
) {
101 m_out
<< "\"" << Escape(v
.c_str()) << "\"";
105 _OutputStream
&operator<< (const Name
&n
) {
106 m_out
<< "\"" << n
.getName() << "\":";
110 _OutputStream
&operator<< (const Null
&n
) {
115 _OutputStream
&operator<< (const _ISerializable
<Type
> &v
) {
121 _OutputStream
&operator<< (const std::shared_ptr
<T
> &v
) {
131 _OutputStream
&operator<< (const std::vector
<T
> &v
) {
133 for (unsigned int i
= 0; i
< v
.size(); i
++) {
134 if (i
> 0) m_out
<< ',';
142 _OutputStream
&operator<< (const std::set
<T
> &v
) {
145 BOOST_FOREACH(T el
, v
) {
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
) {
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
;
173 template<typename K
, typename T
, typename C
>
174 _OutputStream
&operator<<
175 (const hphp_hash_map
<K
, T
, C
> &v
) {
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
;
187 std::shared_ptr
<AnalysisResult
> analysisResult() const { return m_ar
; }
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
>
202 explicit _MapStream(_OutputStream
<Type
> &jout
)
203 : m_out(jout
.raw()), m_jout(jout
), m_first(true) {}
206 _MapStream
&add(const std::string
&n
, T v
) {
212 _MapStream
&add(const std::string
&n
) {
226 _OutputStream
<Type
> &m_jout
;
229 void init(const std::string
&n
) {
240 template <typename Type
>
243 explicit _ListStream(_OutputStream
<Type
> &jout
)
244 : m_out(jout
.raw()), m_jout(jout
), m_first(true) {}
256 _ListStream
&operator<< (T
&v
) {
271 _OutputStream
<Type
> &m_jout
;
275 ///////////////////////////////////////////////////////////////////////////////
278 #endif // incl_HPHP_JSON_H_