filmov
tv
How To Serialize Python Objects In To JSON Strings?

Показать описание
JSON, Javascript Object Notation is a widely used file format for data exchange all across the web thanks to the prevalence of Javascript frameworks. Converting a Python object into a JSON string is called Serialization. In this video, we will see three different ways of serializing Python objects to JSON.
→Timecodes←
---------------------
00:00 Context
00:30 Setup
00:54 The wrong way
01:47 Serializing the Manual Way
02:55 Serializing using an Encoder Function
05:08 Serializing using an Encoder Class
07:08 Closing Remarks
→Links←
-------------
→Social Media←
-----------------------
→Video Summary←
---------------------------
We can serialize Python Objects into JSON strings in multiple ways. First approach is to do this manually. We manually pull our the values from the Python object and put it into a JSON string using f-strings. This is the simplest way but it does not offer easy access to change formatting of the output string.
The second way is to use an encoder function. An encoder function is one which takes our Python object and returns a JSON encodable object. This object then gets serialized by the "dumps" function. As we are using dumps here, we can customize the indentation and formatting. We pass in the encoder function to the dumps function with the keyword default.
The third way is to use an Encoder class. This works similar to an encoder function. We create a class that extends json.JSONEncoder class. In the encoder class we create the default method that has the same content as our encoder function as before.
We pass in the class to dumps using the "cls" keyword. We can use the super class (JSONEncoder) to take care of the TypeError for incompatible types. We can also create an object of the encoder class and pass in the object to that object.
This way, we can serialize a Python object into a JSON string.
→Attributions←
----------------------
(In order)
ERF -"Royalty Free Music from Bensound"
Subscribe Video - Free Stock Video Footage by Videezy
→Tools/Gear←
---------------------
Operating System - Windows 10
Python IDE - Pycharm
Video Editor - Shotcut
Audio Editor - Audacity
Thumbnail Editor - Canva
→Timecodes←
---------------------
00:00 Context
00:30 Setup
00:54 The wrong way
01:47 Serializing the Manual Way
02:55 Serializing using an Encoder Function
05:08 Serializing using an Encoder Class
07:08 Closing Remarks
→Links←
-------------
→Social Media←
-----------------------
→Video Summary←
---------------------------
We can serialize Python Objects into JSON strings in multiple ways. First approach is to do this manually. We manually pull our the values from the Python object and put it into a JSON string using f-strings. This is the simplest way but it does not offer easy access to change formatting of the output string.
The second way is to use an encoder function. An encoder function is one which takes our Python object and returns a JSON encodable object. This object then gets serialized by the "dumps" function. As we are using dumps here, we can customize the indentation and formatting. We pass in the encoder function to the dumps function with the keyword default.
The third way is to use an Encoder class. This works similar to an encoder function. We create a class that extends json.JSONEncoder class. In the encoder class we create the default method that has the same content as our encoder function as before.
We pass in the class to dumps using the "cls" keyword. We can use the super class (JSONEncoder) to take care of the TypeError for incompatible types. We can also create an object of the encoder class and pass in the object to that object.
This way, we can serialize a Python object into a JSON string.
→Attributions←
----------------------
(In order)
ERF -"Royalty Free Music from Bensound"
Subscribe Video - Free Stock Video Footage by Videezy
→Tools/Gear←
---------------------
Operating System - Windows 10
Python IDE - Pycharm
Video Editor - Shotcut
Audio Editor - Audacity
Thumbnail Editor - Canva
Комментарии