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 *******************************************************************************/
32 #include <yajl/yajl_parse.h>
34 #include "Deserializer.hh"
36 #include "Serializable.hh"
42 class Deserializer::Impl
45 Impl(const std::string
& filePath
, bool comments
= false,
48 std::ifstream
* input
= new std::ifstream(filePath
.c_str());
49 init(*input
, true, comments
, check
);
52 Impl(std::istream
& input
, bool comments
= false,
55 init(input
, false, comments
, check
);
60 while (!parsed
.empty())
62 delete parsed
.front();
75 unsigned char* errorStr
= yajl_get_error(hand
, 0, 0, 0);
76 logError("parser error: %s", errorStr
);
77 yajl_free_error(hand
, errorStr
);
79 throw Exception(Exception::PARSING_FAILED
);
83 static int parsedNull(void* ctx
)
85 ((Impl
*)ctx
)->parsed
.push(new SerializableNull
);
89 static int parsedBoolean(void* ctx
, int value
)
91 ((Impl
*)ctx
)->parsed
.push(new SerializableBasic
<Serializable::Boolean
>(value
));
95 static int parsedInteger(void* ctx
, long value
)
97 ((Impl
*)ctx
)->parsed
.push(new SerializableBasic
<Serializable::Integer
>(value
));
101 static int parsedFloat(void* ctx
, double value
)
103 ((Impl
*)ctx
)->parsed
.push(new SerializableBasic
<Serializable::Float
>(value
));
107 static int parsedString(void* ctx
, const unsigned char* value
,
110 ((Impl
*)ctx
)->parsed
.push(new SerializableBasic
<Serializable::String
>(std::string((char*)value
, length
)));
114 static int parsedBeginMap(void* ctx
)
116 ((Impl
*)ctx
)->parsed
.push(new SerializableBasic
<Serializable::Map
>);
120 static int parsedMapKey(void* ctx
, const unsigned char* value
,
123 // same thing as a string
124 return parsedString(ctx
, value
, length
);
127 static int parsedEndMap(void* ctx
)
129 // null means the end of a structure
130 ((Impl
*)ctx
)->parsed
.push(0);
134 static int parsedBeginArray(void* ctx
)
136 ((Impl
*)ctx
)->parsed
.push(new SerializableBasic
<Serializable::Array
>);
140 static int parsedEndArray(void* ctx
)
142 // null means the end of a structure
143 ((Impl
*)ctx
)->parsed
.push(0);
150 unsigned char buffer
[4096];
154 while (parsed
.empty() && in
->good())
156 in
->read((char*)buffer
, sizeof(buffer
));
157 unsigned readIn
= in
->gcount();
161 stat
= yajl_parse(hand
, buffer
, readIn
);
165 stat
= yajl_parse_complete(hand
);
168 if (stat
!= yajl_status_ok
&&
169 stat
!= yajl_status_insufficient_data
)
182 std::queue
<Serializable
*> parsed
;
185 void init(std::istream
& input
, bool deleteIn
, bool comments
, bool check
)
187 // this has to be static because yajl actually does not copy it into its
188 // internal data structures but rather keeps a pointer to this
189 static const yajl_callbacks callbacks
=
197 Impl::parsedBeginMap
,
200 Impl::parsedBeginArray
,
205 deleteWhenDone
= deleteIn
;
207 yajl_parser_config config
= {comments
, check
};
208 hand
= yajl_alloc(&callbacks
, &config
, NULL
, this);
213 Deserializer::Deserializer(const std::string
& filePath
, bool comments
,
216 impl_(new Deserializer::Impl(filePath
, comments
, check
)) {}
218 Deserializer::Deserializer(std::istream
& input
, bool comments
, bool check
) :
220 impl_(new Deserializer::Impl(input
, comments
, check
)) {}
223 SerializableP
Deserializer::deserialize()
225 Serializable
* ptr
= pullNext();
228 ptr
->deserialize(*this);
230 return SerializableP(ptr
);
234 Serializable
* Deserializer::pullNext()
237 if (!impl_
->parsed
.empty())
239 Serializable
* ptr
= impl_
->parsed
.front();
245 void Deserializer::pop()
254 /** vim: set ts=4 sw=4 tw=80: *************************************************/