LeechCraft 0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
serializejson.cpp
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#include "serializejson.h"
10#include <QJsonDocument>
11#include <QFile>
12#include "either.h"
13
14namespace LC
15{
16namespace Util
17{
18 QByteArray SerializeJson (const QVariant& var, bool compact)
19 {
20 return QJsonDocument::fromVariant (var)
21 .toJson (compact ? QJsonDocument::Compact : QJsonDocument::Indented);
22 }
23
25
26 SerializeResult_t SerializeJsonToFile (const QString& filename, const QVariant& var, bool compact)
27 {
28 QFile file { filename };
29 if (!file.open (QIODevice::WriteOnly))
30 {
31 qWarning () << Q_FUNC_INFO
32 << "unable to open file"
33 << file.fileName ()
34 << "for writing:"
35 << file.errorString ();
36 return SerializeResult_t::Left (file.errorString ());
37 }
38
39 if (!file.write (SerializeJson (var, compact)))
40 {
41 qWarning () << Q_FUNC_INFO
42 << "unable to write to file"
43 << file.fileName ()
44 << ":"
45 << file.errorString ();
46 return SerializeResult_t::Left (file.errorString ());
47 }
48
49 return SerializeResult_t::Right ({});
50 }
51}
52}
static Either Left(const QString &l)
Definition either.h:119
static Either Right(Void &&r)
Definition either.h:124
SerializeResult_t SerializeJsonToFile(const QString &filename, const QVariant &var, bool compact)
Either< QString, Void > SerializeResult_t
QByteArray SerializeJson(const QVariant &var, bool compact)
Serializes the given var to JSON representation.
Definition constants.h:15