swift
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 
8 #include "misc/jsonexception.h"
10 
11 namespace swift::misc
12 {
13 
14  QJsonObject &CVariantMap::mergeToJson(QJsonObject &json) const
15  {
16  for (auto it = cbegin(); it != cend(); ++it) { json.insert(it.key(), it.value().toJson()); }
17  return json;
18  }
19 
20  QJsonObject CVariantMap::toJson() const
21  {
22  QJsonObject json;
23  mergeToJson(json);
24  return json;
25  }
26 
27  void CVariantMap::convertFromJson(const QJsonObject &json)
28  {
29  clear();
30  for (auto it = json.begin(); it != json.end(); ++it)
31  {
32  const QString key = it.key();
33  CJsonScope scope(key);
34  Q_UNUSED(scope);
36  value.convertFromJson(it.value().toObject());
37  implementationOf(*this).insert(cend(), key, value);
38  }
39  }
40 
41  void CVariantMap::convertFromJson(const QJsonObject &json, const QStringList &keys)
42  {
43  clear();
44  for (const auto &key : keys)
45  {
46  auto value = json.value(key);
47  if (value.isUndefined()) { continue; }
48  CJsonScope scope(key);
49  Q_UNUSED(scope);
50  CVariant var;
51  var.convertFromJson(value.toObject());
52  insert(key, var);
53  }
54  }
55 
56  QJsonObject &CVariantMap::mergeToMemoizedJson(QJsonObject &json) const
57  {
58  for (auto it = cbegin(); it != cend(); ++it) { json.insert(it.key(), it.value().toMemoizedJson()); }
59  return json;
60  }
61 
62  QJsonObject CVariantMap::toMemoizedJson() const
63  {
64  QJsonObject json;
65  mergeToMemoizedJson(json);
66  return json;
67  }
68 
69  void CVariantMap::convertFromMemoizedJson(const QJsonObject &json)
70  {
71  clear();
72  for (auto it = json.begin(); it != json.end(); ++it)
73  {
74  const QString key = it.key();
75  CJsonScope scope(key);
76  Q_UNUSED(scope);
78  value.convertFromMemoizedJson(it.value().toObject(), true);
79  implementationOf(*this).insert(cend(), key, value);
80  }
81  }
82 
83  void CVariantMap::convertFromMemoizedJson(const QJsonObject &json, const QStringList &keys)
84  {
85  clear();
86  for (const auto &key : keys)
87  {
88  auto value = json.value(key);
89  if (value.isUndefined()) { continue; }
90  CJsonScope scope(key);
91  Q_UNUSED(scope);
92  CVariant var;
93  var.convertFromMemoizedJson(value.toObject(), true);
94  insert(key, var);
95  }
96  }
97 
98  CStatusMessageList CVariantMap::convertFromJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories,
99  const QString &prefix)
100  {
101  CStatusMessageList messages;
102  clear();
103  for (auto it = json.begin(); it != json.end(); ++it)
104  {
105  const QString key = it.key();
106  CJsonScope scope(key);
107  Q_UNUSED(scope);
108  CVariant value;
109  auto message = value.convertFromJsonNoThrow(it.value().toObject(), categories, prefix);
110  if (message.isSuccess()) { implementationOf(*this).insert(cend(), key, value); }
111  else { messages.push_back(message); }
112  }
113  return messages;
114  }
115 
116  CStatusMessageList CVariantMap::convertFromJsonNoThrow(const QJsonObject &json, const QStringList &keys,
117  const CLogCategoryList &categories, const QString &prefix)
118  {
119  CStatusMessageList messages;
120  clear();
121  for (const auto &key : keys)
122  {
123  auto value = json.value(key);
124  if (value.isUndefined()) { continue; }
125  CJsonScope scope(key);
126  Q_UNUSED(scope);
127  CVariant var;
128  auto message = var.convertFromJsonNoThrow(value.toObject(), categories, prefix);
129  if (message.isSuccess()) { insert(key, var); }
130  else { messages.push_back(message); }
131  }
132  return messages;
133  }
134 
136  const CLogCategoryList &categories,
137  const QString &prefix)
138  {
139  CStatusMessageList messages;
140  clear();
141  for (auto it = json.begin(); it != json.end(); ++it)
142  {
143  const QString key = it.key();
144  CJsonScope scope(key);
145  Q_UNUSED(scope);
146  CVariant value;
147  auto message = value.convertFromMemoizedJsonNoThrow(it.value().toObject(), categories, prefix);
148  if (message.isSuccess()) { implementationOf(*this).insert(cend(), key, value); }
149  else { messages.push_back(message); }
150  }
151  return messages;
152  }
153 
154  CStatusMessageList CVariantMap::convertFromMemoizedJsonNoThrow(const QJsonObject &json, const QStringList &keys,
155  const CLogCategoryList &categories,
156  const QString &prefix)
157  {
158  CStatusMessageList messages;
159  clear();
160  for (const auto &key : keys)
161  {
162  auto value = json.value(key);
163  if (value.isUndefined()) { continue; }
164  CJsonScope scope(key);
165  Q_UNUSED(scope);
166  CVariant var;
167  auto message = var.convertFromMemoizedJsonNoThrow(value.toObject(), categories, prefix);
168  if (message.isSuccess()) { insert(key, var); }
169  else { messages.push_back(message); }
170  }
171  return messages;
172  }
173 } // namespace swift::misc
void clear()
Removes all items from the dictionary.
Definition: dictionary.h:338
const CVariant value(const QString &key) const
Returns the value associated with the key.
Definition: dictionary.h:404
auto keys() const
Return a range of all keys (does not allocate a temporary container)
Definition: dictionary.h:392
const_iterator cbegin() const
Returns const iterator at the beginning of the dictionary.
Definition: dictionary.h:302
iterator insert(const QString &key, const CVariant &value)
Insert new item with key and value.
Definition: dictionary.h:374
friend impl_type & implementationOf(CDictionary &dict)
Return reference to the internal implementation object.
Definition: dictionary.h:427
const QString key(const CVariant &value) const
Return key assigned to value.
Definition: dictionary.h:386
const_iterator cend() const
Returns const iterator at the end of the dictionary.
Definition: dictionary.h:311
Pseudo-RAII pattern that tracks the current JSON value being converted.
Definition: jsonexception.h:50
A sequence of log categories.
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
Status messages, e.g. from Core -> GUI.
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
CStatusMessage convertFromMemoizedJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix)
Call convertFromMemoizedJson, catch any CJsonException that is thrown and return it as CStatusMessage...
T value() const
Return the value converted to the type T.
Definition: variant.h:169
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.
Definition: variantmap.cpp:27
QJsonObject & mergeToMemoizedJson(QJsonObject &json) const
Insert values from this map into an existing compact JSON object.
Definition: variantmap.cpp:56
CStatusMessageList convertFromMemoizedJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix)
Call convertFromMemoizedJson, catch any CJsonException that are thrown and return them as CStatusMess...
Definition: variantmap.cpp:135
void convertFromMemoizedJson(const QJsonObject &json)
From compact JSON format.
Definition: variantmap.cpp:69
QJsonObject toMemoizedJson() const
To compact JSON format.
Definition: variantmap.cpp:62
QJsonObject toJson() const
Cast to JSON object.
Definition: variantmap.cpp:20
QJsonObject & mergeToJson(QJsonObject &json) const
Insert values from this map into an existing JSON object.
Definition: variantmap.cpp:14
CStatusMessageList convertFromJsonNoThrow(const QJsonObject &json, const CLogCategoryList &categories, const QString &prefix)
Call convertFromJson, catch any CJsonException that are thrown and return them as CStatusMessage.
Definition: variantmap.cpp:98
Free functions in swift::misc.