Skip to main content

cxx_qt_lib/core/
qjsonvalue.rs

1// SPDX-FileCopyrightText: 2026 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
2// SPDX-FileContributor: Yuri Knigavko <yuri.knigavko@qt.io>
3//
4// SPDX-License-Identifier: MIT OR Apache-2.0
5
6use crate::{QJsonArray, QJsonObject, QString};
7use std::fmt;
8use std::mem::MaybeUninit;
9
10#[cxx::bridge]
11mod ffi {
12    unsafe extern "C++" {
13        include!("cxx-qt-lib/qjsonvalue.h");
14        type QJsonValue = super::QJsonValue;
15
16        include!("cxx-qt-lib/qjsonarray.h");
17        type QJsonArray = crate::QJsonArray;
18
19        include!("cxx-qt-lib/qjsonobject.h");
20        type QJsonObject = crate::QJsonObject;
21
22        include!("cxx-qt-lib/qstring.h");
23        type QString = crate::QString;
24
25        /// Returns `true` if the value is null.
26        #[rust_name = "is_null"]
27        fn isNull(self: &QJsonValue) -> bool;
28
29        /// Returns `true` if the value is undefined. This can occur for example if you
30        /// query a non-existent key in a `QJsonObject`.
31        #[rust_name = "is_undefined"]
32        fn isUndefined(self: &QJsonValue) -> bool;
33
34        /// Returns `true` if the value contains a boolean.
35        #[rust_name = "is_bool"]
36        fn isBool(self: &QJsonValue) -> bool;
37
38        /// Returns `true` if the value contains a number (double).
39        #[rust_name = "is_double"]
40        fn isDouble(self: &QJsonValue) -> bool;
41
42        /// Returns `true` if the value contains a string.
43        #[rust_name = "is_string"]
44        fn isString(self: &QJsonValue) -> bool;
45
46        /// Returns `true` if the value contains an array.
47        #[rust_name = "is_array"]
48        fn isArray(self: &QJsonValue) -> bool;
49
50        /// Returns `true` if the value contains an object.
51        #[rust_name = "is_object"]
52        fn isObject(self: &QJsonValue) -> bool;
53
54        /// Converts the value to a `bool` and returns it.
55        ///
56        /// If the value does not contain a boolean, `default_value` is returned.
57        #[rust_name = "to_bool_or"]
58        fn toBool(self: &QJsonValue, default_value: bool) -> bool;
59
60        /// Converts the value to a `f64` and returns it.
61        ///
62        /// If the value does not contain a double, `default_value` is returned.
63        #[rust_name = "to_double_or"]
64        fn toDouble(self: &QJsonValue, default_value: f64) -> f64;
65
66        /// Converts the value to an `i32` and returns it.
67        ///
68        /// If the value does not contain a double, or is not a whole number, `default_value` is returned.
69        #[rust_name = "to_int_or"]
70        fn toInt(self: &QJsonValue, default_value: i32) -> i32;
71
72        /// Converts the value to a [`QString`] and returns it.
73        ///
74        /// If the value does not contain a string, `default_value` is returned.
75        #[rust_name = "to_string_or"]
76        fn toString(self: &QJsonValue, default_value: &QString) -> QString;
77
78        /// Converts the value to a [`QJsonArray`] and returns it.
79        ///
80        /// If the value does not contain an array, `default_value` is returned.
81        #[rust_name = "to_array_or"]
82        fn toArray(self: &QJsonValue, default_value: &QJsonArray) -> QJsonArray;
83
84        /// Converts the value to a [`QJsonObject`] and returns it.
85        ///
86        /// If the value does not contain an object, `default_value` is returned.
87        #[rust_name = "to_object_or"]
88        fn toObject(self: &QJsonValue, default_value: &QJsonObject) -> QJsonObject;
89    }
90
91    #[namespace = "rust::cxxqtlib1"]
92    unsafe extern "C++" {
93        include!("cxx-qt-lib/common.h");
94
95        #[doc(hidden)]
96        #[rust_name = "qjsonvalue_drop"]
97        fn drop(value: &mut QJsonValue);
98
99        #[doc(hidden)]
100        #[rust_name = "qjsonvalue_init_default"]
101        fn construct() -> QJsonValue;
102
103        #[doc(hidden)]
104        #[rust_name = "qjsonvalue_init_from_qjsonvalue"]
105        fn construct(value: &QJsonValue) -> QJsonValue;
106
107        #[doc(hidden)]
108        #[rust_name = "qjsonvalue_init_from_bool"]
109        fn construct(value: bool) -> QJsonValue;
110
111        #[doc(hidden)]
112        #[rust_name = "qjsonvalue_init_from_i64"]
113        fn qjsonvalueFromI64(value: i64) -> QJsonValue;
114
115        #[doc(hidden)]
116        #[rust_name = "qjsonvalue_init_from_f64"]
117        fn construct(value: f64) -> QJsonValue;
118
119        #[doc(hidden)]
120        #[rust_name = "qjsonvalue_init_from_qstring"]
121        fn construct(value: &QString) -> QJsonValue;
122
123        #[doc(hidden)]
124        #[rust_name = "qjsonvalue_init_from_qjsonarray"]
125        fn construct(value: &QJsonArray) -> QJsonValue;
126
127        #[doc(hidden)]
128        #[rust_name = "qjsonvalue_init_from_qjsonobject"]
129        fn construct(value: &QJsonObject) -> QJsonValue;
130
131        #[doc(hidden)]
132        #[rust_name = "qjsonvalue_eq"]
133        fn operatorEq(a: &QJsonValue, b: &QJsonValue) -> bool;
134
135        #[doc(hidden)]
136        #[rust_name = "qjsonvalue_to_debug_qstring"]
137        fn toDebugQString(value: &QJsonValue) -> QString;
138    }
139}
140
141/// The `QJsonValue` class encapsulates a value in JSON.
142///
143/// The default-constructed value is `QJsonValue::Null`.
144///
145/// Qt Documentation: [QJsonValue](https://doc.qt.io/qt/qjsonvalue.html#details)
146#[repr(C)]
147pub struct QJsonValue {
148    _n: MaybeUninit<usize>,
149    _container: MaybeUninit<usize>,
150    _t: MaybeUninit<usize>,
151}
152
153impl Default for QJsonValue {
154    /// Constructs a null value.
155    fn default() -> Self {
156        ffi::qjsonvalue_init_default()
157    }
158}
159
160impl Drop for QJsonValue {
161    fn drop(&mut self) {
162        ffi::qjsonvalue_drop(self);
163    }
164}
165
166impl Clone for QJsonValue {
167    fn clone(&self) -> Self {
168        ffi::qjsonvalue_init_from_qjsonvalue(self)
169    }
170}
171
172impl PartialEq for QJsonValue {
173    fn eq(&self, other: &Self) -> bool {
174        ffi::qjsonvalue_eq(self, other)
175    }
176}
177
178impl fmt::Debug for QJsonValue {
179    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
180        ffi::qjsonvalue_to_debug_qstring(self).fmt(f)
181    }
182}
183
184impl From<bool> for QJsonValue {
185    fn from(value: bool) -> Self {
186        ffi::qjsonvalue_init_from_bool(value)
187    }
188}
189
190impl From<i64> for QJsonValue {
191    fn from(value: i64) -> Self {
192        ffi::qjsonvalue_init_from_i64(value)
193    }
194}
195
196impl From<f64> for QJsonValue {
197    fn from(value: f64) -> Self {
198        ffi::qjsonvalue_init_from_f64(value)
199    }
200}
201
202impl From<&QString> for QJsonValue {
203    fn from(value: &QString) -> Self {
204        ffi::qjsonvalue_init_from_qstring(value)
205    }
206}
207
208impl From<&QJsonArray> for QJsonValue {
209    fn from(value: &QJsonArray) -> Self {
210        ffi::qjsonvalue_init_from_qjsonarray(value)
211    }
212}
213
214impl From<&QJsonObject> for QJsonValue {
215    fn from(value: &QJsonObject) -> Self {
216        ffi::qjsonvalue_init_from_qjsonobject(value)
217    }
218}
219
220// Safety:
221//
222// Static checks on the C++ side ensure that QJsonValue is trivial.
223unsafe impl cxx::ExternType for QJsonValue {
224    type Id = cxx::type_id!("QJsonValue");
225    type Kind = cxx::kind::Trivial;
226}
227
228impl QJsonValue {
229    /// Converts the value to a `bool`. Returns `false` if the value is not a boolean.
230    pub fn to_bool(&self) -> bool {
231        self.to_bool_or(false)
232    }
233
234    /// Converts the value to a `f64`. Returns `0.0` if the value is not a double.
235    pub fn to_double(&self) -> f64 {
236        self.to_double_or(0.0)
237    }
238
239    /// Converts the value to a `i32`. Returns `0` if the value is not an integer.
240    pub fn to_int(&self) -> i32 {
241        self.to_int_or(0)
242    }
243
244    /// Converts the value to a [`QString`]. Returns an empty string if the value is
245    /// not a string.
246    pub fn to_string(&self) -> QString {
247        self.to_string_or(&QString::default())
248    }
249
250    /// Converts the value to a [`QJsonArray`]. Returns an empty array if the value
251    /// is not an array.
252    pub fn to_array(&self) -> QJsonArray {
253        self.to_array_or(&QJsonArray::default())
254    }
255
256    /// Converts the value to a [`QJsonObject`]. Returns an empty object if the value
257    /// is not an object.
258    pub fn to_object(&self) -> QJsonObject {
259        self.to_object_or(&QJsonObject::default())
260    }
261}