6f053bfbb1460a400e984ca1063d0dbdce445fa7
[profile/ivi/qtdeclarative.git] / src / qml / qml / v8 / qv8jsonwrapper.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qv8jsonwrapper_p.h"
43 #include "qv8engine_p.h"
44 #include "qjsconverter_impl_p.h"
45
46 QT_BEGIN_NAMESPACE
47
48 QV8JsonWrapper::QV8JsonWrapper()
49 : m_engine(0)
50 {
51 }
52
53 QV8JsonWrapper::~QV8JsonWrapper()
54 {
55 }
56
57 void QV8JsonWrapper::init(QV8Engine *engine)
58 {
59     m_engine = engine;
60 }
61
62 void QV8JsonWrapper::destroy()
63 {
64 }
65
66 v8::Handle<v8::Value> QV8JsonWrapper::fromJsonValue(const QJsonValue &value)
67 {
68     if (value.isString())
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())
79         return v8::Null();
80     else
81         return v8::Undefined();
82 }
83
84 QJsonValue QV8JsonWrapper::toJsonValue(v8::Handle<v8::Value> value,
85                                        V8ObjectSet &visitedObjects)
86 {
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);
99     else
100         return QJsonValue(QJsonValue::Undefined);
101 }
102
103 v8::Local<v8::Object> QV8JsonWrapper::fromJsonObject(const QJsonObject &object)
104 {
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()));
108     return v8object;
109 }
110
111 QJsonObject QV8JsonWrapper::toJsonObject(v8::Handle<v8::Value> value,
112                                          V8ObjectSet &visitedObjects)
113 {
114     QJsonObject result;
115     if (!value->IsObject() || value->IsArray() || value->IsFunction())
116         return result;
117
118     v8::Handle<v8::Object> v8object(value.As<v8::Object>());
119     if (visitedObjects.contains(v8object)) {
120         // Avoid recursion.
121         // For compatibility with QVariant{List,Map} conversion, we return an
122         // empty object (and no error is thrown).
123         return result;
124     }
125
126     visitedObjects.insert(v8object);
127
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));
136     }
137
138     visitedObjects.remove(v8object);
139
140     return result;
141 }
142
143 v8::Local<v8::Array> QV8JsonWrapper::fromJsonArray(const QJsonArray &array)
144 {
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)));
149     return v8array;
150 }
151
152 QJsonArray QV8JsonWrapper::toJsonArray(v8::Handle<v8::Value> value,
153                                        V8ObjectSet &visitedObjects)
154 {
155     QJsonArray result;
156     if (!value->IsArray())
157         return result;
158
159     v8::Handle<v8::Array> v8array(value.As<v8::Array>());
160     if (visitedObjects.contains(v8array)) {
161         // Avoid recursion.
162         // For compatibility with QVariant{List,Map} conversion, we return an
163         // empty array (and no error is thrown).
164         return result;
165     }
166
167     visitedObjects.insert(v8array);
168
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));
174     }
175
176     visitedObjects.remove(v8array);
177
178     return result;
179 }
180
181 QT_END_NAMESPACE