This source file includes following definitions.
- ParseObject
- GetStringValueForKey
- GetJsonValueForKey
- Build
#include "json.h"
#include <libaddressinput/util/basictypes.h>
#include <libaddressinput/util/scoped_ptr.h>
#include <cassert>
#include <cstddef>
#include <cstring>
#include <string>
#include <rapidjson/document.h>
#include <rapidjson/reader.h>
namespace i18n {
namespace addressinput {
namespace {
class Rapidjson : public Json {
public:
Rapidjson() : dict_() {}
virtual ~Rapidjson() {}
virtual bool ParseObject(const std::string& json) {
scoped_ptr<rapidjson::Document> document(new rapidjson::Document);
document->Parse<rapidjson::kParseValidateEncodingFlag>(json.c_str());
if (!document->HasParseError() && document->IsObject()) {
dict_.reset(document.release());
return true;
}
return false;
}
virtual bool GetStringValueForKey(const std::string& key,
std::string* value) const {
assert(dict_ != NULL);
const rapidjson::Value::Member* member = dict_->FindMember(key.c_str());
if (member == NULL || !member->value.IsString()) {
return false;
}
if (value) {
value->assign(member->value.GetString(), member->value.GetStringLength());
}
return true;
}
virtual bool GetJsonValueForKey(const std::string& key,
scoped_ptr<Json>* value) const {
assert(dict_ != NULL);
const rapidjson::Value::Member* member = dict_->FindMember(key.c_str());
if (member == NULL || !member->value.IsObject()) {
return false;
}
if (value) {
scoped_ptr<rapidjson::Value> copy(new rapidjson::Value);
memcpy(copy.get(), &member->value, sizeof(rapidjson::Value));
value->reset(new Rapidjson(copy.Pass()));
}
return true;
}
protected:
explicit Rapidjson(scoped_ptr<rapidjson::Value> dict)
: dict_(dict.Pass()) {}
scoped_ptr<rapidjson::Value> dict_;
DISALLOW_COPY_AND_ASSIGN(Rapidjson);
};
}
Json::~Json() {}
scoped_ptr<Json> Json::Build() {
return scoped_ptr<Json>(new Rapidjson);
}
Json::Json() {}
}
}