dart_codable 0.2.0
dart_codable: ^0.2.0 copied to clipboard

Implementing the Swift Codable Protocol. It provides the abstract classes Encodable, Decodable and Codable to extend a custom class with.

example/main.dart

import 'dart:convert';

import 'package:dart_codable/dart_codable.dart';

class Person extends Codable {
  Person.decode(Map<String, dynamic> data) : super.decode(data);

  String name;
  int age;
  Grades grades;
}

class Grades extends Codable {
  Grades.decode(Map<String, dynamic> data) : super.decode(data);

  String subject;
  double grade;
}

String jsonString =
    "{ \"name\": \"Marco\", \"age\": 24, \"grades\": { \"subject\": \"Math\", \"grade\": 1.0 } }";
String corruptedJsonString =
    "{ \"notname\": \"Marco\", \"age\": 24, \"score\": 99.9 }";

void main() {
  try{
    Person me = Person.decode(json.decode(jsonString));

    print("Name: ${me.name}");
    print("Age: ${me.age}");
    print("Grades subject: ${me.grades.subject}");
    print("Grades grade: ${me.grades.grade}");
    print("");

    print(json.encode(me.encode()));
    print("");
  } catch(e) {
    print(e);
  }

  try {
    Person.decode(json.decode(corruptedJsonString));
  } catch (e) {
    print(e);
  }
}
3
likes
80
pub points
38%
popularity

Publisher

Unknown

Implementing the Swift Codable Protocol. It provides the abstract classes Encodable, Decodable and Codable to extend a custom class with.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD (LICENSE)

Dependencies

reflectable

More

Packages that depend on dart_codable