How to resolve deep json object in Dart
Issue #198
If we are not on the edge with GRPC and Protocol Buffer, then most likely we are going to deal with Restful and JSON. In one of my Flutter apps I needed to consume JSON
JSON and serialization
The guide at https://flutter.dev/docs/development/data-and-backend/json is definitely the way to go.
Currently there are 2 ways. One is to manually use dart:convert
package
1 | Map<String, dynamic> user = jsonDecode(jsonString); |
The other way is to use json_serializable to generate parsing code
1 | import 'package:json_annotation/json_annotation.dart'; |
json_resolve
The problem with manual approach is that it involves lot of boilerplate code, especially when accessing property inside deeply nested json. The problem with code generation approach is that it does not always fit our need and may lack of customization.
Therefore I created json_resolve which allows us to access json using keypath, with type checking and safety in mind. The code is small, simple to reason and tested.
1 | final String byProperty = resolve(json: json, path: "movie", defaultValue: "error"); |