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
3 // found in the LICENSE file.
5 #include "third_party/libaddressinput/src/cpp/src/util/json.h"
10 #include "base/basictypes.h"
11 #include "base/json/json_reader.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/stl_util.h"
15 #include "base/values.h"
18 namespace addressinput
{
22 // Returns |json| parsed into a JSON dictionary. Sets |parser_error| to true if
24 ::scoped_ptr
<const base::DictionaryValue
> Parse(const std::string
& json
,
27 ::scoped_ptr
<const base::DictionaryValue
> result
;
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
);
35 result
.reset(new base::DictionaryValue
);
37 result
.reset(static_cast<const base::DictionaryValue
*>(parsed
.release()));
44 // Implementation of JSON parser for libaddressinput using JSON parser in
46 class Json::JsonImpl
{
48 explicit JsonImpl(const std::string
& json
)
49 : owned_(Parse(json
, &parser_error_
)),
52 ~JsonImpl() { STLDeleteElements(&sub_dicts_
); }
54 bool parser_error() const { return parser_error_
; }
56 const std::vector
<const Json
*>& GetSubDictionaries() {
57 if (sub_dicts_
.empty()) {
58 for (base::DictionaryValue::Iterator
it(dict_
); !it
.IsAtEnd();
60 if (it
.value().IsType(base::Value::TYPE_DICTIONARY
)) {
61 const base::DictionaryValue
* sub_dict
= NULL
;
62 it
.value().GetAsDictionary(&sub_dict
);
63 sub_dicts_
.push_back(new Json(new JsonImpl(*sub_dict
)));
70 bool GetStringValueForKey(const std::string
& key
, std::string
* value
) const {
71 return dict_
.GetStringWithoutPathExpansion(key
, value
);
75 explicit JsonImpl(const base::DictionaryValue
& dict
)
76 : parser_error_(false), dict_(dict
) {}
78 const ::scoped_ptr
<const base::DictionaryValue
> owned_
;
80 const base::DictionaryValue
& dict_
;
81 std::vector
<const Json
*> sub_dicts_
;
83 DISALLOW_COPY_AND_ASSIGN(JsonImpl
);
90 bool Json::ParseObject(const std::string
& json
) {
92 impl_
.reset(new JsonImpl(json
));
93 if (impl_
->parser_error())
98 const std::vector
<const Json
*>& Json::GetSubDictionaries() const {
99 return impl_
->GetSubDictionaries();
102 bool Json::GetStringValueForKey(const std::string
& key
,
103 std::string
* value
) const {
104 return impl_
->GetStringValueForKey(key
, value
);
107 Json::Json(JsonImpl
* impl
) : impl_(impl
) {}
109 } // namespace addressinput