OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cpp/src/util/json.h" | 5 // Use "base/basictypes.h" instead. |
| 6 #define I18N_ADDRESSINPUT_UTIL_BASICTYPES_H_ |
| 7 |
| 8 // Use "base/memory/scoped_ptr.h" instead. |
| 9 #define I18N_ADDRESSINPUT_UTIL_SCOPED_PTR_H_ |
| 10 |
| 11 #include <map> |
| 12 #include <utility> |
6 | 13 |
7 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
8 #include "base/json/json_reader.h" | 15 #include "base/json/json_reader.h" |
9 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 // Must be before json.h to be used in the Json class. |
10 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/stl_util.h" |
11 #include "base/values.h" | 20 #include "base/values.h" |
| 21 #include "third_party/libaddressinput/src/cpp/src/util/json.h" |
12 | 22 |
13 namespace i18n { | 23 namespace i18n { |
14 namespace addressinput { | 24 namespace addressinput { |
15 | 25 |
16 namespace { | 26 class Json::JsonImpl { |
17 | |
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 { | |
21 public: | 27 public: |
22 virtual bool GetStringValueForKey(const std::string& key, std::string* value) | 28 explicit JsonImpl(scoped_ptr<const base::DictionaryValue> to_take_ownership) |
23 const OVERRIDE; | 29 : owned_(to_take_ownership.Pass()), not_owned_(owned_.get()) { |
24 virtual bool GetJsonValueForKey(const std::string& key, | 30 DCHECK(not_owned_); |
25 scoped_ptr<Json>* value) const OVERRIDE; | 31 InitKeys(); |
26 protected: | |
27 ChromeJson() {} | |
28 virtual ~ChromeJson() {} | |
29 | |
30 virtual const base::DictionaryValue* GetDict() const = 0; | |
31 | |
32 DISALLOW_COPY_AND_ASSIGN(ChromeJson); | |
33 }; | |
34 | |
35 // A Json object that will parse a string and own the parsed data. | |
36 class JsonDataOwner : public ChromeJson { | |
37 public: | |
38 JsonDataOwner() {} | |
39 virtual ~JsonDataOwner() {} | |
40 | |
41 virtual bool ParseObject(const std::string& json) OVERRIDE { | |
42 dict_.reset(); | |
43 | |
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())); | |
49 | |
50 return !!dict_; | |
51 } | 32 } |
52 | 33 |
53 protected: | 34 explicit JsonImpl(const base::DictionaryValue* do_not_take_ownership) |
54 virtual const base::DictionaryValue* GetDict() const OVERRIDE { | 35 : not_owned_(do_not_take_ownership) { |
55 return dict_.get(); | 36 DCHECK(not_owned_); |
| 37 InitKeys(); |
| 38 } |
| 39 |
| 40 ~JsonImpl() { |
| 41 STLDeleteValues(&dictionaries_); |
| 42 } |
| 43 |
| 44 const std::vector<std::string>& GetKeys() const { |
| 45 return keys_; |
| 46 } |
| 47 |
| 48 bool GetStringValueForKey(const std::string& key, std::string* value) const { |
| 49 return not_owned_->GetStringWithoutPathExpansion(key, value); |
| 50 } |
| 51 |
| 52 bool GetDictionaryValueForKey(const std::string& key, |
| 53 const base::DictionaryValue** value) const { |
| 54 return not_owned_->GetDictionaryWithoutPathExpansion(key, value); |
| 55 } |
| 56 |
| 57 // The caller does not own the result. The result can be NULL if there's no |
| 58 // dictionary for |key|. |
| 59 const Json* FindDictionary(const std::string& key) const { |
| 60 std::map<std::string, const Json*>::const_iterator it = |
| 61 dictionaries_.find(key); |
| 62 return it != dictionaries_.end() ? it->second : NULL; |
| 63 } |
| 64 |
| 65 // Takes ownership of |dictionary|. Should be called only once per |key| and |
| 66 // per |dictionary|. |
| 67 void AddDictionary(const std::string& key, const Json* dictionary) { |
| 68 bool inserted = |
| 69 dictionaries_.insert(std::make_pair(key, dictionary)).second; |
| 70 // Cannot do work inside of DCHECK(), because the compiler can optimize it |
| 71 // away. |
| 72 DCHECK(inserted); |
| 73 // Avoid unused variable warning when DCHECK() is optimized away. |
| 74 (void)inserted; |
56 } | 75 } |
57 | 76 |
58 private: | 77 private: |
59 scoped_ptr<base::DictionaryValue> dict_; | 78 void InitKeys() { |
| 79 DCHECK(keys_.empty()); |
| 80 for (base::DictionaryValue::Iterator it(*not_owned_); !it.IsAtEnd(); |
| 81 it.Advance()) { |
| 82 keys_.push_back(it.key()); |
| 83 } |
| 84 } |
60 | 85 |
61 DISALLOW_COPY_AND_ASSIGN(JsonDataOwner); | 86 const scoped_ptr<const base::DictionaryValue> owned_; |
| 87 const base::DictionaryValue* const not_owned_; |
| 88 |
| 89 // Owned JSON objects. |
| 90 std::map<std::string, const Json*> dictionaries_; |
| 91 |
| 92 std::vector<std::string> keys_; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(JsonImpl); |
62 }; | 95 }; |
63 | 96 |
64 // A Json object which will point to data that's been parsed by a different | 97 Json::Json() {} |
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 { | |
68 public: | |
69 explicit JsonDataCopy(const base::DictionaryValue* dict) : | |
70 dict_(dict) {} | |
71 virtual ~JsonDataCopy() {} | |
72 | |
73 virtual bool ParseObject(const std::string& json) OVERRIDE { | |
74 NOTREACHED(); | |
75 return false; | |
76 } | |
77 | |
78 protected: | |
79 virtual const base::DictionaryValue* GetDict() const OVERRIDE { | |
80 return dict_; | |
81 } | |
82 | |
83 private: | |
84 const base::DictionaryValue* dict_; // weak reference. | |
85 | |
86 DISALLOW_COPY_AND_ASSIGN(JsonDataCopy); | |
87 }; | |
88 | |
89 // ChromeJson ------------------------------------------------------------------ | |
90 | |
91 bool ChromeJson::GetStringValueForKey(const std::string& key, | |
92 std::string* value) const { | |
93 return GetDict()->GetStringWithoutPathExpansion(key, value); | |
94 } | |
95 | |
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) || | |
100 !sub_dict) { | |
101 return false; | |
102 } | |
103 | |
104 if (value) | |
105 value->reset(new JsonDataCopy(sub_dict)); | |
106 | |
107 return true; | |
108 } | |
109 | |
110 } // namespace | |
111 | 98 |
112 Json::~Json() {} | 99 Json::~Json() {} |
113 | 100 |
114 // static | 101 bool Json::ParseObject(const std::string& json) { |
115 scoped_ptr<Json> Json::Build() { | 102 DCHECK(!impl_); |
116 return scoped_ptr<Json>(new JsonDataOwner); | 103 |
| 104 // |json| is converted to a |c_str()| here because rapidjson and other parts |
| 105 // of the standalone library use char* rather than std::string. |
| 106 scoped_ptr<base::Value> parsed(base::JSONReader::Read(json.c_str())); |
| 107 if (parsed && parsed->IsType(base::Value::TYPE_DICTIONARY)) { |
| 108 impl_.reset( |
| 109 new JsonImpl(static_cast<base::DictionaryValue*>(parsed.release()))); |
| 110 } |
| 111 |
| 112 return !!impl_; |
117 } | 113 } |
118 | 114 |
119 Json::Json() {} | 115 const std::vector<std::string>& Json::GetKeys() const { |
| 116 DCHECK(impl_); |
| 117 return impl_->GetKeys(); |
| 118 } |
| 119 |
| 120 bool Json::HasStringValueForKey(const std::string& key) const { |
| 121 DCHECK(impl_); |
| 122 std::string not_used; |
| 123 return impl_->GetStringValueForKey(key, ¬_used); |
| 124 } |
| 125 |
| 126 std::string Json::GetStringValueForKey(const std::string& key) const { |
| 127 DCHECK(impl_); |
| 128 std::string value; |
| 129 bool found = impl_->GetStringValueForKey(key, &value); |
| 130 // Do not work inside of a DCHECK(), because it can be optimized away. |
| 131 DCHECK(found); |
| 132 // Avoid a compiler warning about unused variable when DCHECK() is optimized |
| 133 // away. |
| 134 (void)found; |
| 135 return value; |
| 136 } |
| 137 |
| 138 bool Json::HasDictionaryValueForKey(const std::string& key) const { |
| 139 DCHECK(impl_); |
| 140 |
| 141 // The value returned by FindDictionary() is owned by impl_. |
| 142 if (impl_->FindDictionary(key)) |
| 143 return true; |
| 144 |
| 145 // Value is owned by impl_. |
| 146 const base::DictionaryValue* value = NULL; |
| 147 return impl_->GetDictionaryValueForKey(key, &value) && value; |
| 148 } |
| 149 |
| 150 const Json& Json::GetDictionaryValueForKey(const std::string& key) const { |
| 151 DCHECK(impl_); |
| 152 |
| 153 // The value returned by FindDictionary() is owned by impl_. |
| 154 const Json* existing_dictionary = impl_->FindDictionary(key); |
| 155 if (existing_dictionary) |
| 156 return *existing_dictionary; |
| 157 |
| 158 // Value is owned by impl_. |
| 159 const base::DictionaryValue* value = NULL; |
| 160 bool found = impl_->GetDictionaryValueForKey(key, &value); |
| 161 // Cannot do work inside of DCHECK(), because it can be optimized by the |
| 162 // compiler. |
| 163 DCHECK(found); |
| 164 // Avoid a compiler warning about an unused variable when the DCHECK() is |
| 165 // optimized away. |
| 166 (void)found; |
| 167 DCHECK(value); |
| 168 |
| 169 // Dictionary is owned by impl_. |
| 170 Json* dictionary = new Json; |
| 171 dictionary->impl_.reset(new JsonImpl(value)); |
| 172 impl_->AddDictionary(key, dictionary); |
| 173 return *dictionary; |
| 174 } |
120 | 175 |
121 } // namespace addressinput | 176 } // namespace addressinput |
122 } // namespace i18n | 177 } // namespace i18n |
OLD | NEW |