mirror of
https://github.com/thoughtbot/capybara-webkit
synced 2023-03-27 23:22:28 -04:00
Extract JSON serializer
This commit is contained in:
parent
11c596f7a0
commit
c0e896b064
5 changed files with 105 additions and 82 deletions
|
@ -1,85 +1,14 @@
|
|||
#include "Evaluate.h"
|
||||
#include "WebPage.h"
|
||||
#include "WebPageManager.h"
|
||||
#include "JsonSerializer.h"
|
||||
#include <iostream>
|
||||
|
||||
Evaluate::Evaluate(WebPageManager *manager, QStringList &arguments, QObject *parent) : SocketCommand(manager, arguments, parent) {
|
||||
m_buffer = "";
|
||||
}
|
||||
|
||||
void Evaluate::start() {
|
||||
QVariant result = page()->currentFrame()->evaluateJavaScript(arguments()[0]);
|
||||
addVariant(result);
|
||||
emitFinished(true, m_buffer);
|
||||
}
|
||||
|
||||
void Evaluate::addVariant(QVariant &object) {
|
||||
if (object.isValid()) {
|
||||
switch(object.type()) {
|
||||
case QMetaType::QString:
|
||||
{
|
||||
QString string = object.toString();
|
||||
addString(string);
|
||||
}
|
||||
break;
|
||||
case QMetaType::QVariantList:
|
||||
{
|
||||
QVariantList list = object.toList();
|
||||
addArray(list);
|
||||
}
|
||||
break;
|
||||
case QMetaType::Double:
|
||||
m_buffer.append(object.toString());
|
||||
break;
|
||||
case QMetaType::QVariantMap:
|
||||
{
|
||||
QVariantMap map = object.toMap();
|
||||
addMap(map);
|
||||
break;
|
||||
}
|
||||
case QMetaType::Bool:
|
||||
{
|
||||
m_buffer.append(object.toString());
|
||||
break;
|
||||
}
|
||||
default:
|
||||
m_buffer.append("null");
|
||||
}
|
||||
} else {
|
||||
m_buffer.append("null");
|
||||
}
|
||||
}
|
||||
|
||||
void Evaluate::addString(QString &string) {
|
||||
QString escapedString(string);
|
||||
escapedString.replace("\"", "\\\"");
|
||||
m_buffer.append("\"");
|
||||
m_buffer.append(escapedString);
|
||||
m_buffer.append("\"");
|
||||
}
|
||||
|
||||
void Evaluate::addArray(QVariantList &list) {
|
||||
m_buffer.append("[");
|
||||
for (int i = 0; i < list.length(); i++) {
|
||||
if (i > 0)
|
||||
m_buffer.append(",");
|
||||
addVariant(list[i]);
|
||||
}
|
||||
m_buffer.append("]");
|
||||
}
|
||||
|
||||
void Evaluate::addMap(QVariantMap &map) {
|
||||
m_buffer.append("{");
|
||||
QMapIterator<QString, QVariant> iterator(map);
|
||||
while (iterator.hasNext()) {
|
||||
iterator.next();
|
||||
QString key = iterator.key();
|
||||
QVariant value = iterator.value();
|
||||
addString(key);
|
||||
m_buffer.append(":");
|
||||
addVariant(value);
|
||||
if (iterator.hasNext())
|
||||
m_buffer.append(",");
|
||||
}
|
||||
m_buffer.append("}");
|
||||
JsonSerializer serializer;
|
||||
emitFinished(true, serializer.serialize(result));
|
||||
}
|
||||
|
|
|
@ -8,13 +8,5 @@ class Evaluate : public SocketCommand {
|
|||
public:
|
||||
Evaluate(WebPageManager *, QStringList &arguments, QObject *parent = 0);
|
||||
virtual void start();
|
||||
|
||||
private:
|
||||
void addVariant(QVariant &object);
|
||||
void addString(QString &string);
|
||||
void addArray(QVariantList &list);
|
||||
void addMap(QVariantMap &map);
|
||||
|
||||
QString m_buffer;
|
||||
};
|
||||
|
||||
|
|
81
src/JsonSerializer.cpp
Normal file
81
src/JsonSerializer.cpp
Normal file
|
@ -0,0 +1,81 @@
|
|||
#include "JsonSerializer.h"
|
||||
|
||||
JsonSerializer::JsonSerializer(QObject *parent) : QObject(parent) {
|
||||
}
|
||||
|
||||
QString JsonSerializer::serialize(QVariant &object) {
|
||||
addVariant(object);
|
||||
return m_buffer;
|
||||
}
|
||||
|
||||
void JsonSerializer::addVariant(QVariant &object) {
|
||||
if (object.isValid()) {
|
||||
switch(object.type()) {
|
||||
case QMetaType::QString:
|
||||
{
|
||||
QString string = object.toString();
|
||||
addString(string);
|
||||
}
|
||||
break;
|
||||
case QMetaType::QVariantList:
|
||||
{
|
||||
QVariantList list = object.toList();
|
||||
addArray(list);
|
||||
}
|
||||
break;
|
||||
case QMetaType::Double:
|
||||
m_buffer.append(object.toString());
|
||||
break;
|
||||
case QMetaType::QVariantMap:
|
||||
{
|
||||
QVariantMap map = object.toMap();
|
||||
addMap(map);
|
||||
break;
|
||||
}
|
||||
case QMetaType::Bool:
|
||||
{
|
||||
m_buffer.append(object.toString());
|
||||
break;
|
||||
}
|
||||
default:
|
||||
m_buffer.append("null");
|
||||
}
|
||||
} else {
|
||||
m_buffer.append("null");
|
||||
}
|
||||
}
|
||||
|
||||
void JsonSerializer::addString(QString &string) {
|
||||
QString escapedString(string);
|
||||
escapedString.replace("\"", "\\\"");
|
||||
m_buffer.append("\"");
|
||||
m_buffer.append(escapedString);
|
||||
m_buffer.append("\"");
|
||||
}
|
||||
|
||||
void JsonSerializer::addArray(QVariantList &list) {
|
||||
m_buffer.append("[");
|
||||
for (int i = 0; i < list.length(); i++) {
|
||||
if (i > 0)
|
||||
m_buffer.append(",");
|
||||
addVariant(list[i]);
|
||||
}
|
||||
m_buffer.append("]");
|
||||
}
|
||||
|
||||
void JsonSerializer::addMap(QVariantMap &map) {
|
||||
m_buffer.append("{");
|
||||
QMapIterator<QString, QVariant> iterator(map);
|
||||
while (iterator.hasNext()) {
|
||||
iterator.next();
|
||||
QString key = iterator.key();
|
||||
QVariant value = iterator.value();
|
||||
addString(key);
|
||||
m_buffer.append(":");
|
||||
addVariant(value);
|
||||
if (iterator.hasNext())
|
||||
m_buffer.append(",");
|
||||
}
|
||||
m_buffer.append("}");
|
||||
}
|
||||
|
19
src/JsonSerializer.h
Normal file
19
src/JsonSerializer.h
Normal file
|
@ -0,0 +1,19 @@
|
|||
#include <QObject>
|
||||
#include <QVariantList>
|
||||
|
||||
class JsonSerializer : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
JsonSerializer(QObject *parent = 0);
|
||||
QString serialize(QVariant &object);
|
||||
|
||||
private:
|
||||
void addVariant(QVariant &object);
|
||||
void addString(QString &string);
|
||||
void addArray(QVariantList &list);
|
||||
void addMap(QVariantMap &map);
|
||||
|
||||
QString m_buffer;
|
||||
};
|
||||
|
|
@ -55,6 +55,7 @@ HEADERS = \
|
|||
TimeoutCommand.h \
|
||||
SetUrlBlacklist.h \
|
||||
NoOpReply.h \
|
||||
JsonSerializer.h
|
||||
|
||||
SOURCES = \
|
||||
EnableLogging.cpp \
|
||||
|
@ -111,6 +112,7 @@ SOURCES = \
|
|||
TimeoutCommand.cpp \
|
||||
SetUrlBlacklist.cpp \
|
||||
NoOpReply.cpp \
|
||||
JsonSerializer.cpp
|
||||
|
||||
RESOURCES = webkit_server.qrc
|
||||
QT += network webkit
|
||||
|
|
Loading…
Reference in a new issue