LeechCraft  0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
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 
14 namespace 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 }
UnexpectedJson(QJsonValue::Type expected, const QJsonValue &)
Definition: json.cpp:36
Either< QString, QJsonDocument > ToJson(const QByteArray &json)
Definition: json.cpp:16
STL namespace.
static Either Left(const L &l)
Definition: either.h:119
Type
Describes the various types of XDG .desktop files.
Definition: itemtypes.h:23
static Either Right(R &&r)
Definition: either.h:124