@@ -192,12 +192,18 @@ struct LuaJsonMapIter {
192192///
193193/// The optional `opts` table can contain:
194194/// - `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.
195196pub fn decode ( lua : & Lua , ( data, opts) : ( StringOrBytes , Option < Table > ) ) -> Result < StdResult < Value , String > > {
196197 let opts = opts. as_ref ( ) ;
197198 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 ( ) ) {
199200 options = options. set_array_metatable ( enabled) ;
200201 }
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+ }
201207
202208 let json: serde_json:: Value = lua_try ! ( serde_json:: from_slice( & data. as_bytes_deref( ) ) ) ;
203209 Ok ( Ok ( lua. to_value_with ( & json, options) ?) )
@@ -220,11 +226,11 @@ pub fn encode(value: Value, opts: Option<Table>) -> StdResult<String, String> {
220226 let mut value = value. to_serializable ( ) ;
221227 let opts = opts. as_ref ( ) ;
222228
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 ) {
224230 value = value. deny_recursive_tables ( false ) . deny_unsupported_types ( false ) ;
225231 }
226232
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 ) {
228234 value = value. sort_keys ( true ) ;
229235 return serde_json:: to_string_pretty ( & value) . map_err ( |e| e. to_string ( ) ) ;
230236 }
0 commit comments