An open API service indexing awesome lists of open source software.

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.

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};
}

```