jsonable_object 1.0.2 copy "jsonable_object: ^1.0.2" to clipboard
jsonable_object: ^1.0.2 copied to clipboard

A micro plugin that displays interfaces for creating json from an object instance, and building an object instance from a json

example/jsonable_object_example.dart

import 'dart:async';

import 'package:jsonable_object/jsonable_object.dart';

class User implements IConvertToJson {
  final int id;

  const User(this.id);

  @override
  FutureOr<Map<String, dynamic>> toJson() {
    return {'id': id};
  }
}

class FactoryUser implements IFactoryObjectFromJson<User> {
  @override
  FutureOr<User> fromJson(FutureOr<Map<String, dynamic>> json) async {
    final map = await json;
    return User(map['id']);
  }
}

void main() async {
  final json = <String, int>{'id': 1};

  final jsonFuture = Future.value(<String, int>{'id': 2});

  final user = await FactoryUser().fromJson(json);
  final user2 = await FactoryUser().fromJson(jsonFuture);

  final convertedJson = await user.toJson();
  final convertedJson2 = await user2.toJson();

  print(json); // show {id: 1}
  print(convertedJson); // show {id: 1}

  print(jsonFuture); // Instance of 'Future<Map<String, int>>'
  print(convertedJson2); // show {id: 2}
}
0
likes
120
pub points
0%
popularity

Publisher

unverified uploader

A micro plugin that displays interfaces for creating json from an object instance, and building an object instance from a json

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

More

Packages that depend on jsonable_object