blob: 5437e1e55056ef4d2eec6158e54acf85c9f536ea (
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
// 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 {
// A base class for Chrome Json objects. JSON gets parsed into a
// base::DictionaryValue and data is accessed via the Json interface.
class ChromeJson : public Json {
public:
virtual bool GetStringValueForKey(const std::string& key, std::string* value)
const OVERRIDE;
virtual bool GetJsonValueForKey(const std::string& key,
scoped_ptr<Json>* value) const OVERRIDE;
protected:
ChromeJson() {}
virtual ~ChromeJson() {}
virtual const base::DictionaryValue* GetDict() const = 0;
DISALLOW_COPY_AND_ASSIGN(ChromeJson);
};
// A Json object that will parse a string and own the parsed data.
class JsonDataOwner : public ChromeJson {
public:
JsonDataOwner() {}
virtual ~JsonDataOwner() {}
virtual bool ParseObject(const std::string& json) OVERRIDE {
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_;
}
protected:
virtual const base::DictionaryValue* GetDict() const OVERRIDE {
return dict_.get();
}
private:
scoped_ptr<base::DictionaryValue> dict_;
DISALLOW_COPY_AND_ASSIGN(JsonDataOwner);
};
// A Json object which will point to data that's been parsed by a different
// ChromeJson. It does not own its data and is only valid as long as its parent
// ChromeJson is valid.
class JsonDataCopy : public ChromeJson {
public:
explicit JsonDataCopy(const base::DictionaryValue* dict) :
dict_(dict) {}
virtual ~JsonDataCopy() {}
virtual bool ParseObject(const std::string& json) OVERRIDE {
NOTREACHED();
return false;
}
protected:
virtual const base::DictionaryValue* GetDict() const OVERRIDE {
return dict_;
}
private:
const base::DictionaryValue* dict_; // weak reference.
DISALLOW_COPY_AND_ASSIGN(JsonDataCopy);
};
// ChromeJson ------------------------------------------------------------------
bool ChromeJson::GetStringValueForKey(const std::string& key,
std::string* value) const {
return GetDict()->GetStringWithoutPathExpansion(key, value);
}
bool ChromeJson::GetJsonValueForKey(const std::string& key,
scoped_ptr<Json>* value) const {
const base::DictionaryValue* sub_dict = NULL;
if (!GetDict()->GetDictionaryWithoutPathExpansion(key, &sub_dict) ||
!sub_dict) {
return false;
}
if (value)
value->reset(new JsonDataCopy(sub_dict));
return true;
}
} // namespace
Json::~Json() {}
// static
scoped_ptr<Json> Json::Build() {
return scoped_ptr<Json>(new JsonDataOwner);
}
Json::Json() {}
} // namespace addressinput
} // namespace i18n
|