• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "cpp/src/util/json.h"
6 
7 #include "base/basictypes.h"
8 #include "base/json/json_reader.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/values.h"
12 
13 namespace i18n {
14 namespace addressinput {
15 
16 namespace {
17 
18 class ChromeJson : public Json {
19  public:
ChromeJson()20   ChromeJson() {}
21 
~ChromeJson()22   virtual ~ChromeJson() {}
23 
ParseObject(const std::string & json)24   virtual bool ParseObject(const std::string& json) {
25     dict_.reset();
26 
27     // |json| is converted to a |c_str()| here because rapidjson and other parts
28     // of the standalone library use char* rather than std::string.
29     scoped_ptr<base::Value> parsed(base::JSONReader::Read(json.c_str()));
30     if (parsed && parsed->IsType(base::Value::TYPE_DICTIONARY))
31       dict_.reset(static_cast<base::DictionaryValue*>(parsed.release()));
32 
33     return !!dict_;
34   }
35 
HasStringValueForKey(const std::string & key) const36   virtual bool HasStringValueForKey(const std::string& key) const {
37     base::Value* val = NULL;
38     dict_->GetWithoutPathExpansion(key, &val);
39     return val && val->IsType(base::Value::TYPE_STRING);
40   }
41 
GetStringValueForKey(const std::string & key) const42   virtual std::string GetStringValueForKey(const std::string& key) const {
43     std::string result;
44     dict_->GetStringWithoutPathExpansion(key, &result);
45     return result;
46   }
47 
48  private:
49   scoped_ptr<base::DictionaryValue> dict_;
50 
51   DISALLOW_COPY_AND_ASSIGN(ChromeJson);
52 };
53 
54 }  // namespace
55 
~Json()56 Json::~Json() {}
57 
58 // static
Build()59 Json* Json::Build() {
60   return new ChromeJson;
61 }
62 
Json()63 Json::Json() {}
64 
65 }  // namespace addressinput
66 }  // namespace i18n
67