-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipp_in_json.cc
178 lines (161 loc) · 5.17 KB
/
ipp_in_json.cc
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2019 The Chromium OS 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 "print_tools/ipp_in_json.h"
#include <memory>
#include <utility>
#include <base/json/json_writer.h>
#include <base/macros.h>
#include <base/optional.h>
#include <base/values.h>
namespace {
base::Value SaveAsJson(const ipp::Collection* coll);
// It saves a single value (at given index) from the attribute as JSON
// structure. The parameter "attr" cannot be nullptr, "index" must be correct.
base::Value SaveAsJson(const ipp::Attribute* attr, unsigned index) {
CHECK(attr != nullptr);
CHECK(index < attr->GetSize());
using AttrType = ipp::AttrType;
switch (attr->GetType()) {
case AttrType::integer: {
int vi;
attr->GetValue(&vi, index);
return base::Value(vi);
}
case AttrType::boolean: {
int vb;
attr->GetValue(&vb, index);
return base::Value(static_cast<bool>(vb));
}
case AttrType::enum_: {
std::string vs;
attr->GetValue(&vs, index);
if (vs.empty()) {
int vi;
attr->GetValue(&vi, index);
return base::Value(vi);
}
return base::Value(vs);
}
case AttrType::collection:
return SaveAsJson(attr->GetCollection(index));
case AttrType::text:
case AttrType::name: {
ipp::StringWithLanguage vs;
attr->GetValue(&vs, index);
if (vs.language.empty())
return base::Value(vs.value);
base::Value obj(base::Value::Type::DICTIONARY);
obj.SetStringKey("value", vs.value);
obj.SetStringKey("language", vs.language);
return obj;
}
case AttrType::dateTime:
case AttrType::resolution:
case AttrType::rangeOfInteger:
case AttrType::octetString:
case AttrType::keyword:
case AttrType::uri:
case AttrType::uriScheme:
case AttrType::charset:
case AttrType::naturalLanguage:
case AttrType::mimeMediaType: {
std::string vs;
attr->GetValue(&vs, index);
return base::Value(vs);
}
}
return base::Value(); // not reachable
}
// It saves all attribute's values as JSON structure.
// The parameter "attr" cannot be nullptr.
base::Value SaveAsJson(const ipp::Attribute* attr) {
CHECK(attr != nullptr);
if (attr->IsASet()) {
base::Value arr(base::Value::Type::LIST);
const unsigned size = attr->GetSize();
for (unsigned i = 0; i < size; ++i)
arr.Append(SaveAsJson(attr, i));
return arr;
} else {
return SaveAsJson(attr, 0);
}
}
// It saves a given Collection as JSON object.
// The parameter "coll" cannot be nullptr.
base::Value SaveAsJson(const ipp::Collection* coll) {
CHECK(coll != nullptr);
base::Value obj(base::Value::Type::DICTIONARY);
std::vector<const ipp::Attribute*> attrs = coll->GetAllAttributes();
for (auto a : attrs) {
auto state = a->GetState();
if (state == ipp::AttrState::unset)
continue;
if (state == ipp::AttrState::set) {
base::Value obj2(base::Value::Type::DICTIONARY);
obj2.SetStringKey("type", ToString(a->GetType()));
obj2.SetKey("value", SaveAsJson(a));
obj.SetKey(a->GetName(), std::move(obj2));
} else {
obj.SetStringKey(a->GetName(), ToString(state));
}
}
return obj;
}
// It saves all groups from given Package as JSON object.
// The parameter "pkg" cannot be nullptr.
base::Value SaveAsJson(const ipp::Package* pkg) {
CHECK(pkg != nullptr);
base::Value obj(base::Value::Type::DICTIONARY);
std::vector<const ipp::Group*> groups = pkg->GetAllGroups();
for (auto g : groups) {
const size_t size = g->GetSize();
if (size == 0)
continue;
if (g->IsASet()) {
base::Value arr(base::Value::Type::LIST);
for (size_t i = 0; i < size; ++i)
arr.Append(SaveAsJson(g->GetCollection(i)));
obj.SetKey(ToString(g->GetName()), std::move(arr));
} else {
obj.SetKey(ToString(g->GetName()), SaveAsJson(g->GetCollection()));
}
}
return obj;
}
// Saves given logs as JSON array.
base::Value SaveAsJson(const std::vector<ipp::Log>& log) {
base::Value arr(base::Value::Type::LIST);
for (const auto& l : log) {
base::Value obj(base::Value::Type::DICTIONARY);
obj.SetStringKey("message", l.message);
if (!l.frame_context.empty())
obj.SetStringKey("frame_context", l.frame_context);
if (!l.parser_context.empty())
obj.SetStringKey("parser_context", l.parser_context);
arr.Append(std::move(obj));
}
return arr;
}
} // namespace
bool ConvertToJson(const ipp::Response& response,
const std::vector<ipp::Log>& log,
bool compressed_json,
std::string* json) {
// Build structure.
base::Value doc(base::Value::Type::DICTIONARY);
doc.SetStringKey("status", ipp::ToString(response.StatusCode()));
if (!log.empty()) {
doc.SetKey("parsing_logs", SaveAsJson(log));
}
doc.SetKey("response", SaveAsJson(&response));
// Convert to JSON.
bool result;
if (compressed_json) {
result = base::JSONWriter::Write(doc, json);
} else {
const int options = base::JSONWriter::OPTIONS_PRETTY_PRINT;
result = base::JSONWriter::WriteWithOptions(doc, options, json);
}
return result;
}