dart_codable 0.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 55

dart_codable #

Implementing the Swift Codable Protocol.

Example #

First of all import the package alongside the dart:convert package.

import 'dart:convert';
import 'package:dart_codable/dart_codable.dart';

Have your Class extend the Codable Class. Note that you have to pass down the constructor from the superclass because dart does not automatically create non-default constructors.

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

  String name;
  int age;
  double score;
}

Then simply use it:

String jsonString = "{ \"name\": \"Marco\", \"age\": 24, \"score\": 99.9 }";

Person me = Person.decode(json.decode(jsonString));

print("Name: ${me.name}");
print("Age: ${me.age}");
print("Score: ${me.score}");
print("");

print(json.encode(me.encode()));

Which outputs

Name: Marco
Age: 24
Score: 99.9

{"name":"Marco","age":24,"score":99.9}

[0.2.0] - 10.05.2020 #

  • Support nesting classes.

[0.1.0] - 10.05.2020 #

  • Release version 0.1.0.

[0.0.3] - 10.05.2020 #

  • Reformat the code and extend example.

[0.0.2] - 10.05.2020 #

  • Add doc Strings and update the description.

[0.0.1] - 09.05.2020 #

  • Basic Implementation of the Swift Codable Protocol.

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);
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  dart_codable: ^0.2.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

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

3. Import it

Now in your Dart code, you can use:


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

We analyzed this package on Jul 10, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Analysis suggestions

Package not compatible with SDK flutter

Because it is not compatible with any of the supported runtimes: flutter-native, flutter-web

Package not compatible with runtime flutter-native on android

Because:

  • package:dart_codable/dart_codable.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-native on ios

Because:

  • package:dart_codable/dart_codable.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-native on linux

Because:

  • package:dart_codable/dart_codable.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-native on macos

Because:

  • package:dart_codable/dart_codable.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-native on windows

Because:

  • package:dart_codable/dart_codable.dart that imports:
  • dart:mirrors

Package not compatible with runtime flutter-web on web

Because:

  • package:dart_codable/dart_codable.dart that imports:
  • dart:mirrors

Package not compatible with runtime js

Because:

  • package:dart_codable/dart_codable.dart that imports:
  • dart:mirrors

Package not compatible with runtime native-aot

Because:

  • package:dart_codable/dart_codable.dart that imports:
  • dart:mirrors

Health suggestions

Format lib/dart_codable.dart.

Run dartfmt to format lib/dart_codable.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
reflectable ^2.2.4 2.2.5
Transitive dependencies
_fe_analyzer_shared 4.0.0 5.0.0
analyzer 0.39.10 0.39.12
args 1.6.0
async 2.4.2
build 1.3.0
build_config 0.4.2
build_daemon 2.1.4
build_resolvers 1.3.9 1.3.10
build_runner 1.10.0
build_runner_core 5.2.0 6.0.0
built_collection 4.3.2
built_value 7.1.0
charcode 1.1.3
checked_yaml 1.0.2
code_builder 3.4.0
collection 1.14.13 1.15.0-nullsafety
convert 2.1.1
crypto 2.1.5
csslib 0.16.1
dart_style 1.3.6
fixnum 0.10.11
glob 1.2.0
graphs 0.2.0
html 0.14.0+3
http_multi_server 2.2.0
http_parser 3.1.4
io 0.3.4
js 0.6.2
json_annotation 3.0.1
logging 0.11.4
matcher 0.12.8
meta 1.2.2 1.3.0-nullsafety
mime 0.9.6+3
node_interop 1.1.1
node_io 1.1.1
package_config 1.9.3
path 1.7.0
pedantic 1.9.2
pool 1.4.0
pub_semver 1.4.4
pubspec_parse 0.1.5
quiver 2.1.3
shelf 0.7.7
shelf_web_socket 0.2.3
source_span 1.7.0
stack_trace 1.9.5
stream_channel 2.0.0
stream_transform 1.2.0
string_scanner 1.0.5
term_glyph 1.1.0
timing 0.1.1+2
typed_data 1.2.0 1.3.0-nullsafety
watcher 0.9.7+15
web_socket_channel 1.1.0
yaml 2.2.1
Dev dependencies
test any
test_coverage ^0.4.1