LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
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 <QFile>
11 #include <QJsonDocument>
12 #include <QtDebug>
13 #include "either.h"
14 
15 namespace LC
16 {
17 namespace Util
18 {
19  QByteArray SerializeJson (const QVariant& var, bool compact)
20  {
21  return QJsonDocument::fromVariant (var)
22  .toJson (compact ? QJsonDocument::Compact : QJsonDocument::Indented);
23  }
24 
25  Either<QString, Void> SerializeJsonToFile (const QString& filename, const QVariant& var, bool compact)
26  {
27  QFile file { filename };
28  if (!file.open (QIODevice::WriteOnly))
29  {
30  qWarning () << "unable to open file" << file.fileName () << "for writing:" << file.errorString ();
31  return Left { file.errorString () };
32  }
33 
34  if (!file.write (SerializeJson (var, compact)))
35  {
36  qWarning () << "unable to write to file" << file.fileName () << ":" << file.errorString ();
37  return Left { file.errorString () };
38  }
39 
40  return Void {};
41  }
42 }
43 }
QByteArray SerializeJson(const QVariant &var, bool compact)
Serializes the given var to JSON representation.
Either< QString, Void > SerializeJsonToFile(const QString &filename, const QVariant &var, bool compact)
A proper void type, akin to unit (or ()) type in functional languages.
Definition: void.h:20