All 7 JSON Serialization & Deserialization questions
More in JSON Serialization & Deserialization
Advertisement
Back to results for “Generics”
Flutter › JSON Serialization & Deserialization

What is JSON serialization and deserialization, and how do Dart's own built in jsonEncode and jsonDecode functions let you convert genuinely freely between a Dart object and its own corresponding JSON text representation?

Beginner

Answer

Serialization means converting a genuine Dart object into a JSON formatted text string, typically specifically to actually send that data over a network or properly save it to a local file, while deserialization means the exact reverse genuine process, converting a JSON formatted text string back into a genuinely usable Dart object, and Dart's own built in dart:convert library provides jsonEncode specifically for serialization and jsonDecode specifically for deserialization, both working directly with Dart's own basic native Map and List types representing that particular underlying JSON structure.
final jsonString = jsonEncode({'name': 'Alex', 'age': 30});
final decoded = jsonDecode(jsonString);
print(decoded['name']);
Real-world example A weather app converts its own fetched weather data directly into a JSON string using jsonEncode specifically to properly cache it locally, then later properly converts that exact same cached JSON string back into a genuinely usable Dart Map using jsonDecode when the app is later actually genuinely reopened.

Common follow-ups: What is the specific practical difference between jsonEncode and jsonDecode in terms of exactly what direction each one actually converts data?;What genuinely happens if you try to actually decode a JSON string that is genuinely malformed or invalid?