Last active
March 8, 2024 07:15
-
-
Save pskink/2d0cdd8cd7934bd0a5d620861402f49d to your computer and use it in GitHub Desktop.
This file contains hidden or 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'; | |
import 'dart:io'; | |
import 'package:flutter/foundation.dart'; | |
import 'package:http/http.dart' as http; | |
parseJSON() async { | |
final json = JsonCodec.withReviver((key, value) { | |
// print(' -- reviver -- key: $key, value: $value'); | |
return switch (value) { | |
/* Address */ { | |
'street': String street, | |
'suite': String suite, | |
'city': String city, | |
'zipcode': String zipcode, | |
'geo': Geo geo, | |
} => Address(street, suite, city, zipcode, geo), | |
/* Geo */ { | |
'lat': String lat, | |
'lng': String lng, | |
} => Geo(lat, lng), | |
/* Company */ { | |
'name': String name, | |
'catchPhrase': String catchPhrase, | |
'bs': String bs, | |
} => Company(name, catchPhrase, bs), | |
/* Login */ { | |
'uuid': String uuid, | |
'username': String username, | |
'password': String password, | |
'md5': String md5, | |
'sha1': String sha1, | |
'registered': String registered, | |
} => Login(uuid, username, password, md5, sha1, registered), | |
/* User */ { | |
'id': int id, | |
'firstname': String firstname, | |
'lastname': String lastname, | |
'email': String email, | |
'birthDate': String birthDate, | |
'login': Login login, | |
'address': Address address, | |
'phone': String phone, | |
'website': String website, | |
'company': Company company, | |
} => User(id, firstname, lastname, email, birthDate, login, address, phone, website, company), | |
// final List<User> array (optional?) | |
List allUsers => allUsers.cast<User>(), | |
// all other go unchanged | |
_ => value | |
}; | |
}); | |
final request = http.Request('get', Uri.parse('https://jsonplaceholder.org/users')); | |
// final request = http.Request('get', Uri.parse('http://0.0.0.0/flutter/users.json')); | |
// final request = http.Request('get', Uri.parse('http://0.0.0.0/flutter/users.json.gz')); | |
final response = await request.send(); | |
assert (response.statusCode == 200); | |
debugPrint('start ------------------------------------------'); | |
final t1 = DateTime.now(); | |
Stream<List<int>> stream = response.stream; | |
if (response.headers['content-type'] == 'application/gzip') { | |
debugPrint('📢 activated gzip transformed stream 📢'); | |
stream = stream.transform(gzip.decoder); | |
} | |
final users = await stream.transform(json.fuse(utf8).decoder).single as List<User>; | |
final t2 = DateTime.now(); | |
debugPrint('end --------------------------------------------'); | |
debugPrint('✅ parsing took ${t2.millisecondsSinceEpoch - t1.millisecondsSinceEpoch} ms'); | |
debugPrint('users.runtimeType: ${users.runtimeType}'); | |
debugPrint('users.length: ${users.length}'); | |
// users.forEach(print); | |
debugPrint(users.first.address.city); | |
debugPrint(users.last.company.name); | |
debugPrint('------------------------------------------------'); | |
} | |
// | |
// automagically created with https://app.quicktype.io | |
// | |
class User { | |
User(this.id, this.firstname, this.lastname, this.email, this.birthDate, this.login, this.address, this.phone, this.website, this.company); | |
final int id; | |
final String firstname; | |
final String lastname; | |
final String email; | |
final String birthDate; | |
final Login login; | |
final Address address; | |
final String phone; | |
final String website; | |
final Company company; | |
@override | |
String toString() { | |
return 'User(id: $id, firstname: $firstname, lastname: $lastname, email: $email, birthDate: $birthDate, login: $login, address: $address, phone: $phone, website: $website, company: $company)'; | |
} | |
} | |
class Address { | |
Address(this.street, this.suite, this.city, this.zipcode, this.geo); | |
final String street; | |
final String suite; | |
final String city; | |
final String zipcode; | |
final Geo geo; | |
@override | |
String toString() { | |
return 'Address(street: $street, suite: $suite, city: $city, zipcode: $zipcode, geo: $geo)'; | |
} | |
} | |
class Geo { | |
Geo(this.lat, this.lng); | |
final String lat; | |
final String lng; | |
@override | |
String toString() => 'Geo(lat: $lat, lng: $lng)'; | |
} | |
class Company { | |
Company(this.name, this.catchPhrase, this.bs); | |
final String name; | |
final String catchPhrase; | |
final String bs; | |
@override | |
String toString() => 'Company(name: $name, catchPhrase: $catchPhrase, bs: $bs)'; | |
} | |
class Login { | |
Login(this.uuid, this.username, this.password, this.md5, this.sha1, this.registered); | |
final String uuid; | |
final String username; | |
final String password; | |
final String md5; | |
final String sha1; | |
final String registered; | |
@override | |
String toString() { | |
return 'Login(uuid: $uuid, username: $username, password: $password, md5: $md5, sha1: $sha1, registered: $registered)'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment