dartson 0.2.7

  • README.md
  • changelog.md
  • Installing
  • Versions
  • 83

dartson

Pub Version Build Status

Dartson is a dart library which converts Dart Objects into their JSON representation. It helps you keep your code clean of fromJSON and toJSON functions by using dart:mirrors reflection. It works after dart2js compiling.

Transformer

Add the following lines to the pubspec.yaml in order to use the transformer:

transformers:
- dartson
...

Remove the @MirrorsUsed annotation and the assigned import if it's no longer used by any other library. When using the transformer, mirrors are completely removed when pub build is called.

Features not completed yet

  • Support of nested generics (example: Map<String,List<MyClass>>)
  • Support of methods within entities (example: String getAName() => "${whatEver}.Name";)
  • "as" import of dartson within a library separated into parts
  • Complete end2end testing

How the transformer works

  1. All dartson imports "package:dartson/dartson.dart" are rewritten to "package:dartson/dartson_static.dart"
  2. Classes that are annotated using "@Entity" receive 3 methods "dartsonEntityEncode", "dartsonEntityDecode", "newEntity" and implement "StaticEntity"

Known issues:

  • Entities cannot contain one of the following methods: "dartsonEntityEncode", "dartsonEntityDecode", "newEntity"
  • The interface StaticEntity will be added to the global namespace, there shouldn't be any other class named the same
  • Entities need to have a default constructor without any arguments
  • Entities of third party libraries do not work
  • Entities can only extend other Entities
  • Dartson transformer should be placed on top of all transformers to prevent CodeTransform issues (known when using polymer)

Serializing objects in dart

library example;

import 'package:dartson/dartson.dart';

@Entity()
class EntityClass {
  String name;
  
  @Property(name:"renamed")
  bool otherName;
  
  @Property(ignore:true)
  String notVisible;
  
  // private members are never serialized
  String _private = "name";
  
  String get doGetter => _private;
}

void main() {
  var dson = new Dartson.JSON();

  EntityClass object = new EntityClass();
  object.name = "test";
  object.otherName = "blub";
  object.notVisible = "hallo";
  
  String jsonString = dson.encode(object);
  print(jsonString);
  // will return: '{"name":"test","renamed":"blub","doGetter":"name"}'
}

Parsing json to dart object

library example;

import 'package:dartson/dartson.dart';

@Entity()
class EntityClass {
  String name;
  String _setted;
  
  @Property(name:"renamed")
  bool otherName;
  
  @Property(ignore:true)
  String notVisible;
  
  List<EntityClass> children;
  
  set setted(String s) => _setted = s;
  String get setted => _setted;
}

void main() {
  var dson = new Dartson.JSON();

  EntityClass object = dson.decode('{"name":"test","renamed":"blub","notVisible":"it is", "setted": "awesome"}', new EntityClass());
  
  print(object.name); // > test
  print(object.otherName); // > blub
  print(object.notVisible); // > it is
  print(object.setted); // > awesome
  
  // to parse a list of items use [decode] and set the third argument to true
  List<EntityClass> list = dson.decode('[{"name":"test", "children": [{"name":"child1"},{"name":"child2"}]},{"name":"test2"}]', new EntityClass(), true);
  print(list.length); // > 2
  print(list[0].name); // > test
  print(list[0].children[0].name); // > child1
}

Mapping Maps and Lists to dart objects

Frameworks like Angular.dart come with several HTTP services which already transform the HTTP response to a map using JSON.encode. To use those encoded Maps or Lists use map.

library example;

import 'package:dartson/dartson.dart';

@Entity()
class EntityClass {
  String name;
  String _setted;
  
  @Property(name:"renamed")
  bool otherName;
  
  @Property(ignore:true)
  String notVisible;
  
  List<EntityClass> children;
  
  set setted(String s) => _setted = s;
  String get setted => _setted;
}

void main() {
  var dson = new Dartson.JSON();

  EntityClass object = dson.map({"name":"test","renamed":"blub","notVisible":"it is", "setted": "awesome"}, new EntityClass());
  print(object.name); // > test
  print(object.otherName); // > blub
  print(object.notVisible); // > it is
  print(object.setted); // > awesome
  
  // to parse a list of items use [map] and set the third argument to true
  List<EntityClass> list = dson.map([{"name":"test", "children": [{"name":"child1"},{"name":"child2"}]},{"name":"test2"}], new EntityClass(), true);
  print(list.length); // > 2
  print(list[0].name); // > test
  print(list[0].children[0].name); // > child1
}

Writting custom TypeTransformers

Transformers are used to encode / decode none serializable types that shouldn't be treated as objects / lists (for example DateTime).


/// A simple DateTime transformer which uses the toString() method.
class DateTimeParser<T> extends TypeTransformer {
  T decode(dynamic value) {
    return DateTime.parse(value);
  }

  dynamic encode(T value) {
    return value.toString();
  }
}

In order to use the TypeTransformer you need to register the transformer in a main function:

// ...
void main() {
  var dson = new Dartson.JSON();
  dson.addTransformer(new DateTimeParser(), DateTime);
}

Use default transformers

library test;

import 'package:dartson/dartson.dart';
import 'package:dartson/transformers/date_time.dart';

void main() {
  var dson = new Dartson.JSON();
  dson.addTransformer(new DateTimeParser(), DateTime);
}

Changelog

0.2.6 (10/05/2015)

  • Bump dependency versions in pubspec

0.2.4 (7/04/2015)

  • Support for "double" types
  • Update of source_spans dependency

0.2.3 (5/25/2015)

  • Updated analyzer and test dependency (thanks to @rightisleft)

0.2.1 (2/10/2015)

  • Changed the default DateTime serialized format to ISO 8601 with Z notation.

0.2.0 (1/29/2015)

  • Added a transformer that generates static serialization rules and does not use mirrors.
  • Breaking changes:
    • You now have to instantiate a Dartson instance instead of relying on global functions.
    • You now add custom transformer to the Dartson instance instead of adding them globally.

0.1.6 (6/05/2014)

  • Added dartson.fill function
  • Compatible to dart 1.4+

0.1.5 (3/14/2014)

  • Added dartson.TypeTransformer see README.md
  • Added package:dartson/default_transformers.dart

0.1.4

  • Fixed @MirrorsUsed annotations
  • Added map and mapList functions to use an already parsed map

Use this package as a library

1. Depend on it

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


dependencies:
  dartson: "^0.2.7"

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:dartson/dartson.dart';
  
Version Uploaded Documentation Archive
0.2.7 Mar 31, 2017 Go to the documentation of dartson 0.2.7 Download dartson 0.2.7 archive
0.2.6 Oct 5, 2016 Go to the documentation of dartson 0.2.6 Download dartson 0.2.6 archive
0.2.5 Oct 19, 2015 Go to the documentation of dartson 0.2.5 Download dartson 0.2.5 archive
0.2.4 Jul 4, 2015 Go to the documentation of dartson 0.2.4 Download dartson 0.2.4 archive
0.2.3 May 26, 2015 Go to the documentation of dartson 0.2.3 Download dartson 0.2.3 archive
0.2.2 Mar 2, 2015 Go to the documentation of dartson 0.2.2 Download dartson 0.2.2 archive
0.2.1 Feb 13, 2015 Go to the documentation of dartson 0.2.1 Download dartson 0.2.1 archive
0.2.0 Jan 29, 2015 Go to the documentation of dartson 0.2.0 Download dartson 0.2.0 archive
0.1.6 Jun 6, 2014 Go to the documentation of dartson 0.1.6 Download dartson 0.1.6 archive
0.1.5 Mar 15, 2014 Go to the documentation of dartson 0.1.5 Download dartson 0.1.5 archive

All 15 versions...

Analysis

We analyzed this package on May 22, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.54.0
  • pana: 0.11.1

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
92 / 100
Health:
Code health derived from static analysis. [more]
67 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
84 / 100
Overall score:
Weighted score of the above. [more]
83
Learn more about scoring.

Platforms

Detected platforms: web, other

Primary library: package:dartson/dartson.dart with components: mirrors.

Suggestions

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 4 errors 15 hints.

    Strong-mode analysis of lib/src/simple_type_transformer.dart failed with the following error:

    line: 8 col: 3
    Invalid override. The type of '_SimpleTypeTransformer.encode' ('(T) → dynamic') isn't a subtype of 'TypeTransformer<dynamic>.encode' ('(dynamic) → dynamic').

    Strong-mode analysis of lib/transformer.dart failed with the following error:

    line: 54 col: 50
    The argument type 'AssetId' can't be assigned to the parameter type 'String'.

    Similar analysis of the following files failed:

    • lib/dartson.dart (hint)
    • lib/dartson_static.dart (hint)
    • lib/src/exceptions.dart (hint)
    • lib/type_transformer.dart (hint)
  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • The description is too long.

    Search engines will display only the first part of the description. Try to keep it under 180 characters.

  • Maintain an example.

    Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use dartson.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.3.0
analyzer >=0.22.4 <0.30.0 0.29.11 0.32.0
barback >=0.15.2+2 <0.16.0 0.15.2+16
logging >=0.9.3 <0.12.0 0.11.3+1
source_maps >=0.10.0 <0.11.0 0.10.5
source_span >=1.0.3 <2.0.0 1.4.0
Transitive dependencies
args 0.13.7 1.4.3
async 2.0.7
charcode 1.1.1
cli_util 0.0.1+2 0.1.3
collection 1.14.9
convert 2.0.1
crypto 2.0.3
csslib 0.14.4
glob 1.1.5
html 0.13.3
isolate 1.1.0 2.0.0
meta 1.1.5
package_config 1.0.3
plugin 0.2.0+2
pool 1.3.5
stack_trace 1.9.2
string_scanner 1.0.2
typed_data 1.1.5
utf 0.9.0+4
watcher 0.9.7+7
when 0.2.0
which 0.1.3
yaml 2.1.13
Dev dependencies
path >=1.3.9 <1.4.0 1.5.1
test >=0.12.15+9 <0.13.0