From: <sh...@zi...> - 2002-08-09 23:58:15
|
More details on automatic class-to-typed-YAML mappings. > > Solution: > > 1. Provide a super-class with a method __yaml__(), which adds > the "__classname__" key to any dictionaries when dumped. > The next version of PyYaml will change the interface for __yaml__(), so that it returns a tuple. The first item of the tuple will be the data to serialize, which typically will be a map (YAML equivalent of a dictionary), but which can also be sequence or a scalar. The second item of the tuple will be the YAML type indicator, which will either be something like "!!MyClassName" or just None, if you don't to indicate the type in YAML. So, you can have each of your classes hand-specify the YAML type indicator, or as you suggest, you can have a superclass that automatically creates the YAML type indicator from the Python class name. > 2. Provide a conversion function which, when applied to loaded > data structures, will convert any dictionaries containing > the key "__classname__" into instances of Python classes. > The type-resolver object that I mentioned in my previous post should simplify things considerably for you. Your object will be called back as the YAML is parsed, in depth-first order, and you will be given the private type, as well as the data itself, of course. Just construct the objects as you are called, pass them back to the YAML parser, and you should be good to go. Since things happen depth-first, you can resursively create the objects from smaller objects. |