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 "cpp/src/util/json.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 | 12 |
13 namespace i18n { | 13 namespace i18n { |
14 namespace addressinput { | 14 namespace addressinput { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // A base class for Chrome Json objects. Json gets parsed into a | |
Dan Beam
2014/02/01 01:02:36
nit: s/Json/JSON/g
Evan Stade
2014/02/01 01:11:12
Done.
| |
19 // base::DictionaryValue and data is accessed via the Json interface. | |
18 class ChromeJson : public Json { | 20 class ChromeJson : public Json { |
19 public: | 21 public: |
22 virtual bool GetStringValueForKey(const std::string& key, std::string* value) | |
23 const OVERRIDE; | |
24 virtual bool GetJsonValueForKey(const std::string& key, | |
25 scoped_ptr<Json>* value) const OVERRIDE; | |
26 protected: | |
20 ChromeJson() {} | 27 ChromeJson() {} |
28 virtual ~ChromeJson() {} | |
21 | 29 |
22 virtual ~ChromeJson() {} | 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() {} | |
23 | 40 |
24 virtual bool ParseObject(const std::string& json) OVERRIDE { | 41 virtual bool ParseObject(const std::string& json) OVERRIDE { |
25 dict_.reset(); | 42 dict_.reset(); |
26 | 43 |
27 // |json| is converted to a |c_str()| here because rapidjson and other parts | 44 // |json| is converted to a |c_str()| here because rapidjson and other parts |
28 // of the standalone library use char* rather than std::string. | 45 // of the standalone library use char* rather than std::string. |
29 scoped_ptr<base::Value> parsed(base::JSONReader::Read(json.c_str())); | 46 scoped_ptr<base::Value> parsed(base::JSONReader::Read(json.c_str())); |
30 if (parsed && parsed->IsType(base::Value::TYPE_DICTIONARY)) | 47 if (parsed && parsed->IsType(base::Value::TYPE_DICTIONARY)) |
31 dict_.reset(static_cast<base::DictionaryValue*>(parsed.release())); | 48 dict_.reset(static_cast<base::DictionaryValue*>(parsed.release())); |
32 | 49 |
33 return !!dict_; | 50 return !!dict_; |
34 } | 51 } |
35 | 52 |
36 virtual bool GetStringValueForKey(const std::string& key, std::string* value) | 53 protected: |
37 const OVERRIDE { | 54 virtual const base::DictionaryValue* GetDict() const OVERRIDE { |
38 DCHECK(dict_); | 55 return dict_.get(); |
39 return dict_->GetStringWithoutPathExpansion(key, value); | |
40 } | |
41 | |
42 virtual bool GetJsonValueForKey(const std::string& key, | |
43 scoped_ptr<Json>* value) const OVERRIDE { | |
44 DCHECK(dict_); | |
45 base::DictionaryValue* sub_dict = NULL; // Owned by |dict_|. | |
46 if (!dict_->GetDictionaryWithoutPathExpansion(key, &sub_dict) || !sub_dict) | |
47 return false; | |
48 | |
49 if (value) { | |
50 value->reset(new ChromeJson( | |
51 scoped_ptr<base::DictionaryValue>(sub_dict->DeepCopy()))); | |
52 } | |
53 | |
54 return true; | |
55 } | 56 } |
56 | 57 |
57 private: | 58 private: |
58 explicit ChromeJson(scoped_ptr<base::DictionaryValue> dict) | |
59 : dict_(dict.Pass()) {} | |
60 | |
61 scoped_ptr<base::DictionaryValue> dict_; | 59 scoped_ptr<base::DictionaryValue> dict_; |
62 | 60 |
63 DISALLOW_COPY_AND_ASSIGN(ChromeJson); | 61 DISALLOW_COPY_AND_ASSIGN(JsonDataOwner); |
64 }; | 62 }; |
65 | 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 DCHECK(GetDict()); | |
Dan Beam
2014/02/01 01:02:36
nit: remove if it'll just explode in the next line
Evan Stade
2014/02/01 01:11:12
Done.
| |
94 return GetDict()->GetStringWithoutPathExpansion(key, value); | |
95 } | |
96 | |
97 bool ChromeJson::GetJsonValueForKey(const std::string& key, | |
98 scoped_ptr<Json>* value) const { | |
99 DCHECK(GetDict()); | |
Dan Beam
2014/02/01 01:02:36
same
Evan Stade
2014/02/01 01:11:12
Done.
| |
100 const base::DictionaryValue* sub_dict = NULL; | |
101 if (!GetDict()->GetDictionaryWithoutPathExpansion(key, &sub_dict) || | |
102 !sub_dict) { | |
103 return false; | |
104 } | |
105 | |
106 if (value) | |
107 value->reset(new JsonDataCopy(sub_dict)); | |
108 | |
109 return true; | |
110 } | |
111 | |
66 } // namespace | 112 } // namespace |
67 | 113 |
68 Json::~Json() {} | 114 Json::~Json() {} |
69 | 115 |
70 // static | 116 // static |
71 scoped_ptr<Json> Json::Build() { | 117 scoped_ptr<Json> Json::Build() { |
72 return scoped_ptr<Json>(new ChromeJson); | 118 return scoped_ptr<Json>(new JsonDataOwner); |
73 } | 119 } |
74 | 120 |
75 Json::Json() {} | 121 Json::Json() {} |
76 | 122 |
77 } // namespace addressinput | 123 } // namespace addressinput |
78 } // namespace i18n | 124 } // namespace i18n |
OLD | NEW |