LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
parsejson.h
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 #pragma once
10 
11 #include <QByteArray>
12 #include <QVariant>
13 #include <QIODevice>
14 #include <QtDebug>
15 #include <QJsonDocument>
16 
17 namespace LC
18 {
19 namespace Util
20 {
28  inline QVariant ParseJson (const QByteArray& bytes, const char *context)
29  {
30  QJsonParseError error;
31  const auto& result = QJsonDocument::fromJson (bytes, &error).toVariant ();
32  if (error.error != QJsonParseError::NoError)
33  {
34  qWarning () << context
35  << "cannot parse"
36  << error.errorString ();
37  return {};
38  }
39  return result;
40  }
41 
53  inline QVariant ParseJson (QIODevice *device, const char *context)
54  {
55  return ParseJson (device->readAll (), context);
56  }
57 }
58 }
QVariant ParseJson(const QByteArray &bytes, const char *context)
Parses JSON content in the given bytearray.
Definition: parsejson.h:28