Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

Commit

Permalink
Further refactorings, moving generator code into separate folder
Browse files Browse the repository at this point in the history
  • Loading branch information
eredo committed Sep 17, 2018
1 parent 0c5e286 commit bd5b656
Show file tree
Hide file tree
Showing 18 changed files with 356 additions and 309 deletions.
141 changes: 71 additions & 70 deletions README.md
Expand Up @@ -59,6 +59,77 @@ import 'my_class.dart';
final Dartson<String> serializer = _serializer$dartson.useCodec(json);
```


### Encoding / decoding lists

As of dartson `>1.0.0` there are specific `encodeList` and `decodeList` methods. Because of type restrictions
`encodeList` returns an `Object` and `decodeList` expects an `Object`. This should not cause any further actions when
using `json` codec, however when working with the default serializer without any Codec, than a cast to
`List<Map<String, dynamic>>` might be necessary when using the `encodeList` result.

```dart
main() {
final result = serializer.encodeList([
MyClass()..name = 'test1',
MyClass()..name = 'test2',
]) as List<Map<String, dynamic>>;
expect(result, allOf(isList, hasLength(2)));
expect(result[0]['name'], 'test1');
expect(result[1]['name'], 'test2');
}
```

### Replacing entities

Sometimes entities are automatically generated and as such cannot contain any handwritten code, which could provide
further logic and reduce complexity. This is where the `replacement` feature of dartson can help.

Here an example of an entity called `Money` which is replaced using `MoneyImpl` for replacing the operators.

```dart
import 'package:dartson/dartson.dart';
import 'package:dartson/transformers/date_time.dart';
// Imagine Money and Product couldn't be touched.
class Money {
double net;
double gross;
}
class Product {
Money price;
String name;
}
class MoneyImpl extends Money {
operator +(dynamic ob) {
if (obj is! Money) {
throw TypeError();
}
net += ob.net;
gross += ob.gross;
}
}
@Serializer(
entities: [
Money,
Product,
],
replacements: {
Money: MoneyImpl,
},
transformers: [
DateTimeParser,
],
)
final Dartson serializer = _serializer$dartson;
```

### Extending the serializer

Dartson supports extending serializers to provide a module approach. This is necessary to support functionality
Expand Down Expand Up @@ -137,76 +208,6 @@ import 'my_class.dart';
final Dartson serializer = _serializer$dartson;
```

## Encoding / decoding lists

As of dartson `>1.0.0` there are specific `encodeList` and `decodeList` methods. Because of type restrictions
`encodeList` returns an `Object` and `decodeList` expects an `Object`. This should not cause any further actions when
using `json` codec, however when working with the default serializer without any Codec, than a cast to
`List<Map<String, dynamic>>` might be necessary when using the `encodeList` result.

```dart
main() {
final result = serializer.encodeList([
MyClass()..name = 'test1',
MyClass()..name = 'test2',
]) as List<Map<String, dynamic>>;
expect(result, allOf(isList, hasLength(2)));
expect(result[0]['name'], 'test1');
expect(result[1]['name'], 'test2');
}
```

## Replacing entities

Sometimes entities are automatically generated and as such cannot contain any handwritten code, which could provide
further logic and reduce complexity. This is where the `replacement` feature of dartson can help.

Here an example of an entity called `Money` which is replaced using `MoneyImpl` for replacing the operators.

```dart
import 'package:dartson/dartson.dart';
import 'package:dartson/transformers/date_time.dart';
// Imagine Money and Product couldn't be touched.
class Money {
double net;
double gross;
}
class Product {
Money price;
String name;
}
class MoneyImpl extends Money {
operator +(dynamic ob) {
if (obj is! Money) {
throw TypeError();
}
net += ob.net;
gross += ob.gross;
}
}
@Serializer(
entities: [
Money,
Product,
],
replacements: {
Money: MoneyImpl,
},
transformers: [
DateTimeParser,
],
)
final Dartson serializer = _serializer$dartson;
```

## Roadmap for 1.0.0 alpha/beta

- First alpha release evaluates and tests the reuse of `json_serializable`
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
@@ -1,5 +1,11 @@
# Changelog

## 1.0.0-alpha+3

**Breaking changes**
- `DateTimeParser` is deprecated. The default is now the `json_serializable` implementation and it's no longer necessary
to register a transformer at all.

## 1.0.0-alpha+2 (09/17/2018)

- Add replacement functionality
Expand Down
4 changes: 3 additions & 1 deletion lib/builder.dart
Expand Up @@ -3,7 +3,9 @@ library dartson.builder;
import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';

import 'src/generator.dart';
import 'src/generator/generator.dart';

/// Provides a [SharedPartBuilder] for the dartson generator. See README.md for
/// usage.
Builder dartsonBuilder(_) =>
SharedPartBuilder([SerializerGenerator()], 'dartson');
8 changes: 6 additions & 2 deletions lib/src/annotations.dart
Expand Up @@ -19,8 +19,12 @@ class Property {
/// Defines the generation of a serializer. Assign the variable name as private
/// with a suffix "$dartson" to the annotated variable.
///
/// @Serializer(entities: [MyClass])
/// final serializer = _serializer$dartson;
/// @Serializer(
/// entities: [MyClass],
/// replacements: {MyInterface: MyImplementation},
/// transformers: [MyCustomTransformer],
/// )
/// final Dartson<String> serializer = _serializer$dartson.useCodec(json);
///
class Serializer {
/// A list of entities which will be serialized by the [Serializer].
Expand Down

0 comments on commit bd5b656

Please sign in to comment.