Serializing With YAML
00:00 Now it’s time to introduce a new format. So far you have seen XML and JSON. Now you will see YAML. And this is where the flexibility that you have built into the code will really show.
00:15
Think about it. The client objects Song
and Book
. They don’t need to know how their objects are being serialized. All they need to know is that whatever serializer product the creator will provide, that serialized product will use the start
objects and add_property
methods.
00:34
So all you need to do is build a YAML product that does exactly that. And please remember that for this code to work, you will need to install PyYAML
using the instruction on the screen, so python -m pip install PyYAML
.
00:56
To get started, import yaml
, not with capitals, just like json
is not with capitals. The second step is to then create a product class to serialize to YAML.
01:12
So in the Products
section, create a new class. So the class will be called _
YAMLSerializer
.
01:25
And now you have a choice. So far, the _XMLSerializer
and the _JSONSerializer
inherit from the Serialization
ProductBluePrint
, which is the abstract base class for the products.
01:39
You can do that here, but I wanted to show you something different. YAML is actually very close to JSON. So you could have the YAMLSerializer
just inherit from the JSONSerializer
. Type _JSONSerializer
.
01:59
Now that makes things a lot easier because as opposed to the _XMLSerializer
class, which starts on line 109, where you have an .__init__()
method and start_object
method and add_property
method.
02:12
And that is the same for the _JSONSerializer
, which starts on line 95. Given that you are now already inheriting from the JSON serializer, the only thing you have to do is redefine the .__str__()
dunder method because of course the .__str__()
dunder method is specific to the serializer for JSON on line 106, it’s json.dumps()
.
02:38
For the _XMLSerializer
on line 121 it is et.tostring()
. So for YAML it is going to be something specific as well. Define the .__str__()
dunder method, which takes self
as an input parameter.
02:56
And then what you return is yaml.dump()
.
03:02
And what you pass into this method is the _current_object
attribute of the class which you have inherited from the JSON serializer. So that makes the code a lot simpler and it shows you the power of inheritance.
03:20
Now you’ll need to make one more change. The factory_method
of course, needs to be able to call or instantiate this new product class. So move up to the factory_method
, which is in your Creator
.
03:34
Here we go. It starts on line 59 and you will need another elif
.
03:41
And so data_format
== "YAML"
.
03:50
If that is the case, then return newly created product, which is the _YAMLSerializer
04:00 and in fact return the instance of it. Make sure you now save the code and then restart the REPL and test the code.
04:12
So from serializer import Song, serialize
and Book
. I will create my book, which is It by Stephen King. And now I’d like to serialize my book JSON.
04:25 That still works, but now let’s try YAML.
04:30 And now you see that that works. So you have your same extra information there. The serializer is indeed YAML. And at the bottom here you have your book, which is now represented in YAML format.
04:47
Now if you have a look back at your code, the factory_method
here uses an if-elif-else
construction, which is fine so far because there are only three data formats.
04:57 But if this keeps growing, if there are more data formats, this could become a little bit cumbersome, a little bit busy. So, in the next lesson, you will look at a new way of dealing with different data formats.
Become a Member to join the conversation.