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 +----------------------------------------------------------------------+
19 #include "hphp/util/hash-map.h"
28 namespace HPHP
{ namespace JSON
{
29 ///////////////////////////////////////////////////////////////////////////////
31 template <typename T
> struct _OutputStream
;
32 template <typename T
> struct _MapStream
;
33 template <typename T
> struct _ListStream
;
34 template <typename T
> struct _ISerializable
;
36 #define DEFINE_JSON_OUTPUT_TYPE(type) \
38 typedef _OutputStream<type> OutputStream; \
39 typedef _MapStream<type> MapStream; \
40 typedef _ListStream<type> ListStream; \
41 typedef _ISerializable<type> ISerializable; \
44 DEFINE_JSON_OUTPUT_TYPE(CodeError
);
46 std::string
Escape(const char *s
);
49 struct _ISerializable
{
50 virtual ~_ISerializable() {}
53 * Generate JSON output of this data structure.
55 virtual void serialize(_OutputStream
<T
> &out
) const = 0;
59 explicit Name(const char *name
) {
60 assert(name
&& *name
);
63 explicit Name(const std::string
&name
) {
64 assert(!name
.empty());
67 const std::string
&getName() const { return m_name
; }
76 template <typename Type
>
77 struct _OutputStream
{
78 explicit _OutputStream(std::ostream
&out
) : m_out(out
) {}
80 _OutputStream
&operator<< (unsigned int v
) { m_out
<< v
; return *this; }
82 _OutputStream
&operator<< (int v
) { m_out
<< v
; return *this; }
84 _OutputStream
&operator<< (bool v
) {
85 m_out
<< (v
? "true" : "false");
89 _OutputStream
&operator<< (const char *v
) {
90 m_out
<< "\"" << Escape(v
) << "\"";
94 _OutputStream
&operator<< (const std::string
&v
) {
95 m_out
<< "\"" << Escape(v
.c_str()) << "\"";
99 _OutputStream
&operator<< (const Name
&n
) {
100 m_out
<< "\"" << n
.getName() << "\":";
104 _OutputStream
& operator<<(const Null
& /*n*/) {
109 _OutputStream
&operator<< (const _ISerializable
<Type
> &v
) {
115 _OutputStream
&operator<< (const std::shared_ptr
<T
> &v
) {
125 _OutputStream
&operator<< (const std::vector
<T
> &v
) {
127 for (unsigned int i
= 0; i
< v
.size(); i
++) {
128 if (i
> 0) m_out
<< ',';
136 _OutputStream
&operator<< (const std::set
<T
> &v
) {
151 // TODO: std::map and std::unordered_map should share
152 // the same function...
154 template<typename K
, typename T
, typename C
>
155 _OutputStream
&operator<< (const std::map
<K
, T
, C
> &v
) {
157 for (auto iter
= v
.begin(); iter
!= v
.end(); ++iter
) {
158 if (iter
!= v
.begin()) m_out
<< ',';
159 *this << Name(iter
->first
);
160 *this << iter
->second
;
166 template<typename K
, typename T
, typename H
, typename E
>
167 _OutputStream
&operator<< (const hphp_hash_map
<K
, T
, H
, E
> &v
) {
169 for (auto iter
= v
.begin(); iter
!= v
.end(); ++iter
) {
170 if (iter
!= v
.begin()) m_out
<< ',';
171 *this << Name(iter
->first
);
172 *this << iter
->second
;
181 std::ostream
&raw() { return m_out
;}
183 friend struct _MapStream
<Type
>;
184 friend struct _ListStream
<Type
>;
187 template <typename Type
>
189 explicit _MapStream(_OutputStream
<Type
> &jout
)
190 : m_out(jout
.raw()), m_jout(jout
), m_first(true) {}
193 _MapStream
&add(const std::string
&n
, T v
) {
199 _MapStream
&add(const std::string
&n
) {
213 _OutputStream
<Type
> &m_jout
;
216 void init(const std::string
&n
) {
227 template <typename Type
>
229 explicit _ListStream(_OutputStream
<Type
> &jout
)
230 : m_out(jout
.raw()), m_jout(jout
), m_first(true) {}
242 _ListStream
&operator<< (T
&v
) {
257 _OutputStream
<Type
> &m_jout
;
261 ///////////////////////////////////////////////////////////////////////////////