6 #ifndef SWIFT_MISC_JSON_H
7 #define SWIFT_MISC_JSON_H
14 #include <QJsonDocument>
15 #include <QJsonObject>
17 #include <QJsonValueRef>
18 #include <QStringList>
69 std::enable_if_t<std::is_enum_v<ENUM>, QJsonObject> &
operator<<(QJsonObject &json,
70 std::pair<QString, const ENUM &> value)
72 json.insert(value.first, QJsonValue(
static_cast<int>(value.second)));
76 std::enable_if_t<std::is_enum_v<ENUM>, QJsonObject> &
77 operator<<(QJsonObject &json, std::pair<swift::misc::CExplicitLatin1String, const ENUM &> value)
79 json[value.first] = QJsonValue(
static_cast<int>(value.second));
88 QJsonObject &
operator<<(QJsonObject &json, std::pair<QString,
const QFlags<ENUM> &> value)
90 json.insert(value.first, QJsonValue(
static_cast<int>(value.second)));
96 json[value.first] = QJsonValue(
static_cast<int>(value.second));
103 template <
class ENUM>
104 std::enable_if_t<std::is_enum_v<ENUM>, QJsonValue>
const &
operator>>(
const QJsonValue &json, ENUM &value)
106 value =
static_cast<ENUM
>(json.toInt());
112 template <
class ENUM>
113 const QJsonValue &
operator>>(
const QJsonValue &json, QFlags<ENUM> &value)
115 value =
static_cast<QFlags<ENUM>
>(json.toInt());
121 template <
class ENUM,
typename = std::enable_if_t<std::is_enum_v<ENUM>>>
124 value =
static_cast<ENUM
>(json.toInt());
130 template <
class ENUM>
131 QJsonValueRef
operator>>(QJsonValueRef json, QFlags<ENUM> &value)
133 value =
static_cast<QFlags<ENUM>
>(json.toInt());
139 template <
class FIRST,
class SECOND>
140 QJsonValueRef
operator>>(QJsonValueRef json, std::pair<FIRST, SECOND> &pair)
142 json.toArray() >> pair.first >> pair.second;
148 template <
class FIRST,
class SECOND>
149 QJsonArray &
operator<<(QJsonArray &json,
const std::pair<FIRST, SECOND> &pair)
152 return json << QJsonValue(array << pair.first << pair.second);
189 const std::pair<swift::misc::CExplicitLatin1String, const int &> &value);
191 const std::pair<swift::misc::CExplicitLatin1String, const qint16 &> &value);
193 operator<<(QJsonObject &json,
const std::pair<swift::misc::CExplicitLatin1String, const qlonglong &> &value);
195 const std::pair<swift::misc::CExplicitLatin1String, const uint &> &value);
197 operator<<(QJsonObject &json,
const std::pair<swift::misc::CExplicitLatin1String, const qulonglong &> &value);
199 const std::pair<swift::misc::CExplicitLatin1String, const QString &> &value);
201 operator<<(QJsonObject &json,
const std::pair<swift::misc::CExplicitLatin1String, const std::string &> &value);
203 operator<<(QJsonObject &json,
const std::pair<swift::misc::CExplicitLatin1String, const QStringList &> &value);
205 const std::pair<swift::misc::CExplicitLatin1String, const double &> &value);
207 const std::pair<swift::misc::CExplicitLatin1String, const bool &> &value);
209 operator<<(QJsonObject &json,
const std::pair<swift::misc::CExplicitLatin1String, const QDateTime &> &value);
211 const std::pair<swift::misc::CExplicitLatin1String, const QPixmap &> &value);
213 operator<<(QJsonObject &json,
const std::pair<swift::misc::CExplicitLatin1String, const QByteArray &> &value);
219 QDataStream &
operator<<(QDataStream &s,
const std::string &v);
220 QDataStream &
operator>>(QDataStream &s, std::string &v);
223 namespace swift::misc::json
236 QJsonDocument::JsonFormat format = QJsonDocument::Indented);
249 SWIFT_MISC_EXPORT int firstJsonValueAsInt(
const QString &json,
int defaultValue = -1,
bool *ok =
nullptr);
252 SWIFT_MISC_EXPORT int firstJsonValueAsInt(
const QJsonObject &json,
int defaultValue = -1,
bool *ok =
nullptr);
258 SWIFT_MISC_EXPORT QStringList firstJsonValueAsStringList(
const QJsonObject &json);
264 SWIFT_MISC_EXPORT QJsonObject getIncrementalObject(
const QJsonObject &previousObject,
265 const QJsonObject ¤tObject);
268 SWIFT_MISC_EXPORT QJsonObject applyIncrementalObject(
const QJsonObject &previousObject,
269 const QJsonObject &incrementalObject);
286 SWIFT_MISC_EXPORT bool looksLikeSwiftTypeValuePairJson(
const QJsonObject &
object);
315 bool loadFromJsonFile(T &
object,
const QString &fileNameAndPath,
bool acceptCacheFormat =
false)
318 if (jsonString.isEmpty()) {
return false; }
319 object.convertFromJson(jsonString, acceptCacheFormat);
329 const QString jsonString(
object.toJsonString());
330 if (jsonString.isEmpty()) {
return false; }
static bool writeStringToFile(const QString &content, const QString &fileNameAndPath)
Write string to text file.
static QString readFileToString(const QString &fileNameAndPath)
Read file into string.
QString stringFromJsonObject(const QJsonObject &jsonObject, QJsonDocument::JsonFormat format)
JSON Object from string.
std::enable_if_t< std::is_enum_v< ENUM >, QJsonObject > & operator<<(QJsonObject &json, std::pair< QString, const ENUM & > value)
Specialized JSON serialization for enum.
QJsonObject & appendJsonObject(QJsonObject &target, const QJsonObject &toBeAppended)
Append to first JSON object (concatenate)
QJsonObject jsonObjectFromString(const QString &json, bool acceptCacheFormat)
JSON Object from string.
QJsonArray jsonArrayFromString(const QString &json)
JSON Array from string.
SWIFT_MISC_EXPORT const QJsonValue & operator>>(const QJsonValue &json, int &value)
Streaming operators for QJsonValue (to value)
bool loadFromJsonFile(T &object, const QString &fileNameAndPath, bool acceptCacheFormat=false)
Load JSON file and init by that.
bool saveToJsonFile(const T &object, const QString &fileNameAndPath)
Save to JSON file.
Simple literal type containing a single QLatin1String.
#define SWIFT_MISC_EXPORT
Export a class or function from the library.