swift
Loading...
Searching...
No Matches
variantmap.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
4#include "misc/variantmap.h"
5
6#include <QJsonValue>
7
10
11namespace swift::misc
12{
13
15 {
16 for (auto it = cbegin(); it != cend(); ++it)
17 {
18 json.insert(it.key(), it.value().toJson());
19 }
20 return json;
21 }
22
24 {
25 QJsonObject json;
26 mergeToJson(json);
27 return json;
28 }
29
31 {
32 clear();
33 for (auto it = json.begin(); it != json.end(); ++it)
34 {
35 const QString key = it.key();
36 CJsonScope scope(key);
37 Q_UNUSED(scope);
39 value.convertFromJson(it.value().toObject());
41 }
42 }
43
45 {
46 clear();
47 for (const auto &key : keys)
48 {
49 auto value = json.value(key);
50 if (value.isUndefined())
51 {
52 continue;
53 }
54 CJsonScope scope(key);
55 Q_UNUSED(scope);
56 CVariant var;
57 var.convertFromJson(value.toObject());
58 insert(key, var);
59 }
60 }
61
63 {
64 for (auto it = cbegin(); it != cend(); ++it)
65 {
66 json.insert(it.key(), it.value().toMemoizedJson());
67 }
68 return json;
69 }
70
72 {
73 QJsonObject json;
75 return json;
76 }
77
79 {
80 clear();
81 for (auto it = json.begin(); it != json.end(); ++it)
82 {
83 const QString key = it.key();
84 CJsonScope scope(key);
85 Q_UNUSED(scope);
87 value.convertFromMemoizedJson(it.value().toObject(), true);
89 }
90 }
91
93 {
94 clear();
95 for (const auto &key : keys)
96 {
97 auto value = json.value(key);
98 if (value.isUndefined())
99 {
100 continue;
101 }
102 CJsonScope scope(key);
103 Q_UNUSED(scope);
104 CVariant var;
105 var.convertFromMemoizedJson(value.toObject(), true);
106 insert(key, var);
107 }
108 }
109
111 const QString &prefix)
112 {
113 CStatusMessageList messages;
114 clear();
115 for (auto it = json.begin(); it != json.end(); ++it)
116 {
117 const QString key = it.key();
118 CJsonScope scope(key);
119 Q_UNUSED(scope);
121 auto message = value.convertFromJsonNoThrow(it.value().toObject(), categories, prefix);
122 if (message.isSuccess())
123 {
125 }
126 else
127 {
128 messages.push_back(message);
129 }
130 }
131 return messages;
132 }
133
135 const CLogCategoryList &categories, const QString &prefix)
136 {
137 CStatusMessageList messages;
138 clear();
139 for (const auto &key : keys)
140 {
141 auto value = json.value(key);
142 if (value.isUndefined())
143 {
144 continue;
145 }
146 CJsonScope scope(key);
147 Q_UNUSED(scope);
148 CVariant var;
149 auto message = var.convertFromJsonNoThrow(value.toObject(), categories, prefix);
150 if (message.isSuccess())
151 {
152 insert(key, var);
153 }
154 else
155 {
156 messages.push_back(message);
157 }
158 }
159 return messages;
160 }
161
163 const CLogCategoryList &categories,
164 const QString &prefix)
165 {
166 CStatusMessageList messages;
167 clear();
168 for (auto it = json.begin(); it != json.end(); ++it)
169 {
170 const QString key = it.key();
171 CJsonScope scope(key);
172 Q_UNUSED(scope);
174 auto message = value.convertFromMemoizedJsonNoThrow(it.value().toObject(), categories, prefix);
175 if (message.isSuccess())
176 {
178 }
179 else
180 {
181 messages.push_back(message);
182 }
183 }
184 return messages;
185 }
186
188 const CLogCategoryList &categories,
189 const QString &prefix)
190 {
191 CStatusMessageList messages;
192 clear();
193 for (const auto &key : keys)
194 {
195 auto value = json.value(key);
196 if (value.isUndefined())
197 {
198 continue;
199 }
200 CJsonScope scope(key);
201 Q_UNUSED(scope);
202 CVariant var;
203 auto message = var.convertFromMemoizedJsonNoThrow(value.toObject(), categories, prefix);
204 if (message.isSuccess())
205 {
206 insert(key, var);
207 }
208 else
209 {
210 messages.push_back(message);
211 }
212 }
213 return messages;
214 }
215} // namespace swift::misc
const CVariant value(const QString &key) const
Definition dictionary.h:447
friend impl_type & implementationOf(CDictionary &dict)
Definition dictionary.h:470
iterator insert(const QString &key, const CVariant &value)
Definition dictionary.h:414
const QString key(const CVariant &value) const
Definition dictionary.h:429
Pseudo-RAII pattern that tracks the current JSON value being converted.
A sequence of log categories.
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition sequence.h:308
Status messages, e.g. from Core -> GUI.
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition variant.h:69
CStatusMessage convertFromMemoizedJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix)
Call convertFromMemoizedJson, catch any CJsonException that is thrown and return it as CStatusMessage...
void convertFromMemoizedJson(const QJsonObject &json, bool allowFallbackToJson)
From compact JSON format.
void convertFromJson(const QJsonObject &json)
Assign from JSON object.
CStatusMessage convertFromJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix)
Call convertFromJson, catch any CJsonException that is thrown and return it as CStatusMessage.
void convertFromJson(const QJsonObject &json)
Assign from JSON object.
QJsonObject & mergeToMemoizedJson(QJsonObject &json) const
Insert values from this map into an existing compact JSON object.
CStatusMessageList convertFromMemoizedJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix)
Call convertFromMemoizedJson, catch any CJsonException that are thrown and return them as CStatusMess...
void convertFromMemoizedJson(const QJsonObject &json)
From compact JSON format.
QJsonObject toMemoizedJson() const
To compact JSON format.
QJsonObject toJson() const
Cast to JSON object.
QJsonObject & mergeToJson(QJsonObject &json) const
Insert values from this map into an existing JSON object.
CStatusMessageList convertFromJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix)
Call convertFromJson, catch any CJsonException that are thrown and return them as CStatusMessage.
Free functions in swift::misc.
QMap< Key, T >::iterator insert(QMap< Key, T >::const_iterator pos, const Key &key, const T &value)