This source file includes following definitions.
- ParseObject
- GetDict
- ParseObject
- GetDict
- GetStringValueForKey
- GetJsonValueForKey
- Build
#include "cpp/src/util/json.h"
#include "base/basictypes.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
namespace i18n {
namespace addressinput {
namespace {
class ChromeJson : public Json {
public:
virtual bool GetStringValueForKey(const std::string& key, std::string* value)
const OVERRIDE;
virtual bool GetJsonValueForKey(const std::string& key,
scoped_ptr<Json>* value) const OVERRIDE;
protected:
ChromeJson() {}
virtual ~ChromeJson() {}
virtual const base::DictionaryValue* GetDict() const = 0;
DISALLOW_COPY_AND_ASSIGN(ChromeJson);
};
class JsonDataOwner : public ChromeJson {
public:
JsonDataOwner() {}
virtual ~JsonDataOwner() {}
virtual bool ParseObject(const std::string& json) OVERRIDE {
dict_.reset();
scoped_ptr<base::Value> parsed(base::JSONReader::Read(json.c_str()));
if (parsed && parsed->IsType(base::Value::TYPE_DICTIONARY))
dict_.reset(static_cast<base::DictionaryValue*>(parsed.release()));
return !!dict_;
}
protected:
virtual const base::DictionaryValue* GetDict() const OVERRIDE {
return dict_.get();
}
private:
scoped_ptr<base::DictionaryValue> dict_;
DISALLOW_COPY_AND_ASSIGN(JsonDataOwner);
};
class JsonDataCopy : public ChromeJson {
public:
explicit JsonDataCopy(const base::DictionaryValue* dict) :
dict_(dict) {}
virtual ~JsonDataCopy() {}
virtual bool ParseObject(const std::string& json) OVERRIDE {
NOTREACHED();
return false;
}
protected:
virtual const base::DictionaryValue* GetDict() const OVERRIDE {
return dict_;
}
private:
const base::DictionaryValue* dict_;
DISALLOW_COPY_AND_ASSIGN(JsonDataCopy);
};
bool ChromeJson::GetStringValueForKey(const std::string& key,
std::string* value) const {
return GetDict()->GetStringWithoutPathExpansion(key, value);
}
bool ChromeJson::GetJsonValueForKey(const std::string& key,
scoped_ptr<Json>* value) const {
const base::DictionaryValue* sub_dict = NULL;
if (!GetDict()->GetDictionaryWithoutPathExpansion(key, &sub_dict) ||
!sub_dict) {
return false;
}
if (value)
value->reset(new JsonDataCopy(sub_dict));
return true;
}
}
Json::~Json() {}
scoped_ptr<Json> Json::Build() {
return scoped_ptr<Json>(new JsonDataOwner);
}
Json::Json() {}
}
}