1 // Copyright (C) 2013 Google Inc.
3 // Licensed under the Apache License, Version 2.0 (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
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
17 #include <libaddressinput/util/basictypes.h>
18 #include <libaddressinput/util/scoped_ptr.h>
25 #include <rapidjson/document.h>
26 #include <rapidjson/reader.h>
29 namespace addressinput {
31 using rapidjson::Document;
32 using rapidjson::kParseValidateEncodingFlag;
33 using rapidjson::Value;
35 class Json::JsonImpl {
37 explicit JsonImpl(const std::string& json)
38 : document_(new Document),
39 value_(document_.get()),
42 document_->Parse<kParseValidateEncodingFlag>(json.c_str());
43 valid_ = !document_->HasParseError() && document_->IsObject();
47 for (std::vector<const Json*>::const_iterator it = dictionaries_.begin();
48 it != dictionaries_.end(); ++it) {
53 bool valid() const { return valid_; }
55 const std::vector<const Json*>& GetSubDictionaries() {
56 if (dictionaries_.empty()) {
57 for (Value::ConstMemberIterator member = value_->MemberBegin();
58 member != value_->MemberEnd(); ++member) {
59 if (member->value.IsObject()) {
60 dictionaries_.push_back(new Json(new JsonImpl(&member->value)));
67 bool GetStringValueForKey(const std::string& key, std::string* value) const {
68 assert(value != NULL);
70 Value::ConstMemberIterator member = value_->FindMember(key.c_str());
71 if (member == value_->MemberEnd() || !member->value.IsString()) {
75 value->assign(member->value.GetString(),
76 member->value.GetStringLength());
81 // Does not take ownership of |value|.
82 explicit JsonImpl(const Value* value)
87 assert(value_ != NULL);
88 assert(value_->IsObject());
91 // An owned JSON document. Can be NULL if the JSON document is not owned.
92 const scoped_ptr<Document> document_;
94 // A JSON document that is not owned. Cannot be NULL. Can point to document_.
95 const Value* const value_;
97 // Owned JSON objects of sub-dictionaries.
98 std::vector<const Json*> dictionaries_;
100 // True if the JSON object was parsed successfully.
103 DISALLOW_COPY_AND_ASSIGN(JsonImpl);
106 Json::Json() : impl_() {}
110 bool Json::ParseObject(const std::string& json) {
111 assert(impl_ == NULL);
112 impl_.reset(new JsonImpl(json));
113 if (!impl_->valid()) {
116 return impl_ != NULL;
119 const std::vector<const Json*>& Json::GetSubDictionaries() const {
120 assert(impl_ != NULL);
121 return impl_->GetSubDictionaries();
124 bool Json::GetStringValueForKey(const std::string& key,
125 std::string* value) const {
126 assert(impl_ != NULL);
127 return impl_->GetStringValueForKey(key, value);
130 Json::Json(JsonImpl* impl) : impl_(impl) {}
132 } // namespace addressinput