dart_json_mapper 2.2.14 copy "dart_json_mapper: ^2.2.14" to clipboard
dart_json_mapper: ^2.2.14 copied to clipboard

This package allows programmers to annotate Dart objects in order to serialize / deserialize them from / to JSON.

example/example.dart

import 'package:dart_json_mapper/dart_json_mapper.dart'
    show JsonMapper, jsonSerializable, JsonProperty, enumConverterNumeric;

import 'example.mapper.g.dart' show initializeJsonMapper;

@jsonSerializable
enum Color { red, blue, green, brown, yellow, black, white }

@jsonSerializable
class Car {
  @JsonProperty(name: 'modelName')
  String model;

  Color color;

  Car(this.model, this.color);

  @override
  String toString() {
    return 'Car{model: $model, color: $color}';
  }
}

@jsonSerializable
class Person {
  List<String> skills = ['Go', 'Dart', 'Flutter'];

  @JsonProperty(name: 'last_promotion_date', ignore: true)
  DateTime? lastPromotionDate;

  @JsonProperty(name: 'hire_date')
  DateTime hireDate = DateTime(2003, 02, 28);

  bool married = true;
  String name = 'Forest';

  @JsonProperty(ignore: true)
  num? salary;

  num? dob;
  num age = 36;
  var lastName = 'Gump';

  @JsonProperty(name: 'eye_color')
  Color eyeColor = Color.blue;

  @JsonProperty(converter: enumConverterNumeric)
  Color hairColor = Color.brown;

  List<Car> vehicles = [Car('Tesla', Color.black), Car('BMW', Color.red)];

  String get fullName => '$name $lastName';
}

void main() {
  initializeJsonMapper();

  final personJson = '''{
 "skills": [
  "Go",
  "Dart",
  "Flutter"
 ],
 "hire_date": "2003-02-28",
 "married": true,
 "name": "Forest",
 "dob": null,
 "age": 36,
 "lastName": "Gump",
 "eye_color": "Blue",
 "hairColor": 3,
 "vehicles": [
  {
   "modelName": "Tesla",
   "color": "Black"
  },
  {
   "modelName": "BMW",
   "color": "Red"
  }
 ]
}''';

  // Serialize
  print(JsonMapper.serialize(Person()));

  // Deserialize
  print(JsonMapper.deserialize<Person>(personJson));
}
272
likes
150
points
2.98k
downloads

Publisher

verified publisheredensapple.com

Weekly Downloads

This package allows programmers to annotate Dart objects in order to serialize / deserialize them from / to JSON.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

analyzer, build, build_config, collection, intl, meta, path, pubspec_parse, reflectable

More

Packages that depend on dart_json_mapper