object_mapper 1.0.0+2

  • Readme
  • Changelog
  • Example
  • Installing
  • 71

Object Mapper #

Coverage GitHub issues GitHub stars GitHub license


A package written in Dart that makes it easy for you to convert your model objects to and from JSON. It's inspired by ObjectMapper

object_mapper vs json_annotation

  • No extras file (*.g.dart), no need to use builder_runner
  • Re-usable Transform (known as Converter in json_annotation) with generic

Implement #

  • Step 1: Extend your class with Mappable mixin
class TestInfo with Mappable {
    //
}

  • Step 2: Override Mappable.mapping method & add your map functions. Check more use cases here
class TestInfo with Mappable {
 int id;
  
 @override
 void mapping(Mapper map) {
  map("id", id, (v) => id = v);
 }
}

  • Step 3: Register factory for new model into Mappable.factories

    Mappable.factories = {
      TestInfo: () => TestInfo()
    };
    

Usage #

  • Map to Object
final json = {
 "id" : 2
};

final info = Mapper.fromJson(json).toObject<TestInfo>();
print(info.id); // 2
  • Object to Map
final info = TestInfo();
info.id = 2;
final json = info.toJson();
print(json); // { "id": 2 }

Map - Use Cases #

  • int, string, numeric, bool
void mapping(Mapper map) {
 map("field", field, (v) => field = v);
}
  • List of object or nested object
void mapping(Mapper map) {
 map<ObjectClass>("field", field, (v) => field = v);
}
  • With transform, such as DateTransform, EnumTransform
DateTime time;
void mapping(Mapper map) {
 map("time", time, (v) => time = v, DateTransform());
}

Custom transform #

Implement your class with Transformable

class EnumTransform<Object extends Enumerable, JSON>
    with Transformable<Object, JSON> {
  Object fromJson(value) {
    if (value == null || !(value is JSON)) return null;
    return RawRepresentable(Object, value);
  }

  JSON toJson(Object value) {
    if (value == null) return null;
    return value.rawValue;
  }
}

1.0.0+2 #

Updated README.md

1.0.0+1 #

Updated to object_mapper: ^1.0.0 and Documentation Updates

0.0.1 #

Initial Version of the library.

example/lib/main.dart

import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:object_mapper/object_mapper.dart';

final json = {
  "id": 1,
  "name": "Fantasy",
  "books": [
    {
      "id": 1,
      "name": "The Hobbit",
      "author": "J. R. R. Tolkien",
      "publish_at": "1937-09-21"
    },
    {
      "id": 2,
      "name": "Harry Potter",
      "author": "J. K. Rowling",
      "publish_at": "1997-12-01"
    }
  ]
};

class CategoryInfo with Mappable {
  // Attributes
  int id;
  String name;
  List<BookInfo> books;

  @override
  void mapping(Mapper map) {
    map("id", id, (v) => id = v);
    map("name", name, (v) => name = v);
    map<BookInfo>("books", books, (v) => books = v);
  }
}

class BookInfo with Mappable {
  // Attributes
  int id;
  String name;
  String author;
  DateTime publishAt;

  @override
  void mapping(Mapper map) {
    map("id", id, (v) => id = v);
    map("name", name, (v) => name = v);
    map("author", author, (v) => author = v);
    map("publish_at", publishAt, (v) => publishAt = v, DateTransform());
  }
}

void main() async {

  // Register factory
  Mappable.factories = {
    CategoryInfo: () => CategoryInfo(),
    BookInfo: () => BookInfo()
  };

  // Convert json to CategoryInfo
  final category = Mapper.fromJson(json).toObject<CategoryInfo>();
  print(category.toJson());
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  object_mapper: ^1.0.0+2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:object_mapper/object_mapper.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
41
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
71
Learn more about scoring.

We analyzed this package on Mar 31, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Format lib/src/enumerable.dart.

Run flutter format to format lib/src/enumerable.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_coverage_badge ^0.0.1+1
flutter_test