How to deserialize an object with PyYAML using safe_load?

前端 未结 3 762
攒了一身酷
攒了一身酷 2021-02-15 15:20

Having a snippet like this:

import yaml
class User(object):
    def __init__(self, name, surname):
       self.name= name
       self.surname= surname

user = U         


        
3条回答
  •  不要未来只要你来
    2021-02-15 15:54

    Another way exists. From the PyYaml docs:

    A python object can be marked as safe and thus be recognized by yaml.safe_load. To do this, derive it from yaml.YAMLObject [...] and explicitly set its class property yaml_loader to yaml.SafeLoader.

    You also have to set the yaml_tag property to make it work.

    YAMLObject does some metaclass magic to make the object loadable. Note that if you do this, the objects will only be loadable by the safe loader, not with regular yaml.load().

    Working example:

    import yaml
    
    class User(yaml.YAMLObject):
        yaml_loader = yaml.SafeLoader
        yaml_tag = u'!User'
    
        def __init__(self, name, surname):
           self.name= name
           self.surname= surname
    
    user = User('spam', 'eggs')
    serialized_user = yaml.dump(user)
    
    #Network
    
    deserialized_user = yaml.safe_load(serialized_user)
    print "name: %s, sname: %s" % (deserialized_user.name, deserialized_user.surname)
    

    The advantage of this one is that it's prety easy to do; the disadvantages are that it only works with safe_load and clutters your class with serialization-related attributes and metaclass.

提交回复
热议问题