serializer 0.2.1 copy "serializer: ^0.2.1" to clipboard
serializer: ^0.2.1 copied to clipboard

outdatedDart 1 only

Serialization to JSON using reflectable

Build Status Coverage Status Pub version

serializer #

Serializer to JSON using reflectable.

##Example

Import the library #

import 'package:serializer/serializer.dart';

Define your model #

@serializable
class MyModel {
    String name;
    
    @ignore //ignore this attribute during serialization
    num age;

    //constructor need to be without parameters or with optional or positional.
    MyModel([this.name, this.age]);
}

Serialize #

 main() async {
    await initSerializer(
        type_info_key: "@dart_type" // add a field "@dart_type" to the json, storing the type of the type of the Dart Object, default: "@type"
    ); // initSerializer will map every class annotated with @serializable
    
    MyModel model = new MyModel("John", 24);
    String json = Serializer.toJson(model);
    Map jsonMap = Serializer.toMap(model);
    print(json);
    
 }

Deserialize #

model = Serializer.fromJson(json, MyModel);
model = Serializer.fromMap(jsonMap, MyModel);

You can extend you Model with the class Serialize to inherit of basic convert method.

 Map toMap();
 String toString();
 String toJson();

Work with List #

 String jsonList = Serializer.toJson(listModel);
 List<MyModel> list =  Serializer.fromList(jsonList, MyModel) as List<MyModel>;

Example

1
likes
0
pub points
14%
popularity

Publisher

unverified uploader

Serialization to JSON using reflectable

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

reflectable

More

Packages that depend on serializer