How To Serialize Python Objects In To JSON Strings?

Publicado em: 03 Outubro 2020
no canal de: Swaroop P
16,863
323

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←
-------------
JSON in Python - Introduction Video -    • JSON In Python | Part 1  
What is JSON Serialization & Deserialization -    • What is JSON Serialization and Deseri...  
JSON Parsing with Python -   / json-parsing-with-python  
JSON documentation - https://docs.python.org/3/library/jso...

→Social Media←
-----------------------
Twitter -   / durgaswaroop  
Medium -   / durgaswaroop  
Youtube -    / @durgaswaroopperla  
Linkedin -   / durgaswaroop  

→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"
Melbourne Beach Video - Video by Dan Assis from Pexels - https://www.pexels.com/video/drone-fo...
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


Nesta página do site você pode assistir ao vídeo on-line How To Serialize Python Objects In To JSON Strings? duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Swaroop P 03 Outubro 2020, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 16,863 vezes e gostou 323 espectadores. Boa visualização!