@@ -188,7 +188,11 @@ struct LuaJsonMapIter {
188
188
iter : serde_json:: map:: Iter < ' this > ,
189
189
}
190
190
191
- fn decode ( lua : & Lua , ( data, opts) : ( StringOrBytes , Option < Table > ) ) -> Result < StdResult < Value , String > > {
191
+ /// Decodes a JSON string or bytes into a Lua value.
192
+ ///
193
+ /// The optional `opts` table can contain:
194
+ /// - `set_array_metatable` (boolean): If true, sets a metatable for arrays. Default is false.
195
+ pub fn decode ( lua : & Lua , ( data, opts) : ( StringOrBytes , Option < Table > ) ) -> Result < StdResult < Value , String > > {
192
196
let opts = opts. as_ref ( ) ;
193
197
let mut options = SerializeOptions :: new ( ) ;
194
198
if let Some ( enabled) = opts. and_then ( |t| t. get :: < bool > ( "set_array_metatable" ) . ok ( ) ) {
@@ -199,20 +203,28 @@ fn decode(lua: &Lua, (data, opts): (StringOrBytes, Option<Table>)) -> Result<Std
199
203
Ok ( Ok ( lua. to_value_with ( & json, options) ?) )
200
204
}
201
205
202
- fn decode_native ( lua : & Lua , data : StringOrBytes ) -> Result < StdResult < Value , String > > {
206
+ /// Decodes a JSON string or bytes as a native Rust object.
207
+ ///
208
+ /// The returned value can be a primitive type or userdata.
209
+ pub fn decode_native ( lua : & Lua , data : StringOrBytes ) -> Result < StdResult < Value , String > > {
203
210
let json: serde_json:: Value = lua_try ! ( serde_json:: from_slice( & data. as_bytes_deref( ) ) ) ;
204
211
Ok ( Ok ( lua_try ! ( JsonObject :: from( json) . into_lua( lua) ) ) )
205
212
}
206
213
207
- fn encode ( value : Value , options : Option < Table > ) -> StdResult < String , String > {
214
+ /// Encodes a Lua value into a JSON string.
215
+ ///
216
+ /// The optional `opts` table can contain:
217
+ /// - `pretty` (boolean): If true, pretty formats the JSON string. Default is false.
218
+ /// - `relaxed` (boolean): If true, skip recursive tables and unsupported types. Default is false.
219
+ pub fn encode ( value : Value , opts : Option < Table > ) -> StdResult < String , String > {
208
220
let mut value = value. to_serializable ( ) ;
209
- let options = options . as_ref ( ) ;
221
+ let opts = opts . as_ref ( ) ;
210
222
211
- if options . and_then ( |t| t. get :: < bool > ( "relaxed" ) . ok ( ) ) == Some ( true ) {
223
+ if opts . and_then ( |t| t. get :: < bool > ( "relaxed" ) . ok ( ) ) == Some ( true ) {
212
224
value = value. deny_recursive_tables ( false ) . deny_unsupported_types ( false ) ;
213
225
}
214
226
215
- if options . and_then ( |t| t. get :: < bool > ( "pretty" ) . ok ( ) ) == Some ( true ) {
227
+ if opts . and_then ( |t| t. get :: < bool > ( "pretty" ) . ok ( ) ) == Some ( true ) {
216
228
value = value. sort_keys ( true ) ;
217
229
return serde_json:: to_string_pretty ( & value) . map_err ( |e| e. to_string ( ) ) ;
218
230
}
0 commit comments