https://github.com/csells/dart_json_fun
Fun with non-trivial JSON serialization/deserialization in Dart
https://github.com/csells/dart_json_fun
Last synced: 3 months ago
JSON representation
Fun with non-trivial JSON serialization/deserialization in Dart
- Host: GitHub
- URL: https://github.com/csells/dart_json_fun
- Owner: csells
- License: bsd-3-clause
- Created: 2019-05-27T15:54:15.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-02-23T01:18:14.000Z (about 3 years ago)
- Last Synced: 2024-12-31T20:16:29.523Z (4 months ago)
- Language: Dart
- Size: 8.79 KB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Playing around with non-trivial JSON serialization/deserialization in Dart.
Specifically, wanted a model where all fields are private so that can do other things on mutation (like notify listeners) but also where it looks reasonable on the wire (specifically not nesting a list into a map).
I'm using [the toJson conventions used by jsonEncode](https://api.dartlang.org/stable/2.3.1/dart-convert/jsonEncode.html), even though I find it confusing (jsonEncode returns a string but it expects the call to toJson to return a Map or a List<dynamic>).
Check out [family.dart](https://github.com/csells/dart_json_fun/blob/master/lib/family.dart) for the types and their support for JSON serialization/deserialization:
```
Family:
name (String)
people (custom collection)
items (Iteratable)
name (String)
age (int)
```It takes a lot of surprisingly picky code to make all of this work properly. Check out the [tests](https://github.com/csells/dart_json_fun/blob/master/test/json_fun_test.dart) if you want to see what cases I'm supporting.
Enjoy!
P.S. I spent hours figuring this stuff out, but most of it is covered nicely in Pooja Bhaumik's [Parsing complex JSON in Flutter](https://medium.com/flutter-community/parsing-complex-json-in-flutter-747c46655f51). Recommended!