Generic Json Parser
Generic utility library for parsing json including background parsing using compute() function.
Features
- Parsing
jsondata list. - Parsing
jsondata object. - Parsing
jsondata list in background. - Parsing
jsondata object in background.
Usage
- Add package dependency to
pubspec.yaml.
dependencies:
generic_json_parser: ^0.0.1
- Import package
import 'package:generic_json_parser/generic_json_parser.dart';
- Create your model
class Photo {
final int albumId;
final int id;
final String title;
final String url;
final String thumbnailUrl;
const Photo({
required this.albumId,
required this.id,
required this.title,
required this.url,
required this.thumbnailUrl,
});
factory Photo.fromJson(Map<String, dynamic> json) {
return Photo(
albumId: json['albumId'] as int,
id: json['id'] as int,
title: json['title'] as String,
url: json['url'] as String,
thumbnailUrl: json['thumbnailUrl'] as String,
);
}
}
- Use one of utility function you like as following.
Using http package.
Future<List<Photo>> fetchPhotos(http.Client client) async {
final response = await client
.get(Uri.parse('https://jsonplaceholder.typicode.com/photos'));
return GenericJsonParser.parseJsonListInBackground(
responseBody,
Photo.fromJson,
);
}
Using dio package.
Future<List<Photo>> fetchPhotos(Dio client) async {
final response = await client
.getUri(Uri.parse('https://jsonplaceholder.typicode.com/photos'));
return GenericJsonParser.parseJsonListInBackground(
responseBody,
Photo.fromJson,
useDioPackage: true,
);
}