json_serializable_ioc 1.0.0 copy "json_serializable_ioc: ^1.0.0" to clipboard
json_serializable_ioc: ^1.0.0 copied to clipboard

json_serializable_ioc simplifies the serialization and deserialization of models by encapsulating toJson and fromJson methods in a container. It allows developers to register converters and factories [...]

example/json_serializable_ioc_example.dart

import 'package:json_serializable_ioc/json_serializable_ioc.dart';

class User {
  final int id;
  final String name;

  User({
    required this.id,
    required this.name,
  });

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

  Map<String, dynamic> toJson() {
    return {
      "id": id,
      "name": name,
    };
  }
}

void main() {
  // register the factory inside the ioc
  JsonFactoryIoc().registerFactory<User>(
    (json) => User.fromJson(json),
  );

  JsonConverterIoc().registerConverter<User>(
    (model) => model.toJson(),
  );
  final int userId = 55;
  final String userName = 'Test User';
  // call the factory using the [factory<T>] method.
  User user = factory({
    'id': userId,
    'name': userName,
  });

  print('user.id: ${user.id} = $userId');
  print('user.name: ${user.name} = $userName');

  // call the converter using [converter<T>] method.
  Map<String, dynamic> map = converter<User>(user);

  print('user.id: ${map['id']} = $userId');
  print('user.name: ${map['name']} = $userName');

}
0
likes
140
points
46
downloads

Publisher

unverified uploader

Weekly Downloads

json_serializable_ioc simplifies the serialization and deserialization of models by encapsulating toJson and fromJson methods in a container. It allows developers to register converters and factories for any model, enabling the dynamic use of these methods without knowing their exact names.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

More

Packages that depend on json_serializable_ioc