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 "third_party/libaddressinput/src/cpp/src/util/json.h" | 5 #include "cpp/src/util/json.h" |
6 | |
7 #include <map> | |
8 #include <utility> | |
9 | 6 |
10 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
11 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
12 #include "base/logging.h" | 9 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
14 #include "base/stl_util.h" | |
15 #include "base/values.h" | 11 #include "base/values.h" |
16 | 12 |
17 namespace i18n { | 13 namespace i18n { |
18 namespace addressinput { | 14 namespace addressinput { |
19 | 15 |
20 namespace { | 16 namespace { |
21 | 17 |
22 // Returns |json| parsed into a JSON dictionary. Sets |parser_error| to true if | 18 // A base class for Chrome Json objects. JSON gets parsed into a |
23 // parsing failed. | 19 // base::DictionaryValue and data is accessed via the Json interface. |
24 ::scoped_ptr<const base::DictionaryValue> Parse(const std::string& json, | 20 class ChromeJson : public Json { |
25 bool* parser_error) { | 21 public: |
26 DCHECK(parser_error); | 22 virtual bool GetStringValueForKey(const std::string& key, std::string* value) |
27 ::scoped_ptr<const base::DictionaryValue> result; | 23 const OVERRIDE; |
| 24 virtual bool GetJsonValueForKey(const std::string& key, |
| 25 scoped_ptr<Json>* value) const OVERRIDE; |
| 26 protected: |
| 27 ChromeJson() {} |
| 28 virtual ~ChromeJson() {} |
28 | 29 |
29 // |json| is converted to a |c_str()| here because rapidjson and other parts | 30 virtual const base::DictionaryValue* GetDict() const = 0; |
30 // of the standalone library use char* rather than std::string. | |
31 ::scoped_ptr<const base::Value> parsed(base::JSONReader::Read(json.c_str())); | |
32 *parser_error = !parsed || !parsed->IsType(base::Value::TYPE_DICTIONARY); | |
33 | 31 |
34 if (*parser_error) | 32 DISALLOW_COPY_AND_ASSIGN(ChromeJson); |
35 result.reset(new base::DictionaryValue); | 33 }; |
36 else | |
37 result.reset(static_cast<const base::DictionaryValue*>(parsed.release())); | |
38 | 34 |
39 return result.Pass(); | 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 } |
| 52 |
| 53 protected: |
| 54 virtual const base::DictionaryValue* GetDict() const OVERRIDE { |
| 55 return dict_.get(); |
| 56 } |
| 57 |
| 58 private: |
| 59 scoped_ptr<base::DictionaryValue> dict_; |
| 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(JsonDataOwner); |
| 62 }; |
| 63 |
| 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 { |
| 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); |
40 } | 94 } |
41 | 95 |
42 // Returns the list of keys in |dict|. | 96 bool ChromeJson::GetJsonValueForKey(const std::string& key, |
43 std::vector<std::string> GetKeysFromDictionary( | 97 scoped_ptr<Json>* value) const { |
44 const base::DictionaryValue& dict) { | 98 const base::DictionaryValue* sub_dict = NULL; |
45 std::vector<std::string> keys; | 99 if (!GetDict()->GetDictionaryWithoutPathExpansion(key, &sub_dict) || |
46 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) | 100 !sub_dict) { |
47 keys.push_back(it.key()); | 101 return false; |
48 return keys; | 102 } |
| 103 |
| 104 if (value) |
| 105 value->reset(new JsonDataCopy(sub_dict)); |
| 106 |
| 107 return true; |
49 } | 108 } |
50 | 109 |
51 } // namespace | 110 } // namespace |
52 | 111 |
53 // Implementation of JSON parser for libaddressinput using JSON parser in | 112 Json::~Json() {} |
54 // Chrome. | |
55 class Json::JsonImpl { | |
56 public: | |
57 explicit JsonImpl(const std::string& json) | |
58 : owned_(Parse(json, &parser_error_)), | |
59 dict_(*owned_), | |
60 keys_(GetKeysFromDictionary(dict_)) {} | |
61 | 113 |
62 ~JsonImpl() { STLDeleteValues(&sub_dicts_); } | 114 // static |
63 | 115 scoped_ptr<Json> Json::Build() { |
64 bool parser_error() const { return parser_error_; } | 116 return scoped_ptr<Json>(new JsonDataOwner); |
65 | 117 } |
66 const std::vector<std::string>& keys() const { return keys_; } | |
67 | |
68 bool GetStringValueForKey(const std::string& key, std::string* value) const { | |
69 return dict_.GetStringWithoutPathExpansion(key, value); | |
70 } | |
71 | |
72 bool HasDictionaryValueForKey(const std::string& key) { | |
73 return !!FindDictionary(key); | |
74 } | |
75 | |
76 const Json& GetDictionaryValueForKey(const std::string& key) { | |
77 const Json* result = FindDictionary(key); | |
78 DCHECK(result); | |
79 return *result; | |
80 } | |
81 | |
82 private: | |
83 explicit JsonImpl(const base::DictionaryValue& dict) | |
84 : parser_error_(false), dict_(dict), keys_(GetKeysFromDictionary(dict)) {} | |
85 | |
86 // The caller does not own the returned value, which can be NULL if there's no | |
87 // dictionary for |key|. | |
88 const Json* FindDictionary(const std::string& key) { | |
89 std::map<std::string, Json*>::const_iterator it = sub_dicts_.find(key); | |
90 if (it != sub_dicts_.end()) | |
91 return it->second; | |
92 | |
93 const base::DictionaryValue* sub_dict = NULL; | |
94 if (!dict_.GetDictionaryWithoutPathExpansion(key, &sub_dict) || !sub_dict) | |
95 return NULL; | |
96 | |
97 std::pair<std::map<std::string, Json*>::iterator, bool> result = | |
98 sub_dicts_.insert(std::make_pair(key, new Json)); | |
99 DCHECK(result.second); | |
100 | |
101 Json* sub_json = result.first->second; | |
102 sub_json->impl_.reset(new JsonImpl(*sub_dict)); | |
103 | |
104 return sub_json; | |
105 } | |
106 | |
107 const ::scoped_ptr<const base::DictionaryValue> owned_; | |
108 bool parser_error_; | |
109 const base::DictionaryValue& dict_; | |
110 const std::vector<std::string> keys_; | |
111 std::map<std::string, Json*> sub_dicts_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(JsonImpl); | |
114 }; | |
115 | 118 |
116 Json::Json() {} | 119 Json::Json() {} |
117 | 120 |
118 Json::~Json() {} | |
119 | |
120 bool Json::ParseObject(const std::string& json) { | |
121 DCHECK(!impl_); | |
122 impl_.reset(new JsonImpl(json)); | |
123 if (impl_->parser_error()) | |
124 impl_.reset(); | |
125 return !!impl_; | |
126 } | |
127 | |
128 const std::vector<std::string>& Json::GetKeys() const { | |
129 return impl_->keys(); | |
130 } | |
131 | |
132 bool Json::GetStringValueForKey(const std::string& key, | |
133 std::string* value) const { | |
134 return impl_->GetStringValueForKey(key, value); | |
135 } | |
136 | |
137 bool Json::HasDictionaryValueForKey(const std::string& key) const { | |
138 return impl_->HasDictionaryValueForKey(key); | |
139 } | |
140 | |
141 const Json& Json::GetDictionaryValueForKey(const std::string& key) const { | |
142 return impl_->GetDictionaryValueForKey(key); | |
143 } | |
144 | |
145 } // namespace addressinput | 121 } // namespace addressinput |
146 } // namespace i18n | 122 } // namespace i18n |
OLD | NEW |