@@ -32,7 +32,7 @@ than JSON or TOML --- without the complexity of a database.
3232- Human-readable aligned format
3333- Atomic file saves (corruption protection)
3434- Custom type registry
35- - Supports ` datetime ` , ` date ` , ` time ` , and ` Path ` natively
35+ - Supports ` datetime ` , ` date ` , ` time ` , ` Path ` and ` Optional ` natively
3636- Safe variable handling with deep copy
3737
3838** NEW in v2:**
@@ -99,7 +99,7 @@ user = data.section("user123") # for simplicity user always refers to this secti
9999
100100user.set(" name" , str , " Steve" )
101101user.set(" score" , int , 42 )
102- user.set(" tags " , list[str ], [" admin" , " tester" ])
102+ user.set(" roles " , list[str ], [" admin" , " tester" ])
103103user.set(" prefs" , dict[int , str ], {1 : " dark" , 2 : " light" })
104104
105105data.save(" botdata.rdm" )
@@ -181,8 +181,8 @@ user.set("items", list[int], [1]) # [1]
181181user.extend(" items" , 2 ) # [1, 2]
182182user.extend(" items" , [3 , 4 ]) # [1, 2, 3, 4]
183183
184- user.set(" tags " , set[str ], {" a" }) # {"a"}
185- user.extend(" tags " , " b" ) # {"a", "b"}
184+ user.set(" roles " , set[str ], {" a" }) # {"a"}
185+ user.extend(" roles " , " b" ) # {"a", "b"}
186186
187187user.set(" settings" , dict[str ,int ], {" a" :1 }) # {"a": 1}
188188user.extend(" settings" , {" b" :2 }) # {"a": 1, "b": 2}
@@ -218,6 +218,7 @@ user.delete("score") # key, value and type of "score" will be deleted entirely f
218218- ` float `
219219- ` bool `
220220- ` None `
221+ - ` Optional[T] `
221222- ` list[T] `
222223- ` set[T] `
223224- ` tuple[T] `
@@ -243,19 +244,31 @@ class Color:
243244 def __init__ (self , hex_code : str ):
244245 self .hex = hex_code
245246
247+ # using lambda, where 'v' is the stored value
246248rheelDM.TypeRegistry.register(
247249 " Color" ,
248250 Color,
249- lambda v : f ' " { v.hex} " ' ,
250- lambda v : Color(v.strip(' "' ))
251+ lambda v : f ' # { v.hex} ' ,
252+ lambda v : Color(v[1 :])
253+ )
254+
255+ # using callables (functions)
256+ def serialize_color (v ): ...
257+ def deserialize_color (v ): ...
258+
259+ rheelDM.TypeRegistry.register(
260+ " Color" ,
261+ Color,
262+ serialize_color, # do not call them here,
263+ deserialize_color # just pass the callable
251264)
252265```
253266
254267Now you can use it like any native type:
255268
256269``` python
257270# same with TempObj and set()
258- data.section(" settings" ).set(" theme" , Color, Color(" # ff8800" ))
271+ data.section(" settings" ).set(" theme" , Color, Color(" ff8800" ))
259272```
260273
261274------------------------------------------------------------------------
@@ -277,12 +290,14 @@ JSON cannot store:
277290- Union types
278291- Nested generics
279292- ` int ` as ` dict ` -keys
280- - ` None ` (stores it as ` null ` )
293+ - ` NoneType ` (stores it as ` null ` )
294+ - ` Optional `
281295
282296TOML cannot store:
283297- ` Path `
284298- ` set `
285- - ` None `
299+ - ` NoneType `
300+ - ` Optional `
286301- ` int ` as ` dict ` -keys
287302
288303RDM can store all of these, even the most complex and custom types.
0 commit comments