mapper 0.1.1 copy "mapper: ^0.1.1" to clipboard
mapper: ^0.1.1 copied to clipboard

outdated

library converts map to object and object to map

Mapper

Simple library to convert maps to objects and objects to maps

import 'package:mapper/mapper.dart';

class Simple {
  String strProp;
  int intProp;
  bool boolProp;
  double doubleProp;
}

const Map<String, dynamic> simple = const {
  "strProp": "val",
  "intProp": 13,
  "boolProp": true,
  "doubleProp": 12.4,
};

main() {
    Simple obj = decode<Simple>(simple);

    obj.strProp; // val
    obj.intProp; // 13
    obj.boolProp; // true
    obj.doubleProp; 12,4

    Map<String, dynamic> simple2 = encode(obj);

    simple2['strProp'] == simple['strProp']; // true
    simple2['intProp'] == simple['intProp']; // true 
    simple2['boolProp'] == simple['boolProp']; // true
    simple2['doubleProp'] == simple['doubleProp']; // true
}

Basic features:

  • can convert simple types (String, int, boolean, double)
  • can convert Lists and Maps (only with simple types inside)
  • can be added external converters
  • mirror fields are cache after first use to increase speed

Roadmap:

  • add meta to control convertation process
  • add ability to convert complex Maps and Lists with included classes
  • add ability to convert classes with constructors with arguments

Custom converter example:

import 'package:mapper/mapper.dart';

class BoolParser extends Parser {
  bool decode(val) {
    if (val is int) {
      return val == 1;
    }
    return null;
  }

  int encode(val) {
    if (val is bool) {
      return val == true ? 1 : 0;
    }
    return null;
  }
}

class Simple {
  String strProp;
  bool boolProp;
}

main() {

    addParser('bool', new BoolParser());

    Simple obj = new Simple();
    obj.strProp = "string";
    obj.boolProp = true;

    Map<String, dynamic> simple = encode(obj);

    simple['strProp'] == 'string'; // true
    simple['boolProp'] == 1; // true
}
1
likes
0
pub points
57%
popularity

Publisher

unverified uploader

library converts map to object and object to map

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

More

Packages that depend on mapper