OLD | NEW |
1 // Copyright (C) 2013 Google Inc. | 1 // Copyright (C) 2013 Google Inc. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #ifndef I18N_ADDRESSINPUT_UTIL_JSON_H_ | 15 #ifndef I18N_ADDRESSINPUT_UTIL_JSON_H_ |
16 #define I18N_ADDRESSINPUT_UTIL_JSON_H_ | 16 #define I18N_ADDRESSINPUT_UTIL_JSON_H_ |
17 | 17 |
18 #include <libaddressinput/util/scoped_ptr.h> | 18 #include <libaddressinput/util/scoped_ptr.h> |
19 | 19 |
20 #include <string> | 20 #include <string> |
21 | 21 |
22 namespace i18n { | 22 namespace i18n { |
23 namespace addressinput { | 23 namespace addressinput { |
24 | 24 |
25 // Parses a JSON dictionary of strings. Sample usage: | 25 // Parses a JSON dictionary of strings. Sample usage: |
26 // scoped_ptr<Json> json(Json::Build()); | 26 // scoped_ptr<Json> json(Json::Build()); |
| 27 // std::string value; |
27 // if (json->ParseObject("{'key1':'value1', 'key2':'value2'}") && | 28 // if (json->ParseObject("{'key1':'value1', 'key2':'value2'}") && |
28 // json->HasStringKey("key1")) { | 29 // json->GetStringValueForKey("key1", &value)) { |
29 // Process(json->GetStringValueForKey("key1")); | 30 // Process(value); |
30 // } | 31 // } |
31 class Json { | 32 class Json { |
32 public: | 33 public: |
33 virtual ~Json(); | 34 virtual ~Json(); |
34 | 35 |
35 // Returns a new instance of |Json| object. | 36 // Returns a new instance of |Json| object. |
36 static scoped_ptr<Json> Build(); | 37 static scoped_ptr<Json> Build(); |
37 | 38 |
38 // Parses the |json| string and returns true if |json| is valid and it is an | 39 // Parses the |json| string and returns true if |json| is valid and it is an |
39 // object. | 40 // object. |
40 virtual bool ParseObject(const std::string& json) = 0; | 41 virtual bool ParseObject(const std::string& json) = 0; |
41 | 42 |
42 // Sets |value| to the string for |key| if it exists, or false if the key | 43 // Sets |value| to the string for |key| if it exists, or false if the key |
43 // doesn't exist or doesn't correspond to a string. The JSON object must be | 44 // doesn't exist or doesn't correspond to a string. The JSON object must be |
44 // parsed successfully in ParseObject() before invoking this method. | 45 // parsed successfully in ParseObject() before invoking this method. |
45 virtual bool GetStringValueForKey(const std::string& key, | 46 virtual bool GetStringValueForKey(const std::string& key, |
46 std::string* value) const = 0; | 47 std::string* value) const = 0; |
47 | 48 |
48 protected: | 49 protected: |
49 Json(); | 50 Json(); |
50 }; | 51 }; |
51 | 52 |
52 } // namespace addressinput | 53 } // namespace addressinput |
53 } // namespace i18n | 54 } // namespace i18n |
54 | 55 |
55 #endif // I18N_ADDRESSINPUT_UTIL_JSON_H_ | 56 #endif // I18N_ADDRESSINPUT_UTIL_JSON_H_ |
OLD | NEW |