json_class 1.0.3

  • Readme
  • Changelog
  • Installing
  • 78

json_class #

Singular class that when extended can encode itself to a JSON compatible map or list. This also provides convenience functions to decode from a JSON compatible map or list to the actual data model.

Using the library #

Add the repo to your Flutter pubspec.yaml file.

dependencies:
  json_class: <<version>> 

Then run...

flutter packages get

Example #

import 'package:json_class/json_class.dart';
import 'package:meta/meta.dart';

@immutable
class Person extends JsonClass {
  Person({
    this.age,
    this.minor,
    this.firstName,
    this.lastName,
  });

  final int age;
  final bool minor;
  final String firstName;
  final String lastName;

  static Person fromDynamic(dynamic map) {
    // It's recommended to use dynamic over Map<String, dynamic> because it's
    // compatible with other types of map-like results such as Firebase Realtime
    // Database's values or the ones returned from sqlflite.
    Person result;

    if (map != null) {
      result = Person(
        age: JsonClass.parseInt(map['age']),
        minor: JsonClass.parseBool(map['minor']),
        firstName: map['firstName'],
        lastName: map['lastName'],
      );
    }

    return result;
  }

  static List<Person> fromDynamicList(Iterable<dynamic> list) {
    List<Person> results;

    if (list != null) {
      results = [];
      for (dynamic map in list) {
        results.add(fromDynamic(map));
      }
    }

    return results;
  }

  @override
  Map<String, dynamic> toJson() => JsonClass.removeNull({
    age: age,
    minor: minor,
    firstName: firstName,
    lastName: lastName,
  });
}


class SomeoneElseWhoUsesMyUiFramework extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MyUiFramework(
      childWidgetBuilder: (BuildContext context, Widget child) {
        return Container(
          // This would allow you to find by key this specific widget
          key: ValueKey('MySpiffyUiWidgetKey'),
          child: child,
        );
      }
    );
  }
}

[1.0.3] - June 23rd, 2020 #

  • Added ability to parse int and double values from the Hex ("0xff") format

[1.0.2] - June 19th, 2020 #

  • Added fromDynamicList to JsonClass
  • Added "infinity" as a valid value for parseDouble
  • Changed signature of JsonClassListBuilder to guarantee it is an Iterable

[1.0.1] - June 11th, 2020 #

  • Added JsonClassBuilder and JsonClassListBuilder types

[1.0.0] - June 6th, 2020 #

  • Initial release

Use this package as a library

1. Depend on it

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


dependencies:
  json_class: ^1.0.3

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:json_class/json_class.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
60
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
78
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.14

Maintenance suggestions

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 json_class.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.7.0 <3.0.0
logging ^0.11.4 0.11.4
meta ^1.1.8 1.2.2 1.3.0-nullsafety
Dev dependencies
test any
test_coverage any