Skip to main content

cxx_qt_lib/core/
qjsonobject.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::{QJsonValue, QString};
7use std::fmt;
8use std::mem::MaybeUninit;
9
10#[cxx::bridge]
11mod ffi {
12    unsafe extern "C++" {
13        include!("cxx-qt-lib/qjsonobject.h");
14        type QJsonObject = super::QJsonObject;
15
16        include!("cxx-qt-lib/qjsonvalue.h");
17        type QJsonValue = crate::QJsonValue;
18
19        include!("cxx-qt-lib/qstring.h");
20        type QString = crate::QString;
21
22        include!("cxx-qt-lib/qstringlist.h");
23        type QStringList = crate::QStringList;
24
25        /// Returns `true` if the object is empty.
26        #[rust_name = "is_empty"]
27        fn isEmpty(self: &QJsonObject) -> bool;
28
29        /// Returns `true` if the object contains `key`.
30        fn contains(self: &QJsonObject, key: &QString) -> bool;
31
32        /// Returns the value for `key`, or an undefined [`QJsonValue`] if the key is absent.
33        fn value(self: &QJsonObject, key: &QString) -> QJsonValue;
34
35        /// Removes `key` from the object.
36        fn remove(self: &mut QJsonObject, key: &QString);
37
38        /// Returns all keys in the object.
39        fn keys(self: &QJsonObject) -> QStringList;
40    }
41
42    #[namespace = "rust::cxxqtlib1"]
43    unsafe extern "C++" {
44        include!("cxx-qt-lib/common.h");
45
46        #[doc(hidden)]
47        #[rust_name = "qjsonobject_drop"]
48        fn drop(object: &mut QJsonObject);
49
50        #[doc(hidden)]
51        #[rust_name = "qjsonobject_init_default"]
52        fn construct() -> QJsonObject;
53
54        #[doc(hidden)]
55        #[rust_name = "qjsonobject_init_from_qjsonobject"]
56        fn construct(object: &QJsonObject) -> QJsonObject;
57
58        #[doc(hidden)]
59        #[rust_name = "qjsonobject_eq"]
60        fn operatorEq(a: &QJsonObject, b: &QJsonObject) -> bool;
61
62        #[doc(hidden)]
63        #[rust_name = "qjsonobject_to_debug_qstring"]
64        fn toDebugQString(object: &QJsonObject) -> QString;
65
66        #[doc(hidden)]
67        #[rust_name = "qjsonobject_len"]
68        fn qjsonobjectLen(object: &QJsonObject) -> isize;
69
70        #[doc(hidden)]
71        #[rust_name = "qjsonobject_insert"]
72        fn qjsonobjectInsert(object: &mut QJsonObject, key: &QString, value: &QJsonValue);
73    }
74}
75
76/// The `QJsonObject` class encapsulates a JSON object.
77///
78/// Qt Documentation: [QJsonObject](https://doc.qt.io/qt/qjsonobject.html#details)
79#[repr(C)]
80pub struct QJsonObject {
81    #[cfg(cxxqt_qt_version_major = "5")]
82    _d: MaybeUninit<usize>,
83    _o: MaybeUninit<usize>,
84}
85
86impl Default for QJsonObject {
87    fn default() -> Self {
88        ffi::qjsonobject_init_default()
89    }
90}
91
92impl Drop for QJsonObject {
93    fn drop(&mut self) {
94        ffi::qjsonobject_drop(self);
95    }
96}
97
98impl Clone for QJsonObject {
99    fn clone(&self) -> Self {
100        ffi::qjsonobject_init_from_qjsonobject(self)
101    }
102}
103
104impl PartialEq for QJsonObject {
105    fn eq(&self, other: &Self) -> bool {
106        ffi::qjsonobject_eq(self, other)
107    }
108}
109
110impl fmt::Debug for QJsonObject {
111    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
112        ffi::qjsonobject_to_debug_qstring(self).fmt(f)
113    }
114}
115
116// Safety:
117//
118// Static checks on the C++ side ensure that QJsonObject is trivial.
119unsafe impl cxx::ExternType for QJsonObject {
120    type Id = cxx::type_id!("QJsonObject");
121    type Kind = cxx::kind::Trivial;
122}
123
124impl QJsonObject {
125    /// Returns the number of key-value pairs in the object.
126    pub fn len(&self) -> isize {
127        ffi::qjsonobject_len(self)
128    }
129
130    /// Inserts or replaces the value for `key`.
131    pub fn insert(&mut self, key: &QString, value: &QJsonValue) {
132        ffi::qjsonobject_insert(self, key, value);
133    }
134}