1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore 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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qbinaryjsonobject_p.h"
41 #include "qbinaryjsonvalue_p.h"
42 #include "qbinaryjsonarray_p.h"
43 #include "qbinaryjson_p.h"
44 
45 #include <qjsonarray.h>
46 #include <qjsonobject.h>
47 
48 QT_BEGIN_NAMESPACE
49 
QBinaryJsonValue(QBinaryJsonPrivate::MutableData * data,QBinaryJsonPrivate::Base * parent,const QBinaryJsonPrivate::Value & v)50 QBinaryJsonValue::QBinaryJsonValue(QBinaryJsonPrivate::MutableData *data,
51                                    QBinaryJsonPrivate::Base *parent,
52                                    const QBinaryJsonPrivate::Value &v)
53     : t(QJsonValue::Type(uint(v.type)))
54 {
55     switch (t) {
56     case QJsonValue::Undefined:
57     case QJsonValue::Null:
58         dbl = 0;
59         break;
60     case QJsonValue::Bool:
61         b = v.toBoolean();
62         break;
63     case QJsonValue::Double:
64         dbl = v.toDouble(parent);
65         break;
66     case QJsonValue::String: {
67         QString s = v.toString(parent);
68         stringData = s.data_ptr();
69         stringData->ref.ref();
70         break;
71     }
72     case QJsonValue::Array:
73     case QJsonValue::Object:
74         d = data;
75         base = v.base(parent);
76         break;
77     }
78     if (d)
79         d->ref.ref();
80 }
81 
QBinaryJsonValue(QString string)82 QBinaryJsonValue::QBinaryJsonValue(QString string)
83     : stringData(*reinterpret_cast<QStringData **>(&string)), t(QJsonValue::String)
84 {
85     stringData->ref.ref();
86 }
87 
QBinaryJsonValue(const QBinaryJsonArray & a)88 QBinaryJsonValue::QBinaryJsonValue(const QBinaryJsonArray &a)
89     : base(a.a), d(a.d), t(QJsonValue::Array)
90 {
91     if (d)
92         d->ref.ref();
93 }
94 
QBinaryJsonValue(const QBinaryJsonObject & o)95 QBinaryJsonValue::QBinaryJsonValue(const QBinaryJsonObject &o)
96     : base(o.o), d(o.d), t(QJsonValue::Object)
97 {
98     if (d)
99         d->ref.ref();
100 }
101 
~QBinaryJsonValue()102 QBinaryJsonValue::~QBinaryJsonValue()
103 {
104     if (t == QJsonValue::String && stringData && !stringData->ref.deref())
105         free(stringData);
106 
107     if (d && !d->ref.deref())
108         delete d;
109 }
110 
fromJsonValue(const QJsonValue & json)111 QBinaryJsonValue QBinaryJsonValue::fromJsonValue(const QJsonValue &json)
112 {
113     switch (json.type()) {
114     case QJsonValue::Bool:
115         return QBinaryJsonValue(json.toBool());
116     case QJsonValue::Double:
117         return QBinaryJsonValue(json.toDouble());
118     case QJsonValue::String:
119         return QBinaryJsonValue(json.toString());
120     case QJsonValue::Array:
121         return QBinaryJsonArray::fromJsonArray(json.toArray());
122     case QJsonValue::Object:
123         return QBinaryJsonObject::fromJsonObject(json.toObject());
124     case QJsonValue::Null:
125         return QBinaryJsonValue(QJsonValue::Null);
126     case QJsonValue::Undefined:
127         return QBinaryJsonValue(QJsonValue::Undefined);
128     }
129     Q_UNREACHABLE();
130     return QBinaryJsonValue(QJsonValue::Null);
131 }
132 
toString() const133 QString QBinaryJsonValue::toString() const
134 {
135     if (t != QJsonValue::String)
136         return QString();
137     stringData->ref.ref(); // the constructor below doesn't add a ref.
138     QStringDataPtr holder = { stringData };
139     return QString(holder);
140 }
141 
detach()142 void QBinaryJsonValue::detach()
143 {
144     if (!d)
145         return;
146 
147     QBinaryJsonPrivate::MutableData *x = d->clone(base);
148     x->ref.ref();
149     if (!d->ref.deref())
150         delete d;
151     d = x;
152     base = static_cast<QBinaryJsonPrivate::Object *>(d->header->root());
153 }
154 
155 QT_END_NAMESPACE
156