json_serializable 0.1.0+1 copy "json_serializable: ^0.1.0+1" to clipboard
json_serializable: ^0.1.0+1 copied to clipboard

outdatedDart 1 only

Generates utilities to aid in serializing to/from JSON.

Build Status

Provides source_gen Generators which generate code to make it simple to serialize to and from JSON.

Example #

Given a library example.dart with an Person class annotated with @JsonSerializable():

library json_serializable.example;

import 'package:json_serializable/annotations.dart';
part 'example.g.dart';

@JsonSerializable()
class Person extends Object with _$PersonSerializerMixin {
  final String firstName, middleName, lastName;

  @JsonKey('date-of-birth')
  final DateTime dateOfBirth;

  Person(this.firstName, this.lastName, {this.middleName, this.dateOfBirth});

  factory Person.fromJson(json) => _$PersonFromJson(json);
}

source_gen creates the corresponding part example.g.dart:

part of json_serializable.example;

Person _$PersonFromJson(Map json) =>
    new Person(json['firstName'] as String, json['lastName'] as String,
        middleName: json['middleName'] as String,
        dateOfBirth: json['date-of-birth'] == null
            ? null
            : DateTime.parse(json['date-of-birth']));

abstract class _$PersonSerializerMixin {
  String get firstName;
  String get middleName;
  String get lastName;
  DateTime get dateOfBirth;
  Map<String, dynamic> toJson() => <String, dynamic>{
        'firstName': firstName,
        'middleName': middleName,
        'lastName': lastName,
        'date-of-birth': dateOfBirth?.toIso8601String(),
      };
}
3324
likes
0
pub points
99%
popularity

Publisher

verified publishergoogle.dev

Generates utilities to aid in serializing to/from JSON.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

analyzer, build, cli_util, path, source_gen

More

Packages that depend on json_serializable