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 "qbinaryjsonarray_p.h"
41#include "qbinaryjson_p.h"
42
43#include <qjsonarray.h>
44
45QT_BEGIN_NAMESPACE
46
47QBinaryJsonArray::~QBinaryJsonArray()
48{
49 if (d && !d->ref.deref())
50 delete d;
51}
52
53QBinaryJsonArray QBinaryJsonArray::fromJsonArray(const QJsonArray &array)
54{
55 QBinaryJsonArray binary;
56 for (const QJsonValue &value : array)
57 binary.append(value: QBinaryJsonValue::fromJsonValue(json: value));
58 if (binary.d) // We want to compact it as it is a root item now
59 binary.d->compactionCounter++;
60 binary.compact();
61 return binary;
62}
63
64void QBinaryJsonArray::append(const QBinaryJsonValue &value)
65{
66 const uint i = a ? a->length : 0;
67
68 bool compressed;
69 uint valueSize = QBinaryJsonPrivate::Value::requiredStorage(v: value, compressed: &compressed);
70
71 if (!detach(reserve: valueSize + sizeof(QBinaryJsonPrivate::Value)))
72 return;
73
74 if (!a->length)
75 a->tableOffset = sizeof(QBinaryJsonPrivate::Array);
76
77 uint valueOffset = a->reserveSpace(dataSize: valueSize, posInTable: i, numItems: 1, replace: false);
78 if (!valueOffset)
79 return;
80
81 QBinaryJsonPrivate::Value *v = a->at(i);
82 v->type = (value.t == QJsonValue::Undefined ? QJsonValue::Null : value.t);
83 v->latinOrIntValue = compressed;
84 v->latinKey = false;
85 v->value = QBinaryJsonPrivate::Value::valueToStore(v: value, offset: valueOffset);
86 if (valueSize) {
87 QBinaryJsonPrivate::Value::copyData(v: value, dest: reinterpret_cast<char *>(a) + valueOffset,
88 compressed);
89 }
90}
91
92char *QBinaryJsonArray::takeRawData(uint *size)
93{
94 if (d)
95 return d->takeRawData(size);
96 *size = 0;
97 return nullptr;
98}
99
100bool QBinaryJsonArray::detach(uint reserve)
101{
102 if (!d) {
103 if (reserve >= QBinaryJsonPrivate::Value::MaxSize) {
104 qWarning(msg: "QBinaryJson: Document too large to store in data structure");
105 return false;
106 }
107 d = new QBinaryJsonPrivate::MutableData(reserve, QJsonValue::Array);
108 a = static_cast<QBinaryJsonPrivate::Array *>(d->header->root());
109 d->ref.ref();
110 return true;
111 }
112 if (reserve == 0 && d->ref.loadRelaxed() == 1)
113 return true;
114
115 QBinaryJsonPrivate::MutableData *x = d->clone(b: a, reserve);
116 if (!x)
117 return false;
118 x->ref.ref();
119 if (!d->ref.deref())
120 delete d;
121 d = x;
122 a = static_cast<QBinaryJsonPrivate::Array *>(d->header->root());
123 return true;
124}
125
126void QBinaryJsonArray::compact()
127{
128 if (!d || !d->compactionCounter)
129 return;
130
131 detach();
132 d->compact();
133 a = static_cast<QBinaryJsonPrivate::Array *>(d->header->root());
134}
135
136QT_END_NAMESPACE
137
138

source code of qtbase/src/corelib/serialization/qbinaryjsonarray.cpp