velix_mapper 1.2.0
velix_mapper: ^1.2.0 copied to clipboard
a flutter mapping framework for class to class and json.
Introduction #
Velix is Dart/Flutter library implementing some of the core parts required in every Flutter application:
- type meta data
- specification and validation of type constraints
- general purpose mapping framework
- json mapper
- model-based two-way form data-binding
- i18n
- dependency injection container
- command pattern for ui actions
Check out some articles on Medium:
Detailed information can be found in the corresponding Wiki.
Mapping #
A general purpose mapping framework let's you declaratively specify mappings:
var mapper = Mapper([
mapping<Money, Money>()
.map(all: matchingProperties()),
mapping<Product, Product>()
.map(from: "status", to: "status")
.map(from: "name", to: "name")
.map(from: "price", to: "price", deep: true),
mapping<Invoice, Invoice>()
.map(from: "date", to: "date")
.map(from: "products", to: "products", deep: true)
]);
var invoice = Invoice(...);
var result = mapper.map(invoice);
As a special case, json mapping is supported as well:
// overall configuration
JSON(
validate: true,
converters: [Convert<DateTime,String>((value) => value.toIso8601String(), convertTarget: (str) => DateTime.parse(str))],
factories: [Enum2StringFactory()]
);
// funny money class
@Dataclass()
@JsonSerializable(includeNull: true) // doesn't make sense here, but anyway...
class Money {
// instance data
@Attribute(type: "length 7")
@Json(name: "c", required: false, defaultValue: "EU")
final String currency;
@Json(name="v", required: false, defaultValue: 0)
@Attribute()
final int value;
const Money({required this.currency, this.value});
}
var price = Money(currency: "EU", value: 0);
var json = JSON.serialize(price);
var result = JSON.deserialize<Money>(json);
Installation #
The library is published on pub.flutter-io.cn