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 // | |
5 // This class provides the same interface as ../src/cpp/src/util/json.h but with | |
6 // a Chromium-specific JSON parser. This is because it didn't make much sense to | |
7 // have 2 JSON parsers in Chrome's code base, and the standalone library opted | |
8 // to use rapidjson instead of jsoncpp. See the other file for an explanation of | |
9 // what this class does. | |
10 | 4 |
11 #ifndef THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_UTIL_JSON_H_ | 5 #include "cpp/src/util/json.h" |
12 #define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_UTIL_JSON_H_ | |
13 | |
14 #include <string> | |
15 | 6 |
16 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/json/json_reader.h" |
| 9 #include "base/logging.h" |
17 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
18 #include "base/values.h" | 11 #include "base/values.h" |
19 | 12 |
20 namespace i18n { | 13 namespace i18n { |
21 namespace addressinput { | 14 namespace addressinput { |
22 | 15 |
23 class Json { | 16 namespace { |
| 17 |
| 18 class ChromeJson : public Json { |
24 public: | 19 public: |
25 Json(); | 20 ChromeJson() {} |
26 ~Json(); | |
27 | 21 |
28 bool ParseObject(const std::string& json); | 22 virtual ~ChromeJson() {} |
29 bool HasStringValueForKey(const std::string& key) const; | 23 |
30 std::string GetStringValueForKey(const std::string& key) const; | 24 virtual bool ParseObject(const std::string& json) { |
| 25 dict_.reset(); |
| 26 |
| 27 // |json| is converted to a |c_str()| here because rapidjson and other parts |
| 28 // of the standalone library use char* rather than std::string. |
| 29 scoped_ptr<base::Value> parsed(base::JSONReader::Read(json.c_str())); |
| 30 if (parsed && parsed->IsType(base::Value::TYPE_DICTIONARY)) |
| 31 dict_.reset(static_cast<base::DictionaryValue*>(parsed.release())); |
| 32 |
| 33 return !!dict_; |
| 34 } |
| 35 |
| 36 virtual bool HasStringValueForKey(const std::string& key) const { |
| 37 base::Value* val = NULL; |
| 38 dict_->GetWithoutPathExpansion(key, &val); |
| 39 return val && val->IsType(base::Value::TYPE_STRING); |
| 40 } |
| 41 |
| 42 virtual std::string GetStringValueForKey(const std::string& key) const { |
| 43 std::string result; |
| 44 dict_->GetStringWithoutPathExpansion(key, &result); |
| 45 return result; |
| 46 } |
31 | 47 |
32 private: | 48 private: |
33 scoped_ptr<base::DictionaryValue> dict_; | 49 scoped_ptr<base::DictionaryValue> dict_; |
34 | 50 |
35 DISALLOW_COPY_AND_ASSIGN(Json); | 51 DISALLOW_COPY_AND_ASSIGN(ChromeJson); |
36 }; | 52 }; |
37 | 53 |
| 54 } // namespace |
| 55 |
| 56 Json::~Json() {} |
| 57 |
| 58 // static |
| 59 Json* Json::Build() { |
| 60 return new ChromeJson; |
| 61 } |
| 62 |
| 63 Json::Json() {} |
| 64 |
38 } // namespace addressinput | 65 } // namespace addressinput |
39 } // namespace i18n | 66 } // namespace i18n |
40 | |
41 #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_UTIL_JSON_H_ | |
OLD | NEW |