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.
This build contains the first version of a transformer. IT IS STILL UNDER DEVELOPMENT AND NOT COMPLETELY TESTED YET 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.
Map<String,List<MyClass>>
)String getAName() => "${whatEver}.Name";
)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"}'
}
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
}
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
}
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);
}
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);
}
Add this to your package's pubspec.yaml file:
dependencies:
dartson: ^0.2.1
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.
Now in your Dart code, you can use:
import 'package:dartson/dartson.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
0.2.7 | Mar 31, 2017 |
|
|
0.2.6 | Oct 5, 2016 |
|
|
0.2.5 | Oct 19, 2015 |
|
|
0.2.4 | Jul 4, 2015 |
|
|
0.2.3 | May 26, 2015 |
|
|
0.2.2 | Mar 2, 2015 |
|
|
0.2.1 | Feb 13, 2015 |
|
|
0.2.0 | Jan 29, 2015 |
|
|
0.1.6 | Jun 6, 2014 |
|
|
0.1.5 | Mar 15, 2014 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
83
|
Health:
Code health derived from static analysis.
[more]
|
--
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
--
|
Overall:
Weighted score of the above.
[more]
|
42
|
This package version is not analyzed, because it is more than two years old. Check the latest stable version for its analysis.
Running dartdoc
failed. (-10 points)
Make sure dartdoc
runs without any issues.
Package | Constraint | Resolved | Available |
---|---|---|---|
Direct dependencies | |||
Dart SDK | >=1.3.0 |