Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: third_party/libaddressinput/chromium/cpp/src/util/json.cc

Issue 140823005: [rac] Download country code data in a single HTTP request. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add comment. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (C) 2013 Google Inc. 1 // Copyright (C) 2013 Google Inc.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with 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 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include "json.h" 15 #include "json.h"
16 16
17 #include <libaddressinput/util/basictypes.h> 17 #include <libaddressinput/util/basictypes.h>
18 #include <libaddressinput/util/scoped_ptr.h> 18 #include <libaddressinput/util/scoped_ptr.h>
19 19
20 #include <cassert> 20 #include <cassert>
21 #include <cstddef> 21 #include <cstddef>
22 #include <cstring>
22 #include <string> 23 #include <string>
23 24
24 #include <rapidjson/document.h> 25 #include <rapidjson/document.h>
25 #include <rapidjson/reader.h> 26 #include <rapidjson/reader.h>
26 27
27 namespace i18n { 28 namespace i18n {
28 namespace addressinput { 29 namespace addressinput {
29 30
30 namespace { 31 namespace {
31 32
32 class Rapidjson : public Json { 33 class Rapidjson : public Json {
33 public: 34 public:
34 Rapidjson() : document_(new rapidjson::Document), valid_(false) {} 35 Rapidjson() : dict_() {}
35 36
36 virtual ~Rapidjson() {} 37 virtual ~Rapidjson() {}
37 38
38 virtual bool ParseObject(const std::string& json) { 39 virtual bool ParseObject(const std::string& json) {
39 document_->Parse<rapidjson::kParseValidateEncodingFlag>(json.c_str()); 40 scoped_ptr<rapidjson::Document> document(new rapidjson::Document);
40 valid_ = !document_->HasParseError() && document_->IsObject(); 41 document->Parse<rapidjson::kParseValidateEncodingFlag>(json.c_str());
41 return valid_; 42 if (!document->HasParseError() && document->IsObject()) {
43 dict_.reset(document.release());
44 return true;
45 }
46 return false;
42 } 47 }
43 48
44 virtual bool GetStringValueForKey(const std::string& key, 49 virtual bool GetStringValueForKey(const std::string& key,
45 std::string* value) const { 50 std::string* value) const {
46 assert(valid_); 51 assert(dict_ != NULL);
47 const rapidjson::Value::Member* member = document_->FindMember(key.c_str()); 52 const rapidjson::Value::Member* member = dict_->FindMember(key.c_str());
48 if (member == NULL || !member->value.IsString()) { 53 if (member == NULL || !member->value.IsString()) {
49 return false; 54 return false;
50 } 55 }
51 56
52 if (value) { 57 if (value) {
53 value->assign(member->value.GetString(), member->value.GetStringLength()); 58 value->assign(member->value.GetString(), member->value.GetStringLength());
54 } 59 }
55 60
56 return true; 61 return true;
57 } 62 }
58 63
64 virtual bool GetJsonValueForKey(const std::string& key, Json** value) const {
65 assert(dict_ != NULL);
66 const rapidjson::Value::Member* member = dict_->FindMember(key.c_str());
67 if (member == NULL || !member->value.IsObject()) {
68 return false;
69 }
70
71 if (value) {
72 scoped_ptr<rapidjson::Value> copy(new rapidjson::Value);
73
74 // Rapidjson provides only move operations in public API, but implements
75 // the move operation with a memcpy and delete:
76 //
77 // https://code.google.com/p/rapidjson/source/browse/trunk/include/rapidjs on/document.h?r=131#173
78 //
79 // We need a copy of the object, so we use memcpy manually.
80 memcpy(copy.get(), &member->value, sizeof(rapidjson::Value));
81
82 *value = new Rapidjson(copy.Pass());
83 }
84
85 return true;
86 }
87
59 protected: 88 protected:
60 // JSON document. 89 explicit Rapidjson(scoped_ptr<rapidjson::Value> dict)
61 scoped_ptr<rapidjson::Document> document_; 90 : dict_(dict.Pass()) {}
62 91
63 // True if the parsed string is a valid JSON object. 92 // JSON value.
64 bool valid_; 93 scoped_ptr<rapidjson::Value> dict_;
65 94
66 DISALLOW_COPY_AND_ASSIGN(Rapidjson); 95 DISALLOW_COPY_AND_ASSIGN(Rapidjson);
67 }; 96 };
68 97
69 } // namespace 98 } // namespace
70 99
71 Json::~Json() {} 100 Json::~Json() {}
72 101
73 // static 102 // static
74 scoped_ptr<Json> Json::Build() { 103 scoped_ptr<Json> Json::Build() {
75 return scoped_ptr<Json>(new Rapidjson); 104 return scoped_ptr<Json>(new Rapidjson);
76 } 105 }
77 106
78 Json::Json() {} 107 Json::Json() {}
79 108
80 } // namespace addressinput 109 } // namespace addressinput
81 } // namespace i18n 110 } // namespace i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698