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{
19 {
20 return QJsonDocument::fromVariant (var)
21 .toJson (compact ? QJsonDocument::Compact : QJsonDocument::Indented);
22 }
23
25
27 {
29 if (!file.open (QIODevice::WriteOnly))
30 {
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 {
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 L &l)
Definition either.h:119
static Either Right(R &&r)
Definition either.h:124
Container< T > Filter(const Container< T > &c, F f)
Definition prelude.h:118
SerializeResult_t SerializeJsonToFile(const QString &filename, const QVariant &var, bool compact)
QByteArray SerializeJson(const QVariant &var, bool compact)
Serializes the given var to JSON representation.
Definition constants.h:15