OLD | NEW |
(Empty) | |
| 1 // Copyright (C) 2013 Google Inc. |
| 2 // |
| 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 |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 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. |
| 14 |
| 15 #include "json.h" |
| 16 |
| 17 #include <libaddressinput/util/basictypes.h> |
| 18 #include <libaddressinput/util/scoped_ptr.h> |
| 19 |
| 20 #include <cassert> |
| 21 #include <cstddef> |
| 22 #include <string> |
| 23 |
| 24 #include <rapidjson/document.h> |
| 25 #include <rapidjson/reader.h> |
| 26 |
| 27 namespace i18n { |
| 28 namespace addressinput { |
| 29 |
| 30 namespace { |
| 31 |
| 32 class Rapidjson : public Json { |
| 33 public: |
| 34 Rapidjson() : document_(new rapidjson::Document), valid_(false) {} |
| 35 |
| 36 virtual ~RapidJson() {} |
| 37 |
| 38 virtual bool ParseObject(const std::string& json) { |
| 39 document_->Parse<rapidjson::kParseValidateEncodingFlag>(json.c_str()); |
| 40 valid_ = !document_->HasParseError() && document_->IsObject(); |
| 41 return valid_; |
| 42 } |
| 43 |
| 44 virtual bool HasStringValueForKey(const std::string& key) const { |
| 45 assert(valid_); |
| 46 const rapidjson::Value::Member* member = document_->FindMember(key.c_str()); |
| 47 return member != NULL && member->value.IsString(); |
| 48 } |
| 49 |
| 50 virtual std::string GetStringValueForKey(const std::string& key) const { |
| 51 assert(valid_); |
| 52 const rapidjson::Value::Member* member = document_->FindMember(key.c_str()); |
| 53 assert(member != NULL); |
| 54 assert(member->value.IsString()); |
| 55 return std::string(member->value.GetString(), |
| 56 member->value.GetStringLength()); |
| 57 } |
| 58 |
| 59 protected: |
| 60 // JSON document. |
| 61 scoped_ptr<rapidjson::Document> document_; |
| 62 |
| 63 // True if the parsed string is a valid JSON object. |
| 64 bool valid_; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(Rapidjson); |
| 67 }; |
| 68 |
| 69 } // namespace |
| 70 |
| 71 Json::~Json() {} |
| 72 |
| 73 // static |
| 74 Json* Json::Build() { |
| 75 return new Rapidjson; |
| 76 } |
| 77 |
| 78 Json::Json() {} |
| 79 |
| 80 } // namespace addressinput |
| 81 } // namespace i18n |
OLD | NEW |