Permalink
Browse files

Extract JSON serializer

  • Loading branch information...
1 parent 11c596f commit c0e896b064cc2f7230ec0fb06962a69faeea1cbe @mhoran mhoran committed Dec 29, 2012
Showing with 105 additions and 82 deletions.
  1. +3 −74 src/Evaluate.cpp
  2. +0 −8 src/Evaluate.h
  3. +81 −0 src/JsonSerializer.cpp
  4. +19 −0 src/JsonSerializer.h
  5. +2 −0 src/webkit_server.pro
View
@@ -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));
}
View
@@ -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;
};
View
@@ -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("}");
+}
+
View
@@ -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;
+};
+
View
@@ -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

0 comments on commit c0e896b

Please sign in to comment.