Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: third_party/libaddressinput/chromium/json.cc

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

Powered by Google App Engine
This is Rietveld 408576698