1 | // Copyright (C) 2016 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qbinaryjsonobject_p.h" |
5 | #include "qbinaryjsonvalue_p.h" |
6 | #include "qbinaryjsonarray_p.h" |
7 | #include "qbinaryjson_p.h" |
8 | |
9 | #include <qjsonarray.h> |
10 | #include <qjsonobject.h> |
11 | |
12 | QT_BEGIN_NAMESPACE |
13 | |
14 | QBinaryJsonValue::QBinaryJsonValue(QBinaryJsonPrivate::MutableData *data, |
15 | QBinaryJsonPrivate::Base *parent, |
16 | const QBinaryJsonPrivate::Value &v) |
17 | : t(QJsonValue::Type(v.type())) |
18 | { |
19 | switch (t) { |
20 | case QJsonValue::Undefined: |
21 | case QJsonValue::Null: |
22 | dbl = 0; |
23 | break; |
24 | case QJsonValue::Bool: |
25 | b = v.toBoolean(); |
26 | break; |
27 | case QJsonValue::Double: |
28 | dbl = v.toDouble(b: parent); |
29 | break; |
30 | case QJsonValue::String: |
31 | stringData = v.toString(b: parent); |
32 | break; |
33 | case QJsonValue::Array: |
34 | case QJsonValue::Object: |
35 | d = data; |
36 | base = v.base(b: parent); |
37 | break; |
38 | } |
39 | if (d) |
40 | d->ref.ref(); |
41 | } |
42 | |
43 | QBinaryJsonValue::QBinaryJsonValue(QString string) |
44 | : d(nullptr), t(QJsonValue::String) |
45 | { |
46 | stringData = std::move(string); |
47 | } |
48 | |
49 | QBinaryJsonValue::QBinaryJsonValue(const QBinaryJsonArray &a) |
50 | : base(a.a), d(a.d), t(QJsonValue::Array) |
51 | { |
52 | if (d) |
53 | d->ref.ref(); |
54 | } |
55 | |
56 | QBinaryJsonValue::QBinaryJsonValue(const QBinaryJsonObject &o) |
57 | : base(o.o), d(o.d), t(QJsonValue::Object) |
58 | { |
59 | if (d) |
60 | d->ref.ref(); |
61 | } |
62 | |
63 | QBinaryJsonValue::~QBinaryJsonValue() |
64 | { |
65 | if (d && !d->ref.deref()) |
66 | delete d; |
67 | } |
68 | |
69 | QBinaryJsonValue QBinaryJsonValue::fromJsonValue(const QJsonValue &json) |
70 | { |
71 | switch (json.type()) { |
72 | case QJsonValue::Bool: |
73 | return QBinaryJsonValue(json.toBool()); |
74 | case QJsonValue::Double: |
75 | return QBinaryJsonValue(json.toDouble()); |
76 | case QJsonValue::String: |
77 | return QBinaryJsonValue(json.toString()); |
78 | case QJsonValue::Array: |
79 | return QBinaryJsonArray::fromJsonArray(array: json.toArray()); |
80 | case QJsonValue::Object: |
81 | return QBinaryJsonObject::fromJsonObject(object: json.toObject()); |
82 | case QJsonValue::Null: |
83 | return QBinaryJsonValue(QJsonValue::Null); |
84 | case QJsonValue::Undefined: |
85 | return QBinaryJsonValue(QJsonValue::Undefined); |
86 | } |
87 | Q_UNREACHABLE_RETURN(QBinaryJsonValue(QJsonValue::Null)); |
88 | } |
89 | |
90 | QString QBinaryJsonValue::toString() const |
91 | { |
92 | if (t != QJsonValue::String) |
93 | return QString(); |
94 | return stringData; |
95 | } |
96 | |
97 | void QBinaryJsonValue::detach() |
98 | { |
99 | if (!d) |
100 | return; |
101 | |
102 | QBinaryJsonPrivate::MutableData *x = d->clone(b: base); |
103 | x->ref.ref(); |
104 | if (!d->ref.deref()) |
105 | delete d; |
106 | d = x; |
107 | base = static_cast<QBinaryJsonPrivate::Object *>(d->header->root()); |
108 | } |
109 | |
110 | QT_END_NAMESPACE |
111 |