Last active
July 3, 2021 02:26
-
-
Save suragch/2c77d48bca384288afb892d44af3ef2d to your computer and use it in GitHub Desktop.
Safely unpacking JSON objects in Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include: package:pedantic/analysis_options.yaml | |
analyzer: | |
strong-mode: | |
implicit-casts: false | |
implicit-dynamic: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
void main() { | |
implicitCastsExample(); | |
implicitDynamicExample(); | |
jsonMapExample(); | |
jsonListExample(); | |
} | |
/// Implicit casts example | |
void implicitCastsExample() { | |
// num x = 3; | |
// int y = x; | |
num x = 3; | |
int y = x as int; | |
// num x = 3.1; | |
// int y = x as int; | |
} | |
/// Implicit dynamic example | |
void implicitDynamicExample() { | |
// var x; | |
dynamic x; | |
x = 3; | |
// x.length; | |
} | |
/// Map example | |
String jsonMapString = ''' | |
{ | |
"name":"mary", | |
"age":35 | |
} | |
'''; | |
void jsonMapExample() { | |
try { | |
dynamic myMap = json.decode(jsonMapString); | |
if (myMap is! Map<String, dynamic>) throw FormatException(); | |
final person = Person.fromJson(myMap); | |
print('${person.name} ${person.age}'); | |
} catch (error) { | |
print('JSON is in the wrong format'); | |
} | |
} | |
/// List example | |
String jsonListString = ''' | |
[ | |
{ | |
"name":"bob", | |
"age":22 | |
}, | |
{ | |
"name":"mary", | |
"age":35 | |
} | |
] | |
'''; | |
void jsonListExample() { | |
final myList = <Person>[]; | |
try { | |
dynamic jsonList = json.decode(jsonListString); | |
if (jsonList is! List) throw FormatException(); | |
for (dynamic item in jsonList) { | |
if (item is! Map<String, dynamic>) continue; | |
final person = Person.fromJson(item); | |
myList.add(person); | |
} | |
} on FormatException { | |
print('JSON is in the wrong format'); | |
} | |
myList.forEach((person) => print('${person.name} ${person.age}')); | |
} | |
/// Data class | |
class Person { | |
final String name; | |
final int age; | |
Person(this.name, this.age); | |
factory Person.fromJson(Map<String, dynamic> json) { | |
dynamic name = json['name']; | |
dynamic age = json['age']; | |
if (name is! String) name = ''; | |
if (age is! int) age = 0; | |
return Person(name, age); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: dart_playground | |
description: A simple command-line application. | |
version: 1.0.0 | |
environment: | |
sdk: '>=2.12.0 <3.0.0' | |
dev_dependencies: | |
pedantic: ^1.10.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment