juicer 0.10.0+2

  • Readme
  • Changelog
  • Example
  • Installing
  • 70

juicer #

Lightweight JSON serialization library for plain, mutable classes.

Used as a dartson replacement.

Important note #

From 0.10.0 upwards the generated juicer files will have a .juicer.dart suffix instead of .pb.dart, because it seems support has arrived for having custom suffices. If you still want to use the old version add a version constraint in your pubspec.yaml file:

dependencies:
  juicer: '^0.9.0'

If you're ready to use the new version:

dependencies:
  juicer: '^0.10.0'

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 .juicer.dart file will be generated.

0.10.0+2 #

  • Fixed the problems around int/double/num deserialization.

0.10.0+1 #

  • Fixed the problems with the botched release

0.10.0 #

  • The build system seems to support alternative file suffixes now, so instead of .pb.dart it's .juicer.dart

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(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.10.0+2

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]
47
Health:
Code health derived from static analysis. [more]
88
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
70
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.6

Health suggestions

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

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

line 140 col 37: 'name' is deprecated and shouldn't be used. Check element, or use getDisplayString().

line 143 col 17: 'name' is deprecated and shouldn't be used. Check element, or use getDisplayString().

line 175 col 33: 'typeSystem' is deprecated and shouldn't be used. Use LibraryElement.typeSystem.

line 176 col 36: 'typeProvider' is deprecated and shouldn't be used. Use LibraryElement.typeProvider.

line 180 col 47: 'typeProvider' is deprecated and shouldn't be used. Use LibraryElement.typeProvider.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
analyzer >=0.32.4 <1.0.0 0.39.4
build >=1.0.0 <2.0.0 1.2.2
source_gen >=0.9.0 <0.10.0 0.9.5
Transitive dependencies
_fe_analyzer_shared 1.0.3
args 1.6.0
async 2.4.1
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
dart_style 1.3.3
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
logging 0.11.4
meta 1.1.8
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.9.3
path 1.6.4
pub_semver 1.4.4
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+14
yaml 2.2.0
Dev dependencies
pedantic ^1.0.0 1.9.0
test any