juicer 0.9.0+4

  • Readme
  • Changelog
  • Example
  • Installing
  • 74

juicer #

Lightweight JSON serialization library for plain, mutable classes.

Used as a dartson replacement.

Example #

import "dart:convert";
import "package:juicer/juicer_vm.dart";
import "package:juicer/metadata.dart";

@juiced
class Party {
  String key;
  String name;
  String country;
  String city;
  @Property(name: "street_address")
  String streetAddress;
  String zip;

  @Property(ignore: true)
  String get address => "$name\n$streetAddress\n$city\n$country";
}

void main() {
  Juicer juicer = juiceClasses([Party]);
  Map asMap = juicer.encode(new Party()
    ..key = "company_1234556"
    ..name = "Random Ltd."
    ..country = "Sovereignland"
    ..city = "Capitalcity"
    ..streetAddress = "1 Main Street"
    ..zip = "11111");
  // the map is encodable to JSON (you can also use encodeJson/decodeJson)
  print(json.encode(asMap));
  // create a copy by decoding the map
  Part copy = juicer.decode(asMap, (_) => Party());
  print(copy.address);
}

Usage #

A Juicer object “knows” how to encode or decode some classes into maps, and handles all the JSON primitives as well. There are two modes of operation:

  • using the build system for every dart file which exports other dart files with juiced classes it will generate the class-specific encoder classes for all those classes and a global Juicer juicer variable for those classes in the generated file (see https://github.com/skawa-universe/juicer_gen_test for the code generation in action)
  • using mirrors it can create Juicer objects on the fly

In both cases all classes must be marked with the @juiced annotation (from metadata.dart), the mirrors implementation (juicer_vm.dart) will map non-juiced classes as well in some cases.

Mirrors based implementation #

The mirrors implementation provides the following functions:

  • createJuicerForLibraries: juices all the libraries, or a filter can be provided which will filter based on the package URI
  • juiceLibraries: juices the libraries with a specific name (for example you have a comm library with all these classes you can call juiceLibraries(["comm"]) to get a juicer for all these)
  • juiceClasses: juices a list of classes, the default behavior is to juice all classes and the referenced classes regardless of whether they have @juiced annotation

Code generator based implementation #

For every dart file that exports other dart files containing @juiced classes a .pb.dart file will be generated.

0.9.0+4 #

  • Fixed lint warnings

0.9.0+3 #

  • README update

0.9.0+2 #

  • Documentation

0.9.0+1 #

  • Formatting changes

0.9.0 #

  • Initial release

example/main.dart

import "dart:convert";
import "package:juicer/juicer_vm.dart";
import "package:juicer/metadata.dart";

@juiced
class Party {
  String key;
  String name;
  String country;
  String city;
  @Property(name: "street_address")
  String streetAddress;
  String zip;

  @Property(ignore: true)
  String get address => "$name\n$streetAddress\n$city\n$country";
}

void main() {
  Juicer juicer = juiceClasses([Party]);
  Map asMap = juicer.encode(new Party()
    ..key = "company_1234556"
    ..name = "Random Ltd."
    ..country = "Sovereignland"
    ..city = "Capitalcity"
    ..streetAddress = "1 Main Street"
    ..zip = "11111");
  print(json.encode(asMap));
  print(juicer.decode(asMap, (_) => Party()).address);
}

Use this package as a library

1. Depend on it

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


dependencies:
  juicer: ^0.9.0+4

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

We analyzed this package on Oct 21, 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:juicer/juicer.dart.

Health suggestions

Fix lib/generator.dart. (-3.93 points)

Analysis of lib/generator.dart reported 8 hints, including:

line 177 col 10: 'isAssignableTo' is deprecated and shouldn't be used.

line 182 col 47: 'mapType' is deprecated and shouldn't be used.

line 184 col 17: 'isAssignableTo' is deprecated and shouldn't be used.

line 188 col 17: 'isEquivalentTo' is deprecated and shouldn't be used.

line 193 col 17: 'isEquivalentTo' is deprecated and shouldn't be used.

Format lib/src/vm/mapper.dart.

Run dartfmt to format lib/src/vm/mapper.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
analyzer >=0.32.4 <1.0.0 0.38.5 0.39.0
build >=1.0.0 <2.0.0 1.2.0
source_gen >=0.9.0 <0.10.0 0.9.4+5
Transitive dependencies
args 1.5.2
async 2.4.0
charcode 1.1.2
collection 1.14.12
convert 2.1.1
crypto 2.1.3
csslib 0.16.1
dart_style 1.3.2
front_end 0.1.27 0.1.28
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
kernel 0.3.27 0.3.28
logging 0.11.3+2
meta 1.1.7
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0
path 1.6.4
pedantic 1.8.0+1
pub_semver 1.4.2
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+12
yaml 2.2.0
Dev dependencies
test any