@@ -50,6 +50,42 @@ mod ffi {
5050 /// Returns `true` if the value contains an object.
5151 #[ rust_name = "is_object" ]
5252 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 ;
5389 }
5490
5591 #[ namespace = "rust::cxxqtlib1" ]
@@ -99,30 +135,6 @@ mod ffi {
99135 #[ doc( hidden) ]
100136 #[ rust_name = "qjsonvalue_to_debug_qstring" ]
101137 fn toDebugQString ( value : & QJsonValue ) -> QString ;
102-
103- #[ doc( hidden) ]
104- #[ rust_name = "to_bool" ]
105- fn qjsonvalueToBool ( value : & QJsonValue ) -> bool ;
106-
107- #[ doc( hidden) ]
108- #[ rust_name = "to_double" ]
109- fn qjsonvalueToDouble ( value : & QJsonValue ) -> f64 ;
110-
111- #[ doc( hidden) ]
112- #[ rust_name = "to_int" ]
113- fn qjsonvalueToInt ( value : & QJsonValue ) -> i32 ;
114-
115- #[ doc( hidden) ]
116- #[ rust_name = "to_string" ]
117- fn qjsonvalueToQString ( value : & QJsonValue ) -> QString ;
118-
119- #[ doc( hidden) ]
120- #[ rust_name = "to_array" ]
121- fn qjsonvalueToArray ( value : & QJsonValue ) -> QJsonArray ;
122-
123- #[ doc( hidden) ]
124- #[ rust_name = "to_object" ]
125- fn qjsonvalueToObject ( value : & QJsonValue ) -> QJsonObject ;
126138 }
127139}
128140
@@ -216,34 +228,34 @@ unsafe impl cxx::ExternType for QJsonValue {
216228impl QJsonValue {
217229 /// Converts the value to a `bool`. Returns `false` if the value is not a boolean.
218230 pub fn to_bool ( & self ) -> bool {
219- ffi :: to_bool ( self )
231+ self . to_bool_or ( false )
220232 }
221233
222234 /// Converts the value to a `f64`. Returns `0.0` if the value is not a double.
223235 pub fn to_double ( & self ) -> f64 {
224- ffi :: to_double ( self )
236+ self . to_double_or ( 0.0 )
225237 }
226238
227239 /// Converts the value to a `i32`. Returns `0` if the value is not an integer.
228240 pub fn to_int ( & self ) -> i32 {
229- ffi :: to_int ( self )
241+ self . to_int_or ( 0 )
230242 }
231243
232244 /// Converts the value to a [`QString`]. Returns an empty string if the value is
233245 /// not a string.
234246 pub fn to_string ( & self ) -> QString {
235- ffi :: to_string ( self )
247+ self . to_string_or ( & QString :: default ( ) )
236248 }
237249
238250 /// Converts the value to a [`QJsonArray`]. Returns an empty array if the value
239251 /// is not an array.
240252 pub fn to_array ( & self ) -> QJsonArray {
241- ffi :: to_array ( self )
253+ self . to_array_or ( & QJsonArray :: default ( ) )
242254 }
243255
244256 /// Converts the value to a [`QJsonObject`]. Returns an empty object if the value
245257 /// is not an object.
246258 pub fn to_object ( & self ) -> QJsonObject {
247- ffi :: to_object ( self )
259+ self . to_object_or ( & QJsonObject :: default ( ) )
248260 }
249261}
0 commit comments