json_helpers 0.1.4 copy "json_helpers: ^0.1.4" to clipboard
json_helpers: ^0.1.4 copied to clipboard

discontinued
outdated

The `json_helpers` contains functions that make it easier decoding JSON objects directly from strings, lists and maps.

example/example.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<Person>
  string = '[{"name": "Jack"}, {"name": "John"}]';
  var personList = string.jsonList((e) => Person.fromJson(e));
  assert(personList[1].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<Map> to List<Person>
  list = [
    {'name': 'Jack'},
    {'name': 'John'}
  ];
  personList = list.json((e) => Person.fromJson(e));
  assert(personList[1].name == 'John');
}

class Person {
  final String name;

  Person({required this.name});

  factory Person.fromJson(Map<String, dynamic> json) {
    return Person(name: (json['name'] as String?) ?? '');
  }

  Map<String, dynamic> toJson() => {'name': name};
}
3
likes
140
pub points
76%
popularity

Publisher

unverified uploader

The `json_helpers` contains functions that make it easier decoding JSON objects directly from strings, lists and maps.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

More

Packages that depend on json_helpers