LeechCraft 0.6.70-17335-ge406ffdcaf
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
json.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 "json.h"
10#include <QJsonDocument>
11#include <QtDebug>
12#include "either.h"
13
14namespace LC::Util
15{
16 Either<QString, QJsonDocument> ToJson (const QByteArray& json)
17 {
18 QJsonParseError error;
19 auto doc = QJsonDocument::fromJson (json, &error);
20 if (error.error == QJsonParseError::NoError)
22 return Either<QString, QJsonDocument>::Left (error.errorString ());
23 }
24
25 namespace
26 {
27 std::string MakeUnexpectedMessage (QJsonValue::Type expected, const auto& value)
28 {
29 QString result;
30 QDebug dbg { &result };
31 dbg << "unexpected JSON: expected" << expected << "but got" << value;
32 return result.toStdString ();
33 }
34 }
35
36 UnexpectedJson::UnexpectedJson (QJsonValue::Type expected, const QJsonValue& value)
37 : std::runtime_error { MakeUnexpectedMessage (expected, value) }
38 {
39 }
40
41 UnexpectedJson::UnexpectedJson (QJsonValue::Type expected, const QJsonDocument& doc)
42 : std::runtime_error { MakeUnexpectedMessage (expected, doc) }
43 {
44 }
45}
static Either Left(const LL &l)
Definition either.h:126
static Either Right(R &&r)
Definition either.h:134
UnexpectedJson(QJsonValue::Type expected, const QJsonValue &)
Definition json.cpp:36
Either< QString, QJsonDocument > ToJson(const QByteArray &json)
Definition json.cpp:16