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 {
33 class Rapidjson : public Json {
35 Rapidjson() : dict_() {}
37 virtual ~Rapidjson() {}
39 virtual bool ParseObject(const std::string& json) {
40 scoped_ptr<rapidjson::Document> document(new rapidjson::Document);
41 document->Parse<rapidjson::kParseValidateEncodingFlag>(json.c_str());
42 if (!document->HasParseError() && document->IsObject()) {
43 dict_.reset(document.release());
49 virtual bool GetStringValueForKey(const std::string& key,
50 std::string* value) const {
51 assert(dict_ != NULL);
54 const rapidjson::Value::Member* member = dict_->FindMember(key.c_str());
55 if (member == NULL || !member->value.IsString()) {
60 value->assign(member->value.GetString(), member->value.GetStringLength());
66 virtual bool GetJsonValueForKey(const std::string& key,
67 scoped_ptr<Json>* value) const {
68 assert(dict_ != NULL);
71 const rapidjson::Value::Member* member = dict_->FindMember(key.c_str());
72 if (member == NULL || !member->value.IsObject()) {
77 scoped_ptr<rapidjson::Value> copy(new rapidjson::Value);
79 // Rapidjson provides only move operations in public API, but implements
80 // the move operation with a memcpy and delete:
82 // https://code.google.com/p/rapidjson/source/browse/trunk/include/rapidjson/document.h?r=131#173
84 // We need a copy of the object, so we use memcpy manually.
85 memcpy(copy.get(), &member->value, sizeof(rapidjson::Value));
87 value->reset(new Rapidjson(copy.Pass()));
94 explicit Rapidjson(scoped_ptr<rapidjson::Value> dict)
95 : dict_(dict.Pass()) {}
98 scoped_ptr<rapidjson::Value> dict_;
100 DISALLOW_COPY_AND_ASSIGN(Rapidjson);
108 scoped_ptr<Json> Json::Build() {
109 return scoped_ptr<Json>(new Rapidjson);
114 } // namespace addressinput