Giter VIP home page Giter VIP logo

model_map.dart's Introduction

ModelMap

Build Status

An experiment in using mirrors to convert from a map to a model and vice versa.

Introduction

Mirrors are a way of performing reflection in Dart. This permits an unknown class to be traversed and information about fields, properties and methods to be extracted, which is particularly useful if you wish to convert a serialized representation of an object such as JSON into an actual object instance.

Mirrors are now properly supported in dart2js, so model_map will work even if compiled to javascript as long as you decorate your entity with @reflectable.

At this stage ModelMap only worries about non-static, public fields and will ignore getters and setters.

Examples

Simple model

import 'package:model_map/model_map.dart';

class SimpleModel extends ModelMap
{
  String string;
  int integer;
  bool flag;
  num float;
}

main()
{
  var map   = { 'string': 'some text', 'integer': 42, 'flag': true, 'float': 1.23 };
  var model = new SimpleModel().fromMap(map);

  // The model is populated and ready to use at this point
}

You can also take an existing model and convert it to a map

// A map of <String, dynamic>
var map = model.toMap();

Using JSON

A couple of utility functions are included that simply wrap fromMap and toMap using the built-in parse and stringify capabilities so that you can simply call fromJson and toJson on your model instance.

Compiling to javascript

Now that dart2js supports enough of the mirror system, ModelMap will work in a javascript application. One thing to be aware of is that dart2js performs tree shaking and to ensure that your entities can be mirrored properly when compiled to javascript you should decorate them with a helper attribute:

@reflectable
class SimpleModel extends ModelMap
{
  String string;
  int integer;
}

Complex model

ModelMap can support model instances within models, but they must all extend the ModelMap class. It also handles List and Map collections (maps are limited to string keys only, since this is all that JSON can use).

import 'package:model_map/model_map.dart';

class ComplexModel extends ModelMap
{
  int integer;
  SimpleModel simple;
  List<SimpleModel> modelList;
  Map<String, DateTime> dateMap;
  Map<String, SimpleModel> modelMap;
  Map<String, List<SimpleModel>> modelListMap;
}

main()
{
  var json  = getJsonFromServer();
  var model = new ComplexModel().fromJson(json);

  print(model.modelMap['a key'].flag);
}

ModelMap should cope with reasonably complex object trees.

DateTime

When converting to JSON or a map, ModelMap will always convert dates to an ISO 8601 string, however when parsing a map it will accept an ISO 8601 string or an integer representing UTC unix time.

model_map.dart's People

Contributors

parnham avatar

Watchers

rosa maria palacios juncosa avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.