1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "qv8jsonwrapper_p.h"
43 #include "qv8engine_p.h"
44 #include "qjsconverter_impl_p.h"
48 QV8JsonWrapper::QV8JsonWrapper()
53 QV8JsonWrapper::~QV8JsonWrapper()
57 void QV8JsonWrapper::init(QV8Engine *engine)
62 void QV8JsonWrapper::destroy()
66 v8::Handle<v8::Value> QV8JsonWrapper::fromJsonValue(const QJsonValue &value)
69 return QJSConverter::toString(value.toString());
70 else if (value.isDouble())
71 return v8::Number::New(value.toDouble());
72 else if (value.isBool())
73 return value.toBool() ? v8::True() : v8::False();
74 else if (value.isArray())
75 return fromJsonArray(value.toArray());
76 else if (value.isObject())
77 return fromJsonObject(value.toObject());
78 else if (value.isNull())
81 return v8::Undefined();
84 QJsonValue QV8JsonWrapper::toJsonValue(v8::Handle<v8::Value> value,
85 V8ObjectSet &visitedObjects)
87 if (value->IsString())
88 return QJsonValue(QJSConverter::toString(value.As<v8::String>()));
89 else if (value->IsNumber())
90 return QJsonValue(value->NumberValue());
91 else if (value->IsBoolean())
92 return QJsonValue(value->BooleanValue());
93 else if (value->IsArray())
94 return toJsonArray(value.As<v8::Array>(), visitedObjects);
95 else if (value->IsObject())
96 return toJsonObject(value.As<v8::Object>(), visitedObjects);
97 else if (value->IsNull())
98 return QJsonValue(QJsonValue::Null);
100 return QJsonValue(QJsonValue::Undefined);
103 v8::Local<v8::Object> QV8JsonWrapper::fromJsonObject(const QJsonObject &object)
105 v8::Local<v8::Object> v8object = v8::Object::New();
106 for (QJsonObject::const_iterator it = object.begin(); it != object.end(); ++it)
107 v8object->Set(QJSConverter::toString(it.key()), fromJsonValue(it.value()));
111 QJsonObject QV8JsonWrapper::toJsonObject(v8::Handle<v8::Value> value,
112 V8ObjectSet &visitedObjects)
115 if (!value->IsObject() || value->IsArray() || value->IsFunction())
118 v8::Handle<v8::Object> v8object(value.As<v8::Object>());
119 if (visitedObjects.contains(v8object)) {
121 // For compatibility with QVariant{List,Map} conversion, we return an
122 // empty object (and no error is thrown).
126 visitedObjects.insert(v8object);
128 v8::Local<v8::Array> propertyNames = m_engine->getOwnPropertyNames(v8object);
129 uint32_t length = propertyNames->Length();
130 for (uint32_t i = 0; i < length; ++i) {
131 v8::Local<v8::Value> name = propertyNames->Get(i);
132 v8::Local<v8::Value> propertyValue = v8object->Get(name);
133 if (!propertyValue->IsFunction())
134 result.insert(QJSConverter::toString(name->ToString()),
135 toJsonValue(propertyValue, visitedObjects));
138 visitedObjects.remove(v8object);
143 v8::Local<v8::Array> QV8JsonWrapper::fromJsonArray(const QJsonArray &array)
145 int size = array.size();
146 v8::Local<v8::Array> v8array = v8::Array::New(size);
147 for (int i = 0; i < size; i++)
148 v8array->Set(i, fromJsonValue(array.at(i)));
152 QJsonArray QV8JsonWrapper::toJsonArray(v8::Handle<v8::Value> value,
153 V8ObjectSet &visitedObjects)
156 if (!value->IsArray())
159 v8::Handle<v8::Array> v8array(value.As<v8::Array>());
160 if (visitedObjects.contains(v8array)) {
162 // For compatibility with QVariant{List,Map} conversion, we return an
163 // empty array (and no error is thrown).
167 visitedObjects.insert(v8array);
169 uint32_t length = v8array->Length();
170 for (uint32_t i = 0; i < length; ++i) {
171 v8::Local<v8::Value> element = v8array->Get(i);
172 if (!element->IsFunction())
173 result.append(toJsonValue(element, visitedObjects));
176 visitedObjects.remove(v8array);