Update tech_docs/python/json_python.md
This commit is contained in:
@@ -1,3 +1,100 @@
|
|||||||
|
Sure! Let's focus on the Python implementation of serialization and deserialization, illustrating the process with detailed examples.
|
||||||
|
|
||||||
|
### Serialization
|
||||||
|
|
||||||
|
Serialization in Python can be done using various libraries, such as `json`, `pickle`, or others. Here, we'll use the `json` library for simplicity.
|
||||||
|
|
||||||
|
1. **Convert Data Class to JSON Object:**
|
||||||
|
|
||||||
|
First, let's define a simple data class and serialize it to a JSON string.
|
||||||
|
|
||||||
|
**Example in Python:**
|
||||||
|
```python
|
||||||
|
import json
|
||||||
|
|
||||||
|
class Person:
|
||||||
|
def __init__(self, name, age):
|
||||||
|
self.name = name
|
||||||
|
self.age = age
|
||||||
|
|
||||||
|
# Create an instance of the class
|
||||||
|
person = Person("Alice", 30)
|
||||||
|
|
||||||
|
# Serialize the object to JSON
|
||||||
|
person_json = json.dumps(person.__dict__)
|
||||||
|
|
||||||
|
print(person_json) # Output: {"name": "Alice", "age": 30}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Deserialization
|
||||||
|
|
||||||
|
Deserialization is the reverse process, converting the JSON string back into an object.
|
||||||
|
|
||||||
|
1. **Convert JSON Object Back to Data Class:**
|
||||||
|
|
||||||
|
**Example in Python:**
|
||||||
|
```python
|
||||||
|
# Deserialize the JSON back to a dictionary
|
||||||
|
person_dict = json.loads(person_json)
|
||||||
|
|
||||||
|
# Create a new instance of Person with the deserialized data
|
||||||
|
deserialized_person = Person(**person_dict)
|
||||||
|
|
||||||
|
print(deserialized_person.name) # Output: Alice
|
||||||
|
print(deserialized_person.age) # Output: 30
|
||||||
|
```
|
||||||
|
|
||||||
|
### Complete Example
|
||||||
|
|
||||||
|
Combining serialization and deserialization into a complete example:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import json
|
||||||
|
|
||||||
|
class Person:
|
||||||
|
def __init__(self, name, age):
|
||||||
|
self.name = name
|
||||||
|
self.age = age
|
||||||
|
|
||||||
|
# Serialization
|
||||||
|
def serialize(person):
|
||||||
|
"""Serialize a Person object to a JSON string."""
|
||||||
|
return json.dumps(person.__dict__)
|
||||||
|
|
||||||
|
# Deserialization
|
||||||
|
def deserialize(person_json):
|
||||||
|
"""Deserialize a JSON string to a Person object."""
|
||||||
|
person_dict = json.loads(person_json)
|
||||||
|
return Person(**person_dict)
|
||||||
|
|
||||||
|
# Example usage
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# Create an instance of the class
|
||||||
|
person = Person("Alice", 30)
|
||||||
|
|
||||||
|
# Serialize the object to JSON
|
||||||
|
person_json = serialize(person)
|
||||||
|
print(f"Serialized JSON: {person_json}")
|
||||||
|
|
||||||
|
# Deserialize the JSON back to a Person object
|
||||||
|
deserialized_person = deserialize(person_json)
|
||||||
|
print(f"Deserialized Person: Name={deserialized_person.name}, Age={deserialized_person.age}")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Explanation
|
||||||
|
|
||||||
|
1. **Serialization:**
|
||||||
|
- The `serialize` function takes a `Person` object and converts it into a JSON string using `json.dumps()`.
|
||||||
|
- The `__dict__` attribute of the object is used to get a dictionary representation of the object's attributes.
|
||||||
|
|
||||||
|
2. **Deserialization:**
|
||||||
|
- The `deserialize` function takes a JSON string and converts it back into a `Person` object using `json.loads()`.
|
||||||
|
- The resulting dictionary is unpacked into the `Person` constructor using the `**` syntax.
|
||||||
|
|
||||||
|
This approach provides a clear and concise method for serializing and deserializing objects in Python, ensuring that the object's state can be easily saved and restored.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
# Comprehensive Guide: JSON 'Querying' in Python
|
# Comprehensive Guide: JSON 'Querying' in Python
|
||||||
|
|
||||||
When working with JSON in Python, you're essentially navigating and manipulating a nested structure of dictionaries and lists. While not a formal query language like SQL, Python provides powerful tools to extract, filter, and transform JSON data. Here's an in-depth look at common operations:
|
When working with JSON in Python, you're essentially navigating and manipulating a nested structure of dictionaries and lists. While not a formal query language like SQL, Python provides powerful tools to extract, filter, and transform JSON data. Here's an in-depth look at common operations:
|
||||||
|
|||||||
Reference in New Issue
Block a user