@@ -192,12 +192,18 @@ struct LuaJsonMapIter {
192
192
///
193
193
/// The optional `opts` table can contain:
194
194
/// - `set_array_metatable` (boolean): If true, sets a metatable for arrays. Default is false.
195
+ /// - `null_as_nil` (boolean): If true, `null`s will be represented as Lua `nil`. Default is false.
195
196
pub fn decode ( lua : & Lua , ( data, opts) : ( StringOrBytes , Option < Table > ) ) -> Result < StdResult < Value , String > > {
196
197
let opts = opts. as_ref ( ) ;
197
198
let mut options = SerializeOptions :: new ( ) ;
198
- if let Some ( enabled) = opts. and_then ( |t| t. get :: < bool > ( "set_array_metatable" ) . ok ( ) ) {
199
+ if let Some ( enabled) = opts. and_then ( |t| t. raw_get :: < bool > ( "set_array_metatable" ) . ok ( ) ) {
199
200
options = options. set_array_metatable ( enabled) ;
200
201
}
202
+ if let Some ( enabled) = opts. and_then ( |t| t. raw_get :: < bool > ( "null_as_nil" ) . ok ( ) ) {
203
+ options = options
204
+ . serialize_unit_to_null ( !enabled)
205
+ . serialize_none_to_null ( !enabled) ;
206
+ }
201
207
202
208
let json: serde_json:: Value = lua_try ! ( serde_json:: from_slice( & data. as_bytes_deref( ) ) ) ;
203
209
Ok ( Ok ( lua. to_value_with ( & json, options) ?) )
@@ -220,11 +226,11 @@ pub fn encode(value: Value, opts: Option<Table>) -> StdResult<String, String> {
220
226
let mut value = value. to_serializable ( ) ;
221
227
let opts = opts. as_ref ( ) ;
222
228
223
- if opts. and_then ( |t| t. get :: < bool > ( "relaxed" ) . ok ( ) ) == Some ( true ) {
229
+ if opts. and_then ( |t| t. raw_get :: < bool > ( "relaxed" ) . ok ( ) ) == Some ( true ) {
224
230
value = value. deny_recursive_tables ( false ) . deny_unsupported_types ( false ) ;
225
231
}
226
232
227
- if opts. and_then ( |t| t. get :: < bool > ( "pretty" ) . ok ( ) ) == Some ( true ) {
233
+ if opts. and_then ( |t| t. raw_get :: < bool > ( "pretty" ) . ok ( ) ) == Some ( true ) {
228
234
value = value. sort_keys ( true ) ;
229
235
return serde_json:: to_string_pretty ( & value) . map_err ( |e| e. to_string ( ) ) ;
230
236
}
0 commit comments