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 namespace { |
17 | 21 |
18 // A base class for Chrome Json objects. JSON gets parsed into a | 22 // Returns |json| parsed into a JSON dictionary. Sets |parser_error| to true if |
19 // base::DictionaryValue and data is accessed via the Json interface. | 23 // parsing failed. |
20 class ChromeJson : public Json { | 24 ::scoped_ptr<const base::DictionaryValue> Parse(const std::string& json, |
21 public: | 25 bool* parser_error) { |
22 virtual bool GetStringValueForKey(const std::string& key, std::string* value) | 26 DCHECK(parser_error); |
23 const OVERRIDE; | 27 ::scoped_ptr<const base::DictionaryValue> result; |
24 virtual bool GetJsonValueForKey(const std::string& key, | |
25 scoped_ptr<Json>* value) const OVERRIDE; | |
26 protected: | |
27 ChromeJson() {} | |
28 virtual ~ChromeJson() {} | |
29 | 28 |
30 virtual const base::DictionaryValue* GetDict() const = 0; | 29 // |json| is converted to a |c_str()| here because rapidjson and other parts |
| 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); |
31 | 33 |
32 DISALLOW_COPY_AND_ASSIGN(ChromeJson); | 34 if (*parser_error) |
33 }; | 35 result.reset(new base::DictionaryValue); |
| 36 else |
| 37 result.reset(static_cast<const base::DictionaryValue*>(parsed.release())); |
34 | 38 |
35 // A Json object that will parse a string and own the parsed data. | 39 return result.Pass(); |
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); | |
94 } | 40 } |
95 | 41 |
96 bool ChromeJson::GetJsonValueForKey(const std::string& key, | 42 // Returns the list of keys in |dict|. |
97 scoped_ptr<Json>* value) const { | 43 std::vector<std::string> GetKeysFromDictionary( |
98 const base::DictionaryValue* sub_dict = NULL; | 44 const base::DictionaryValue& dict) { |
99 if (!GetDict()->GetDictionaryWithoutPathExpansion(key, &sub_dict) || | 45 std::vector<std::string> keys; |
100 !sub_dict) { | 46 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) |
101 return false; | 47 keys.push_back(it.key()); |
102 } | 48 return keys; |
103 | |
104 if (value) | |
105 value->reset(new JsonDataCopy(sub_dict)); | |
106 | |
107 return true; | |
108 } | 49 } |
109 | 50 |
110 } // namespace | 51 } // namespace |
111 | 52 |
112 Json::~Json() {} | 53 // Implementation of JSON parser for libaddressinput using JSON parser in |
| 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_)) {} |
113 | 61 |
114 // static | 62 ~JsonImpl() { STLDeleteValues(&sub_dicts_); } |
115 scoped_ptr<Json> Json::Build() { | 63 |
116 return scoped_ptr<Json>(new JsonDataOwner); | 64 bool parser_error() const { return parser_error_; } |
117 } | 65 |
| 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 }; |
118 | 115 |
119 Json::Json() {} | 116 Json::Json() {} |
120 | 117 |
| 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 |
121 } // namespace addressinput | 145 } // namespace addressinput |
122 } // namespace i18n | 146 } // namespace i18n |
OLD | NEW |