#include <yajl/yajl_parse.h>
#include "Deserializer.hh"
+#include "Log.hh"
#include "Serializable.hh"
namespace Mf {
-class Deserializer::DeserializerImpl
+class Deserializer::Impl
{
public:
- DeserializerImpl(const std::string& filePath, bool comments = false,
+ Impl(const std::string& filePath, bool comments = false,
bool check = false)
{
std::ifstream* input = new std::ifstream(filePath.c_str());
init(*input, true, comments, check);
}
- DeserializerImpl(std::istream& input, bool comments = false,
+ Impl(std::istream& input, bool comments = false,
bool check = false)
{
init(input, false, comments, check);
}
- ~DeserializerImpl()
+ ~Impl()
{
while (!parsed.empty())
{
yajl_free(hand);
}
- void throwError()
+ void raise()
{
unsigned char* errorStr = yajl_get_error(hand, 0, 0, 0);
- Deserializer::Exception exception((char*)errorStr);
+ logError("parser error: %s", errorStr);
yajl_free_error(hand, errorStr);
- throw exception;
+
+ throw Exception(Exception::PARSING_FAILED);
}
static int parsedNull(void* ctx)
{
- ((DeserializerImpl*)ctx)->parsed.push(new SerializableNull);
+ ((Impl*)ctx)->parsed.push(new SerializableNull);
return 1;
}
static int parsedBoolean(void* ctx, int value)
{
- ((DeserializerImpl*)ctx)->parsed.push(new SerializableBoolean(value));
+ ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::Boolean>(value));
return 1;
}
static int parsedInteger(void* ctx, long value)
{
- ((DeserializerImpl*)ctx)->parsed.push(new SerializableInteger(value));
+ ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::Integer>(value));
return 1;
}
static int parsedFloat(void* ctx, double value)
{
- ((DeserializerImpl*)ctx)->parsed.push(new SerializableReal(value));
+ ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::Float>(value));
return 1;
}
static int parsedString(void* ctx, const unsigned char* value,
unsigned length)
{
- SerializableString* parsed =
- new SerializableString(std::string((char*)value, length));
- ((DeserializerImpl*)ctx)->parsed.push(parsed);
+ ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::String>(std::string((char*)value, length)));
return 1;
}
static int parsedBeginMap(void* ctx)
{
- ((DeserializerImpl*)ctx)->parsed.push(new SerializableMap);
+ ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::Map>);
return 1;
}
static int parsedEndMap(void* ctx)
{
- ((DeserializerImpl*)ctx)->parsed.push(0);
+ // null means the end of a structure
+ ((Impl*)ctx)->parsed.push(0);
return 1;
}
static int parsedBeginArray(void* ctx)
{
- ((DeserializerImpl*)ctx)->parsed.push(new SerializableArray);
+ ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::Array>);
return 1;
}
static int parsedEndArray(void* ctx)
{
- ((DeserializerImpl*)ctx)->parsed.push(0);
+ // null means the end of a structure
+ ((Impl*)ctx)->parsed.push(0);
return 1;
}
if (stat != yajl_status_ok &&
stat != yajl_status_insufficient_data)
{
- throwError();
+ raise();
}
}
}
// internal data structures but rather keeps a pointer to this
static const yajl_callbacks callbacks =
{
- DeserializerImpl::parsedNull,
- DeserializerImpl::parsedBoolean,
- DeserializerImpl::parsedInteger,
- DeserializerImpl::parsedFloat,
+ Impl::parsedNull,
+ Impl::parsedBoolean,
+ Impl::parsedInteger,
+ Impl::parsedFloat,
0,
- DeserializerImpl::parsedString,
- DeserializerImpl::parsedBeginMap,
- DeserializerImpl::parsedMapKey,
- DeserializerImpl::parsedEndMap,
- DeserializerImpl::parsedBeginArray,
- DeserializerImpl::parsedEndArray
+ Impl::parsedString,
+ Impl::parsedBeginMap,
+ Impl::parsedMapKey,
+ Impl::parsedEndMap,
+ Impl::parsedBeginArray,
+ Impl::parsedEndArray
};
in = &input;
Deserializer::Deserializer(const std::string& filePath, bool comments,
bool check) :
// pass through
- impl_(new Deserializer::DeserializerImpl(filePath, comments, check)) {}
+ impl_(new Deserializer::Impl(filePath, comments, check)) {}
Deserializer::Deserializer(std::istream& input, bool comments, bool check) :
// pass through
- impl_(new Deserializer::DeserializerImpl(input, comments, check)) {}
+ impl_(new Deserializer::Impl(input, comments, check)) {}
-SerializablePtr Deserializer::deserialize()
+SerializableP Deserializer::deserialize()
{
Serializable* ptr = pullNext();
if (ptr)
{
ptr->deserialize(*this);
}
- return SerializablePtr(ptr);
+ return SerializableP(ptr);
}