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 #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> |
Evan Stade
2014/07/07 21:59:08
remove this comment
please use gerrit instead
2014/07/08 22:47:59
Done.
| |
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) |
Evan Stade
2014/07/07 21:59:08
why not a const ref?
please use gerrit instead
2014/07/08 22:47:59
Done.
| |
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 { return keys_; } | |
42 | |
43 bool GetStringValueForKey(const std::string& key, std::string* value) const { | |
44 return not_owned_->GetStringWithoutPathExpansion(key, value); | |
45 } | |
46 | |
47 bool GetDictionaryValueForKey(const std::string& key, | |
48 const base::DictionaryValue** value) const { | |
49 return not_owned_->GetDictionaryWithoutPathExpansion(key, value); | |
50 } | |
51 | |
52 // The caller does not own the result. The result can be NULL if there's no | |
53 // dictionary for |key|. | |
54 const Json* FindDictionary(const std::string& key) const { | |
55 std::map<std::string, const Json*>::const_iterator it = | |
56 dictionaries_.find(key); | |
57 return it != dictionaries_.end() ? it->second : NULL; | |
58 } | |
59 | |
60 // Takes ownership of |dictionary|. Should be called only once per |key| and | |
61 // per |dictionary|. | |
62 void AddDictionary(const std::string& key, const Json* dictionary) { | |
63 bool inserted = | |
64 dictionaries_.insert(std::make_pair(key, dictionary)).second; | |
Evan Stade
2014/07/07 21:59:08
this whole function can be distilled down to:
if
please use gerrit instead
2014/07/08 22:47:59
This function is no longer necessary after moving
| |
65 // Cannot do work inside of DCHECK(), because the compiler can optimize it | |
66 // away. | |
67 DCHECK(inserted); | |
68 // Avoid unused variable warning when DCHECK() is optimized away. | |
69 (void)inserted; | |
56 } | 70 } |
57 | 71 |
58 private: | 72 private: |
59 scoped_ptr<base::DictionaryValue> dict_; | 73 void InitKeys() { |
74 DCHECK(keys_.empty()); | |
75 for (base::DictionaryValue::Iterator it(*not_owned_); !it.IsAtEnd(); | |
76 it.Advance()) { | |
77 keys_.push_back(it.key()); | |
78 } | |
79 } | |
60 | 80 |
61 DISALLOW_COPY_AND_ASSIGN(JsonDataOwner); | 81 const ::scoped_ptr<const base::DictionaryValue> owned_; |
82 const base::DictionaryValue* const not_owned_; | |
Evan Stade
2014/07/07 21:59:07
why not a const ref?
please use gerrit instead
2014/07/08 22:47:59
Done.
| |
83 | |
84 // Owned JSON objects. | |
85 std::map<std::string, const Json*> dictionaries_; | |
86 | |
87 std::vector<std::string> keys_; | |
88 | |
89 DISALLOW_COPY_AND_ASSIGN(JsonImpl); | |
62 }; | 90 }; |
63 | 91 |
64 // A Json object which will point to data that's been parsed by a different | 92 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 | 93 |
112 Json::~Json() {} | 94 Json::~Json() {} |
113 | 95 |
114 // static | 96 bool Json::ParseObject(const std::string& json) { |
115 scoped_ptr<Json> Json::Build() { | 97 DCHECK(!impl_); |
116 return scoped_ptr<Json>(new JsonDataOwner); | 98 |
99 // |json| is converted to a |c_str()| here because rapidjson and other parts | |
100 // of the standalone library use char* rather than std::string. | |
101 ::scoped_ptr<const base::Value> parsed(base::JSONReader::Read(json.c_str())); | |
102 if (parsed && parsed->IsType(base::Value::TYPE_DICTIONARY)) { | |
103 ::scoped_ptr<const base::DictionaryValue> dict( | |
104 static_cast<const base::DictionaryValue*>(parsed.release())); | |
105 impl_.reset(new JsonImpl(dict.Pass())); | |
106 } | |
107 | |
108 return !!impl_; | |
117 } | 109 } |
118 | 110 |
119 Json::Json() {} | 111 const std::vector<std::string>& Json::GetKeys() const { |
112 DCHECK(impl_); | |
113 return impl_->GetKeys(); | |
114 } | |
115 | |
116 bool Json::HasStringValueForKey(const std::string& key) const { | |
117 DCHECK(impl_); | |
118 std::string not_used; | |
119 return impl_->GetStringValueForKey(key, ¬_used); | |
120 } | |
121 | |
122 std::string Json::GetStringValueForKey(const std::string& key) const { | |
123 DCHECK(impl_); | |
124 std::string value; | |
125 bool found = impl_->GetStringValueForKey(key, &value); | |
Evan Stade
2014/07/07 21:59:08
ditto
please use gerrit instead
2014/07/08 22:47:59
Done.
| |
126 // Do not work inside of a DCHECK(), because it can be optimized away. | |
127 DCHECK(found); | |
128 // Avoid a compiler warning about unused variable when DCHECK() is optimized | |
129 // away. | |
130 (void)found; | |
131 return value; | |
132 } | |
133 | |
134 bool Json::HasDictionaryValueForKey(const std::string& key) const { | |
135 DCHECK(impl_); | |
136 | |
137 // The value returned by FindDictionary() is owned by impl_. | |
138 if (impl_->FindDictionary(key)) | |
139 return true; | |
140 | |
141 // Value is owned by impl_. | |
142 const base::DictionaryValue* value = NULL; | |
143 return impl_->GetDictionaryValueForKey(key, &value) && value; | |
144 } | |
145 | |
146 const Json& Json::GetDictionaryValueForKey(const std::string& key) const { | |
147 DCHECK(impl_); | |
148 | |
149 // The value returned by FindDictionary() is owned by impl_. | |
Evan Stade
2014/07/07 21:59:08
I think it would be a bit less confusing if all th
please use gerrit instead
2014/07/08 22:47:59
Done.
| |
150 const Json* existing_dictionary = impl_->FindDictionary(key); | |
151 if (existing_dictionary) | |
152 return *existing_dictionary; | |
153 | |
154 // Value is owned by impl_. | |
155 const base::DictionaryValue* value = NULL; | |
156 bool found = impl_->GetDictionaryValueForKey(key, &value); | |
Evan Stade
2014/07/07 21:59:08
ditto
please use gerrit instead
2014/07/08 22:47:59
Done.
| |
157 // Cannot do work inside of DCHECK(), because it can be optimized by the | |
158 // compiler. | |
159 DCHECK(found); | |
160 // Avoid a compiler warning about an unused variable when the DCHECK() is | |
161 // optimized away. | |
162 (void)found; | |
163 DCHECK(value); | |
164 | |
165 // Dictionary is owned by impl_. | |
166 Json* dictionary = new Json; | |
167 dictionary->impl_.reset(new JsonImpl(value)); | |
168 impl_->AddDictionary(key, dictionary); | |
169 return *dictionary; | |
170 } | |
120 | 171 |
121 } // namespace addressinput | 172 } // namespace addressinput |
122 } // namespace i18n | 173 } // namespace i18n |
OLD | NEW |