Skip to content

Commit

Permalink
Move parse and stringify to Json utility class.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed Jan 31, 2013
1 parent eb054c7 commit 4fd5640
Showing 7 changed files with 53 additions and 35 deletions.
1 change: 1 addition & 0 deletions include/QtxJson
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
#define QTXJSON_MODULE_H

#include "../src/json/jsonglobal.h"
#include "../src/json/json.h"
#include "../src/json/jsonreader.h"
#include "../src/json/jsonstreamreader.h"
#include "../src/json/jsonstreamwriter.h"
32 changes: 32 additions & 0 deletions src/json/json.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "json.h"
#include "jsonreader.h"

QTX_BEGIN_NAMESPACE


QHash<QString, QVariant> Json::parse(const QByteArray & data)
{
Q_UNUSED(data)

//qDebug() << "Static parse data";

JsonReader reader;
reader.mKeepRootContext = true;
reader.addData(data);
reader.parse();

// @todo: should check for errors at this point.

if (reader.mRootContext) {
return reader.mRootContext->object();
}

return QHash<QString, QVariant>();
}

QString JsonWriter::stringify(const QHash<QString, QVariant> & object)
{
JsonWriter writer;
QByteArray bytes = writer.write(object);
return QString::fromUtf8(bytes);
}
19 changes: 19 additions & 0 deletions src/json/json.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef QTXJSON_JSON_H
#define QTXJSON_JSON_H

#include "jsonglobal.h"
#include <QtCore>

QTX_BEGIN_NAMESPACE


class Json {
public:
static QHash<QString, QVariant> parse(const QByteArray & data);
static QString stringify(const QHash<QString, QVariant> & object);
};


QTX_END_NAMESPACE

#endif // QTXJSON_JSON_H
21 changes: 0 additions & 21 deletions src/json/jsonreader.cpp
Original file line number Diff line number Diff line change
@@ -3,27 +3,6 @@
QTX_BEGIN_NAMESPACE


QHash<QString, QVariant> JsonReader::parse(const QByteArray & data)
{
Q_UNUSED(data)

//qDebug() << "Static parse data";

JsonReader reader;
reader.mKeepRootContext = true;
reader.addData(data);
reader.parse();

// @todo: should check for errors at this point.

if (reader.mRootContext) {
return reader.mRootContext->object();
}

return QHash<QString, QVariant>();
}


JsonReader::JsonReader(QObject * parent)
: QObject(parent),
mKeepRootContext(false),
5 changes: 1 addition & 4 deletions src/json/jsonreader.h
Original file line number Diff line number Diff line change
@@ -13,10 +13,7 @@ class JsonReaderContext;
class JsonReader : public QObject
{
Q_OBJECT

public:
static QHash<QString, QVariant> parse(const QByteArray & data);


public:
JsonReader(QObject * parent = 0);
~JsonReader();
7 changes: 0 additions & 7 deletions src/json/jsonwriter.cpp
Original file line number Diff line number Diff line change
@@ -3,13 +3,6 @@
QTX_BEGIN_NAMESPACE


QString JsonWriter::stringify(const QHash<QString, QVariant> & object)
{
JsonWriter writer;
QByteArray bytes = writer.write(object);
return QString::fromUtf8(bytes);
}

JsonWriter::JsonWriter()
{
}
3 changes: 0 additions & 3 deletions src/json/jsonwriter.h
Original file line number Diff line number Diff line change
@@ -10,9 +10,6 @@ QTX_BEGIN_NAMESPACE

class JsonWriter
{
public:
static QString stringify(const QHash<QString, QVariant> & object);

public:
JsonWriter();
~JsonWriter();

0 comments on commit 4fd5640

Please sign in to comment.