2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present 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/hash-map.h"
29 namespace HPHP
{ namespace JSON
{
30 ///////////////////////////////////////////////////////////////////////////////
32 template <typename T
> struct _OutputStream
;
33 template <typename T
> struct _MapStream
;
34 template <typename T
> struct _ListStream
;
35 template <typename T
> struct _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
);
47 std::string
Escape(const char *s
);
50 struct _ISerializable
{
51 virtual ~_ISerializable() {}
54 * Generate JSON output of this data structure.
56 virtual void serialize(_OutputStream
<T
> &out
) const = 0;
60 explicit Name(const char *name
) {
61 assert(name
&& *name
);
64 explicit Name(const std::string
&name
) {
65 assert(!name
.empty());
68 const std::string
&getName() const { return m_name
; }
77 template <typename Type
>
78 struct _OutputStream
{
79 explicit _OutputStream(std::ostream
&out
) : m_out(out
) {}
81 _OutputStream
&operator<< (unsigned int v
) { m_out
<< v
; return *this; }
83 _OutputStream
&operator<< (int v
) { m_out
<< v
; return *this; }
85 _OutputStream
&operator<< (bool v
) {
86 m_out
<< (v
? "true" : "false");
90 _OutputStream
&operator<< (const char *v
) {
91 m_out
<< "\"" << Escape(v
) << "\"";
95 _OutputStream
&operator<< (const std::string
&v
) {
96 m_out
<< "\"" << Escape(v
.c_str()) << "\"";
100 _OutputStream
&operator<< (const Name
&n
) {
101 m_out
<< "\"" << n
.getName() << "\":";
105 _OutputStream
& operator<<(const Null
& /*n*/) {
110 _OutputStream
&operator<< (const _ISerializable
<Type
> &v
) {
116 _OutputStream
&operator<< (const std::shared_ptr
<T
> &v
) {
126 _OutputStream
&operator<< (const std::vector
<T
> &v
) {
128 for (unsigned int i
= 0; i
< v
.size(); i
++) {
129 if (i
> 0) m_out
<< ',';
137 _OutputStream
&operator<< (const std::set
<T
> &v
) {
152 // TODO: std::map and std::unordered_map should share
153 // the same function...
155 template<typename K
, typename T
, typename C
>
156 _OutputStream
&operator<< (const std::map
<K
, T
, C
> &v
) {
158 for (auto iter
= v
.begin(); iter
!= v
.end(); ++iter
) {
159 if (iter
!= v
.begin()) m_out
<< ',';
160 *this << Name(iter
->first
);
161 *this << iter
->second
;
167 template<typename K
, typename T
, typename H
, typename E
>
168 _OutputStream
&operator<< (const hphp_hash_map
<K
, T
, H
, E
> &v
) {
170 for (auto iter
= v
.begin(); iter
!= v
.end(); ++iter
) {
171 if (iter
!= v
.begin()) m_out
<< ',';
172 *this << Name(iter
->first
);
173 *this << iter
->second
;
182 std::ostream
&raw() { return m_out
;}
184 friend struct _MapStream
<Type
>;
185 friend struct _ListStream
<Type
>;
188 template <typename Type
>
190 explicit _MapStream(_OutputStream
<Type
> &jout
)
191 : m_out(jout
.raw()), m_jout(jout
), m_first(true) {}
194 _MapStream
&add(const std::string
&n
, T v
) {
200 _MapStream
&add(const std::string
&n
) {
214 _OutputStream
<Type
> &m_jout
;
217 void init(const std::string
&n
) {
228 template <typename Type
>
230 explicit _ListStream(_OutputStream
<Type
> &jout
)
231 : m_out(jout
.raw()), m_jout(jout
), m_first(true) {}
243 _ListStream
&operator<< (T
&v
) {
258 _OutputStream
<Type
> &m_jout
;
262 ///////////////////////////////////////////////////////////////////////////////
265 #endif // incl_HPHP_JSON_H_