2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *******************************************************************************/
31 #include <yajl/yajl_gen.h>
33 #include "stringtools.hh"
34 #include "serializer.hh"
43 serializer_impl(const std::string
& filePath
, const std::string
& indent
= "")
45 std::ofstream
* output
= new std::ofstream(filePath
.c_str());
46 init(*output
, true, indent
);
49 serializer_impl(std::ostream
& output
, const std::string
& indent
= "")
51 init(output
, false, indent
);
63 static void throwError(yajl_gen_status err
)
67 case yajl_gen_generation_complete
:
68 throw serializer::exception("the archive has already terminated");
69 case yajl_gen_keys_must_be_strings
:
70 throw serializer::exception("map keys must be strings");
71 case yajl_max_depth_exceeded
:
72 throw serializer::exception("maximum archive depth exceeded");
73 case yajl_gen_in_error_state
:
74 throw serializer::exception("serializer already in error state");
84 void init(std::ostream
& output
, bool deleteOut
, const std::string
& indent
)
86 yajl_gen_config config
;
89 deleteWhenDone
= deleteOut
;
93 config
.beautify
= true;
94 config
.indentString
= 0;
95 // FIXME: a yajl bug prevents using heap-allocated strings
96 //config.indentString = indent.c_str();
100 config
.beautify
= false;
102 gen
= yajl_gen_alloc(&config
, 0);
107 serializer::serializer(const std::string
& filePath
, const std::string
& indent
) :
108 impl(new serializer_impl(filePath
, indent
)) {}
110 serializer::serializer(std::ostream
& output
, const std::string
& indent
) :
111 impl(new serializer_impl(output
, indent
)) {}
113 serializer::~serializer()
119 void serializer::push(long value
)
121 yajl_gen_status stat
= yajl_gen_integer(impl
->gen
, value
);
122 if (stat
!= yajl_gen_status_ok
) serializer_impl::throwError(stat
);
125 void serializer::push(double value
)
127 yajl_gen_status stat
= yajl_gen_double(impl
->gen
, value
);
128 if (stat
!= yajl_gen_status_ok
) serializer_impl::throwError(stat
);
131 void serializer::push(bool value
)
133 yajl_gen_status stat
= yajl_gen_bool(impl
->gen
, value
);
134 if (stat
!= yajl_gen_status_ok
) serializer_impl::throwError(stat
);
137 void serializer::push(const std::string
& value
)
139 yajl_gen_status stat
= yajl_gen_string(impl
->gen
,
140 (const unsigned char*)value
.c_str(), value
.length());
141 if (stat
!= yajl_gen_status_ok
) serializer_impl::throwError(stat
);
144 void serializer::push(const std::wstring
& value
)
146 push(wideToMulti(value
));
149 void serializer::pushNull()
151 yajl_gen_status stat
= yajl_gen_null(impl
->gen
);
152 if (stat
!= yajl_gen_status_ok
) serializer_impl::throwError(stat
);
156 void serializer::pushMapHead()
158 yajl_gen_status stat
= yajl_gen_map_open(impl
->gen
);
159 if (stat
!= yajl_gen_status_ok
) serializer_impl::throwError(stat
);
162 void serializer::pushMapTail()
164 yajl_gen_status stat
= yajl_gen_map_close(impl
->gen
);
165 if (stat
!= yajl_gen_status_ok
) serializer_impl::throwError(stat
);
168 void serializer::pushArrayHead()
170 yajl_gen_status stat
= yajl_gen_array_open(impl
->gen
);
171 if (stat
!= yajl_gen_status_ok
) serializer_impl::throwError(stat
);
174 void serializer::pushArrayTail()
176 yajl_gen_status stat
= yajl_gen_array_close(impl
->gen
);
177 if (stat
!= yajl_gen_status_ok
) serializer_impl::throwError(stat
);
181 void serializer::flush()
183 const unsigned char* buffer
;
186 yajl_gen_get_buf(impl
->gen
, &buffer
, &length
);
187 impl
->out
->write((const char*)buffer
, length
);
188 yajl_gen_clear(impl
->gen
);