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 "third_party/libaddressinput/src/cpp/src/util/json.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 // parsing failed. | 22 // parsing failed. |
23 ::std::unique_ptr<const base::DictionaryValue> Parse(const std::string& json, | 23 ::std::unique_ptr<const base::DictionaryValue> Parse(const std::string& json, |
24 bool* parser_error) { | 24 bool* parser_error) { |
25 DCHECK(parser_error); | 25 DCHECK(parser_error); |
26 ::std::unique_ptr<const base::DictionaryValue> result; | 26 ::std::unique_ptr<const base::DictionaryValue> result; |
27 | 27 |
28 // |json| is converted to a |c_str()| here because rapidjson and other parts | 28 // |json| is converted to a |c_str()| here because rapidjson and other parts |
29 // of the standalone library use char* rather than std::string. | 29 // of the standalone library use char* rather than std::string. |
30 ::std::unique_ptr<const base::Value> parsed( | 30 ::std::unique_ptr<const base::Value> parsed( |
31 base::JSONReader::Read(json.c_str())); | 31 base::JSONReader::Read(json.c_str())); |
32 *parser_error = !parsed || !parsed->IsType(base::Value::TYPE_DICTIONARY); | 32 *parser_error = !parsed || !parsed->IsType(base::Value::Type::DICTIONARY); |
33 | 33 |
34 if (*parser_error) | 34 if (*parser_error) |
35 result.reset(new base::DictionaryValue); | 35 result.reset(new base::DictionaryValue); |
36 else | 36 else |
37 result.reset(static_cast<const base::DictionaryValue*>(parsed.release())); | 37 result.reset(static_cast<const base::DictionaryValue*>(parsed.release())); |
38 | 38 |
39 return result; | 39 return result; |
40 } | 40 } |
41 | 41 |
42 } // namespace | 42 } // namespace |
43 | 43 |
44 // Implementation of JSON parser for libaddressinput using JSON parser in | 44 // Implementation of JSON parser for libaddressinput using JSON parser in |
45 // Chrome. | 45 // Chrome. |
46 class Json::JsonImpl { | 46 class Json::JsonImpl { |
47 public: | 47 public: |
48 explicit JsonImpl(const std::string& json) | 48 explicit JsonImpl(const std::string& json) |
49 : owned_(Parse(json, &parser_error_)), | 49 : owned_(Parse(json, &parser_error_)), |
50 dict_(*owned_) {} | 50 dict_(*owned_) {} |
51 | 51 |
52 ~JsonImpl() {} | 52 ~JsonImpl() {} |
53 | 53 |
54 bool parser_error() const { return parser_error_; } | 54 bool parser_error() const { return parser_error_; } |
55 | 55 |
56 const std::vector<const Json*>& GetSubDictionaries() { | 56 const std::vector<const Json*>& GetSubDictionaries() { |
57 if (sub_dicts_.empty()) { | 57 if (sub_dicts_.empty()) { |
58 for (base::DictionaryValue::Iterator it(dict_); !it.IsAtEnd(); | 58 for (base::DictionaryValue::Iterator it(dict_); !it.IsAtEnd(); |
59 it.Advance()) { | 59 it.Advance()) { |
60 if (it.value().IsType(base::Value::TYPE_DICTIONARY)) { | 60 if (it.value().IsType(base::Value::Type::DICTIONARY)) { |
61 const base::DictionaryValue* sub_dict = NULL; | 61 const base::DictionaryValue* sub_dict = NULL; |
62 it.value().GetAsDictionary(&sub_dict); | 62 it.value().GetAsDictionary(&sub_dict); |
63 owned_sub_dicts_.push_back( | 63 owned_sub_dicts_.push_back( |
64 base::WrapUnique(new Json(new JsonImpl(*sub_dict)))); | 64 base::WrapUnique(new Json(new JsonImpl(*sub_dict)))); |
65 sub_dicts_.push_back(owned_sub_dicts_.back().get()); | 65 sub_dicts_.push_back(owned_sub_dicts_.back().get()); |
66 } | 66 } |
67 } | 67 } |
68 } | 68 } |
69 return sub_dicts_; | 69 return sub_dicts_; |
70 } | 70 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 | 104 |
105 bool Json::GetStringValueForKey(const std::string& key, | 105 bool Json::GetStringValueForKey(const std::string& key, |
106 std::string* value) const { | 106 std::string* value) const { |
107 return impl_->GetStringValueForKey(key, value); | 107 return impl_->GetStringValueForKey(key, value); |
108 } | 108 } |
109 | 109 |
110 Json::Json(JsonImpl* impl) : impl_(impl) {} | 110 Json::Json(JsonImpl* impl) : impl_(impl) {} |
111 | 111 |
112 } // namespace addressinput | 112 } // namespace addressinput |
113 } // namespace i18n | 113 } // namespace i18n |
OLD | NEW |