codable 1.0.0

  • Readme
  • Changelog
  • Installing
  • 79

codable #

Build Status

A library for encoding and decoding dynamic data into Dart objects.

Basic Usage #

Data objects extend Coding:

class Person extends Coding {
  String name;

  @override
  void decode(KeyedArchive object) {
    // must call super
    super.decode(object);

    name = object.decode("name");   
  }

  @override
  void encode(KeyedArchive object) {
    object.encode("name", name);
  }
}

An object that extends Coding can be read from JSON:

final json = json.decode(...);
final archive = KeyedArchive.unarchive(json);
final person = Person()..decode(archive);

Objects that extend Coding may also be written to JSON:

final person = Person()..name = "Bob";
final archive = KeyedArchive.archive(person);
final json = json.encode(archive);

Coding objects can encode or decode other Coding objects, including lists of Coding objects and maps where Coding objects are values. You must provide a closure that instantiates the Coding object being decoded.

class Team extends Coding {

  List<Person> members;
  Person manager;

  @override
  void decode(KeyedArchive object) {
    super.decode(object); // must call super

    members = object.decodeObjects("members", () => Person());
    manager = object.decodeObject("manager", () => Person());
  }

  @override
  void encode(KeyedArchive object) {
    object.encodeObject("manager", manager);
    object.encodeObjects("members", members);
  }
}

Dynamic Type Casting #

Types with primitive type arguments (e.g., List<String> or Map<String, int>) are a particular pain point when decoding. Override castMap in Coding to perform type coercion. You must import package:codable/cast.dart as cast and prefix type names with cast.

import 'package:codable/cast.dart' as cast;
class Container extends Coding {  
  List<String> things;

  @override
  Map<String, cast.Cast<dynamic>> get castMap => {
    "things": cast.List(cast.String)
  };
  

  @override
  void decode(KeyedArchive object) {
    super.decode(object);

    things = object.decode("things");
  }

  @override
  void encode(KeyedArchive object) {
    object.encode("things", things);
  }
}

Document References #

Coding objects may be referred to multiple times in a document without duplicating their structure. An object is referenced with the $key key. For example, consider the following JSON:

{
  "components": {
    "thing": {
      "name": "The Thing"
    }    
  },
  "data": {
    "$ref": "#/components/thing"
  }
}

In the above, the decoded value of data inherits all properties from /components/thing:

{
  "$ref": "#/components/thing",
  "name": "The Thing"
}

You may create references in your in-memory data structures through the Coding.referenceURI.

final person = Person()..referenceURI = Uri(path: "/teams/engineering/manager");

The above person is encoded as:

{
  "$ref": "#/teams/engineering/manager"
}

You may have cyclical references.

See the specification for JSON Schema and the $ref keyword for more details.

1.0.0 #

  • Initial version

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  codable: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:codable/codable.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
71
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
71
Overall:
Weighted score of the above. [more]
79
Learn more about scoring.

We analyzed this package on Oct 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:codable/codable.dart.

Health suggestions

Fix lib/src/keyed_archive.dart. (-1 points)

Analysis of lib/src/keyed_archive.dart reported 2 hints:

line 36 col 81: Use = to separate a named parameter from its default value.

line 52 col 73: Use = to separate a named parameter from its default value.

Format lib/cast.dart.

Run dartfmt to format lib/cast.dart.

Format lib/codable.dart.

Run dartfmt to format lib/codable.dart.

Fix additional 4 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/codable.dart (Run dartfmt to format lib/src/codable.dart.)
  • lib/src/coding.dart (Run dartfmt to format lib/src/coding.dart.)
  • lib/src/list.dart (Run dartfmt to format lib/src/list.dart.)
  • lib/src/resolver.dart (Run dartfmt to format lib/src/resolver.dart.)

Maintenance suggestions

Package is getting outdated. (-19.45 points)

The package was last published 62 weeks ago.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and codable.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
meta ^1.1.5 1.1.7
Dev dependencies
test ^1.3.0