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 "qbinaryjson_p.h"
42
43#include <qjsonobject.h>
44
45QT_BEGIN_NAMESPACE
46
47QBinaryJsonObject::~QBinaryJsonObject()
48{
49 if (d && !d->ref.deref())
50 delete d;
51}
52
53QBinaryJsonObject QBinaryJsonObject::fromJsonObject(const QJsonObject &object)
54{
55 QBinaryJsonObject binary;
56 for (auto it = object.begin(), end = object.end(); it != end; ++it)
57 binary.insert(key: it.key(), value: QBinaryJsonValue::fromJsonValue(json: it.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 QBinaryJsonObject::insert(const QString &key, const QBinaryJsonValue &value)
65{
66 bool latinOrIntValue;
67 uint valueSize = QBinaryJsonPrivate::Value::requiredStorage(v: value, compressed: &latinOrIntValue);
68
69 bool latinKey = QBinaryJsonPrivate::useCompressed(s: key);
70 uint valueOffset = sizeof(QBinaryJsonPrivate::Entry)
71 + QBinaryJsonPrivate::qStringSize(string: key, compress: latinKey);
72 uint requiredSize = valueOffset + valueSize;
73
74 if (!detach(reserve: requiredSize + sizeof(QBinaryJsonPrivate::offset))) // offset for the new index entry
75 return;
76
77 if (!o->length)
78 o->tableOffset = sizeof(QBinaryJsonPrivate::Object);
79
80 bool keyExists = false;
81 uint pos = o->indexOf(key, exists: &keyExists);
82 if (keyExists)
83 ++d->compactionCounter;
84
85 uint off = o->reserveSpace(dataSize: requiredSize, posInTable: pos, numItems: 1, replace: keyExists);
86 if (!off)
87 return;
88
89 QBinaryJsonPrivate::Entry *e = o->entryAt(i: pos);
90 e->value.type = value.t;
91 e->value.latinKey = latinKey;
92 e->value.latinOrIntValue = latinOrIntValue;
93 e->value.value = QBinaryJsonPrivate::Value::valueToStore(
94 v: value, offset: reinterpret_cast<char *>(e) - reinterpret_cast<char *>(o) + valueOffset);
95 QBinaryJsonPrivate::copyString(dest: reinterpret_cast<char *>(e + 1), str: key, compress: latinKey);
96 if (valueSize) {
97 QBinaryJsonPrivate::Value::copyData(v: value, dest: reinterpret_cast<char *>(e) + valueOffset,
98 compressed: latinOrIntValue);
99 }
100
101 if (d->compactionCounter > 32U && d->compactionCounter >= unsigned(o->length) / 2U)
102 compact();
103}
104
105char *QBinaryJsonObject::takeRawData(uint *size) const
106{
107 if (d)
108 return d->takeRawData(size);
109 *size = 0;
110 return nullptr;
111}
112
113bool QBinaryJsonObject::detach(uint reserve)
114{
115 if (!d) {
116 if (reserve >= QBinaryJsonPrivate::Value::MaxSize) {
117 qWarning(msg: "QBinaryJson: Document too large to store in data structure");
118 return false;
119 }
120 d = new QBinaryJsonPrivate::MutableData(reserve, QJsonValue::Object);
121 o = static_cast<QBinaryJsonPrivate::Object *>(d->header->root());
122 d->ref.ref();
123 return true;
124 }
125 if (reserve == 0 && d->ref.loadRelaxed() == 1)
126 return true;
127
128 QBinaryJsonPrivate::MutableData *x = d->clone(b: o, reserve);
129 if (!x)
130 return false;
131 x->ref.ref();
132 if (!d->ref.deref())
133 delete d;
134 d = x;
135 o = static_cast<QBinaryJsonPrivate::Object *>(d->header->root());
136 return true;
137}
138
139void QBinaryJsonObject::compact()
140{
141 if (!d || !d->compactionCounter)
142 return;
143
144 detach();
145 d->compact();
146 o = static_cast<QBinaryJsonPrivate::Object *>(d->header->root());
147}
148
149QT_END_NAMESPACE
150

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