serialization_and_deserialization
Serialization and deserialization are processes used to convert data from one format to another, commonly used when transferring data between different systems, storing data, or sending data over a network. In the context of Flutter or Dart, these processes are often associated with converting objects to and from JSON format.
Serialization:
Serialization is the process of converting an object into a format that can be easily stored, transmitted, or reconstructed at a later time. In Dart, serialization typically involves converting an object into a JSON (JavaScript Object Notation) string.
Example serialization of an object to JSON in Dart:
import 'dart:convert';
class User {
final String name;
final int age;
User(this.name, this.age);
// Convert User object to JSON
Map<String, dynamic> toJson() {
return {
'name': name,
'age': age,
};
}
}
void main() {
User user = User('John Doe', 30);
// Serialize the User object to JSON string
String userJson = json.encode(user.toJson());
print(userJson); // Output: {"name":"John Doe","age":30}
}
In this example, the toJson()
method of the User
class converts the User
object into a JSON object (Map) and json.encode()
is used to convert the JSON object to a string.
Deserialization:
Deserialization is the opposite process of serialization. It involves converting data from a serialized format back into an object or data structure that can be used within the program.
Example deserialization of JSON to an object in Dart:
import 'dart:convert';
class User {
final String name;
final int age;
User(this.name, this.age);
// Factory method to create User object from JSON
factory User.fromJson(Map<String, dynamic> json) {
return User(json['name'], json['age']);
}
}
void main() {
String userJsonString = '{"name":"Jane Smith","age":25}';
// Deserialize JSON string to a User object
Map<String, dynamic> userMap = json.decode(userJsonString);
User user = User.fromJson(userMap);
print(user.name); // Output: Jane Smith
print(user.age); // Output: 25
}
In this example, the User.fromJson()
factory method creates a User
object from a JSON Map. json.decode()
is used to convert the JSON string to a Map, which is then passed to the User.fromJson()
method to create a User
object.
Serialization and deserialization are essential for communication between applications, storing data, and managing data interchangeably between different systems. In Dart and Flutter, the dart:convert
library provides methods for encoding (serialization) and decoding (deserialization) JSON data. These processes are commonly used when working with APIs, handling network requests, or storing/retrieving data.
Last updated