summaryrefslogtreecommitdiffstats
path: root/third_party/libaddressinput/chromium/json.cc
blob: 67238464a086a5e9f487c54e236201e38036e310 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cpp/src/util/json.h"

#include "base/basictypes.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"

namespace i18n {
namespace addressinput {

namespace {

class ChromeJson : public Json {
 public:
  ChromeJson() {}

  virtual ~ChromeJson() {}

  virtual bool ParseObject(const std::string& json) {
    dict_.reset();

    // |json| is converted to a |c_str()| here because rapidjson and other parts
    // of the standalone library use char* rather than std::string.
    scoped_ptr<base::Value> parsed(base::JSONReader::Read(json.c_str()));
    if (parsed && parsed->IsType(base::Value::TYPE_DICTIONARY))
      dict_.reset(static_cast<base::DictionaryValue*>(parsed.release()));

    return !!dict_;
  }

  virtual bool HasStringValueForKey(const std::string& key) const {
    base::Value* val = NULL;
    dict_->GetWithoutPathExpansion(key, &val);
    return val && val->IsType(base::Value::TYPE_STRING);
  }

  virtual std::string GetStringValueForKey(const std::string& key) const {
    std::string result;
    dict_->GetStringWithoutPathExpansion(key, &result);
    return result;
  }

 private:
  scoped_ptr<base::DictionaryValue> dict_;

  DISALLOW_COPY_AND_ASSIGN(ChromeJson);
};

}  // namespace

Json::~Json() {}

// static
scoped_ptr<Json> Json::Build() {
  return scoped_ptr<Json>(new ChromeJson);
}

Json::Json() {}

}  // namespace addressinput
}  // namespace i18n