1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cpp/src/util/json.h"
7 #include "base/basictypes.h"
8 #include "base/json/json_reader.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/values.h"
14 namespace addressinput {
18 // A base class for Chrome Json objects. JSON gets parsed into a
19 // base::DictionaryValue and data is accessed via the Json interface.
20 class ChromeJson : public Json {
22 virtual bool GetStringValueForKey(const std::string& key, std::string* value)
24 virtual bool GetJsonValueForKey(const std::string& key,
25 scoped_ptr<Json>* value) const OVERRIDE;
28 virtual ~ChromeJson() {}
30 virtual const base::DictionaryValue* GetDict() const = 0;
32 DISALLOW_COPY_AND_ASSIGN(ChromeJson);
35 // A Json object that will parse a string and own the parsed data.
36 class JsonDataOwner : public ChromeJson {
39 virtual ~JsonDataOwner() {}
41 virtual bool ParseObject(const std::string& json) OVERRIDE {
44 // |json| is converted to a |c_str()| here because rapidjson and other parts
45 // of the standalone library use char* rather than std::string.
46 scoped_ptr<base::Value> parsed(base::JSONReader::Read(json.c_str()));
47 if (parsed && parsed->IsType(base::Value::TYPE_DICTIONARY))
48 dict_.reset(static_cast<base::DictionaryValue*>(parsed.release()));
54 virtual const base::DictionaryValue* GetDict() const OVERRIDE {
59 scoped_ptr<base::DictionaryValue> dict_;
61 DISALLOW_COPY_AND_ASSIGN(JsonDataOwner);
64 // A Json object which will point to data that's been parsed by a different
65 // ChromeJson. It does not own its data and is only valid as long as its parent
66 // ChromeJson is valid.
67 class JsonDataCopy : public ChromeJson {
69 explicit JsonDataCopy(const base::DictionaryValue* dict) :
71 virtual ~JsonDataCopy() {}
73 virtual bool ParseObject(const std::string& json) OVERRIDE {
79 virtual const base::DictionaryValue* GetDict() const OVERRIDE {
84 const base::DictionaryValue* dict_; // weak reference.
86 DISALLOW_COPY_AND_ASSIGN(JsonDataCopy);
89 // ChromeJson ------------------------------------------------------------------
91 bool ChromeJson::GetStringValueForKey(const std::string& key,
92 std::string* value) const {
93 return GetDict()->GetStringWithoutPathExpansion(key, value);
96 bool ChromeJson::GetJsonValueForKey(const std::string& key,
97 scoped_ptr<Json>* value) const {
98 const base::DictionaryValue* sub_dict = NULL;
99 if (!GetDict()->GetDictionaryWithoutPathExpansion(key, &sub_dict) ||
105 value->reset(new JsonDataCopy(sub_dict));
115 scoped_ptr<Json> Json::Build() {
116 return scoped_ptr<Json>(new JsonDataOwner);
121 } // namespace addressinput