https://github.com/bsutton/json_helpers
The `json_helpers` contains functions that make it easier decoding JSON objects directly from strings, lists and maps.
https://github.com/bsutton/json_helpers
Last synced: 5 months ago
JSON representation
The `json_helpers` contains functions that make it easier decoding JSON objects directly from strings, lists and maps.
- Host: GitHub
- URL: https://github.com/bsutton/json_helpers
- Owner: bsutton
- License: bsd-3-clause
- Created: 2021-09-26T23:19:07.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-03-14T19:13:45.000Z (over 5 years ago)
- Last Synced: 2025-06-07T07:02:05.445Z (about 1 year ago)
- Size: 8.79 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# json_helpers
The `json_helpers` contains functions that make it easier decoding JSON objects directly from strings, lists and maps.
Version 0.1.6
Allows you to simplify decoding JSON objects directly from `String`, `List` and `Map` values.
Easy to use (by calling one method).
Less code, fewer bugs.
The expected result is guaranteed and predictable.
Examples:
```dart
import 'package:json_helpers/json_helpers.dart';
void main() {
List list;
Map map;
String string;
// String to Person
string = '{"name": "Jack"}';
var person = string.json((e) => Person.fromJson(e));
assert(person.name == 'Jack');
// String to List
string = '[{"name": "Jack"}, {"name": "John"}]';
var persons = string.jsonList((e) => Person.fromJson(e));
assert(persons[1].name == 'John');
// String to Map
string =
'{"Jack Shephard": {"name": "Jack"}, "John Locke": {"name": "John"}}';
final personMap = string.jsonMap((e) => Person.fromJson(e));
assert(personMap['John Locke']!.name == 'John');
// Map to Person
map = {'name': 'Jack'};
person = map.json((e) => Person.fromJson(e));
assert(person.name == 'Jack');
// Map to Person
person = fromJson(map, (e) => Person.fromJson(e));
assert(person.name == 'Jack');
// List to List
list = [
{'name': 'Jack'},
{'name': 'John'}
];
persons = list.json((e) => Person.fromJson(e));
assert(persons[1].name == 'John');
}
class Person {
final String name;
Person({required this.name});
factory Person.fromJson(Map json) {
return Person(name: (json['name'] as String?) ?? '');
}
Map toJson() => {'name': name};
}
```