Skip to content

Commit

Permalink
Add support for JSON.
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldemarco committed Aug 28, 2019
1 parent 3cb1459 commit 35ef6e5
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 279 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.0.3

* Added support for toJson and fromJson
* Renamed toFirebaseMap/fromFirebaseMap -> toMap/fromMap

## 0.0.2

* Various package structure fixes
Expand Down
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class $Point {
}
```

Enjoy your generated named constructor, ==/hashCode, toString, and copyWith:
Enjoy your generated named constructor, ==/hashCode, toString, copyWith, and serialization:

```dart
// GENERATED CODE - DO NOT MODIFY BY HAND
Expand All @@ -43,7 +43,7 @@ class Point {
@override
String toString() {
return 'Point{x: $x, y: $y}';
return 'Point{x: ' + x.toString() + ', y: ' + y.toString() + '}';
}
Point copyWith({
Expand All @@ -55,6 +55,16 @@ class Point {
y: y ?? this.y,
);
}
Point.fromMap(Map<String, dynamic> m)
: x = m['x'],
y = m['y'];
Map<String, dynamic> toMap() => {'x': x, 'y': y};
factory Point.fromJson(String json) => Point.fromMap(jsonDecode(json));
String toJson() => jsonEncode(toMap());
}
```

Expand All @@ -64,11 +74,11 @@ Add the following to your pubspec.yaml:

```yaml
dependencies:
auto_data: ^0.0.2
auto_data: ^0.0.3

dev_dependencies:
build_runner: ^1.0.0
auto_data_generator: ^0.0.2
auto_data_generator: ^0.0.3
```
Create your `point.dart` file with correct imports:
Expand All @@ -77,6 +87,7 @@ Create your `point.dart` file with correct imports:
import 'package:meta/meta.dart';
import 'package:collection/collection.dart';
import 'package:auto_data/auto_data.dart';
import 'dart:convert';
part 'point.g.dart';
Expand Down Expand Up @@ -114,6 +125,7 @@ print(p3.toString());
import 'package:collection/collection.dart';
import 'package:meta/meta.dart';
import 'package:auto_data/auto_data.dart';
import 'dart:convert';
import 'foo.dart';
part 'person.g.dart';
Expand Down Expand Up @@ -151,9 +163,9 @@ class $Person {
- [x] Default values by assigning during declaration: `String name = 'Paul';`
- [x] Add @nullable annotation for fields that are not required
- [ ] Deep immutability for Map
- [ ] Deep immutability for List
- [ ] Serialization toMap/fromMap
- [ ] Serialization toJson/fromJson
- [x] Deep immutability for List
- [x] Serialization toMap/fromMap
- [x] Serialization toJson/fromJson

## Limitations

Expand All @@ -168,4 +180,5 @@ profile = profile.copyWith(imageUrl: null); // This won't have an effect since c
## References

1. [Issue: Statically tracked shared immutable objects](https://github.com/dart-lang/language/issues/125)
1. [Proposal: Shared immutable objects](https://github.com/dart-lang/language/blob/master/working/0125-static-immutability/feature-specification.md)
1. [Proposal: Shared immutable objects](https://github.com/dart-lang/language/blob/master/working/0125-static-immutability/feature-specification.md)
1. [Patterns and related features](https://github.com/dart-lang/language/issues/546)
2 changes: 1 addition & 1 deletion auto_data/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: auto_data
description: >
Automatically generate simple data class files for Dart.
This is the runtime dependency.
version: 0.0.2
version: 0.0.3
author: Paul DeMarco <[email protected]>
homepage: https://www.github.com/pauldemarco/auto_data

Expand Down
34 changes: 25 additions & 9 deletions auto_data_generator/lib/src/file_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ class FileGenerator {
buffer.writeln(_generateHashCode(dataClass));
buffer.writeln(_generateToString(dataClass));
buffer.writeln(_generateCopyWith(dataClass));
buffer.writeln(_generateFromRtdbMap(dataClass));
buffer.writeln(_generateToRtdbMap(dataClass));
buffer.writeln(_generateFromMap(dataClass));
buffer.writeln(_generateToMap(dataClass));
buffer.writeln(_generateFromJson(dataClass));
buffer.writeln(_generateToJson(dataClass));
buffer.write(_generateClassFooter(dataClass));
});
return buffer;
Expand Down Expand Up @@ -191,13 +193,13 @@ class FileGenerator {
return buffer;
}

static StringBuffer _generateFromRtdbMap(DataClass c) {
static StringBuffer _generateFromMap(DataClass c) {
String _getConverter(
String name, String type, String argument, bool isEnum) {
if (type.startsWith('DateTime')) {
return 'DateTime.fromMillisecondsSinceEpoch($argument)';
} else if (type.startsWith('\$')) {
return '$type.fromFirebaseMap($argument)';
return '$type.fromMap($argument)';
} else if (type.startsWith('List')) {
final buffer = new StringBuffer();
buffer.write('(m[\'$name\'] as Map).values');
Expand All @@ -216,7 +218,7 @@ class FileGenerator {
}

final buffer = new StringBuffer();
buffer.writeln('${c.name}.fromFirebaseMap(Map m):');
buffer.writeln('${c.name}.fromMap(Map<String, dynamic> m):');

final params = c.props.map((p) {
var assignment =
Expand All @@ -233,12 +235,12 @@ class FileGenerator {
return buffer;
}

static StringBuffer _generateToRtdbMap(DataClass c) {
static StringBuffer _generateToMap(DataClass c) {
String _getConverter(String name, String type, bool isEnum) {
if (type.startsWith('DateTime')) {
return '$name.millisecondsSinceEpoch';
} else if (type.startsWith('\$')) {
return '$name.toFirebaseMap()';
return '$name.toMap()';
} else if (type.startsWith('List')) {
final buffer = new StringBuffer();
buffer.write('Map.fromIterable($name,');
Expand All @@ -248,8 +250,10 @@ class FileGenerator {
converter = converter.replaceFirst('$name.', 'm.');
final key = listType.startsWith('\$') ? 'm.id' : 'm.hashCode';
buffer.write('key: (m) => $key, value: (m) => $converter)');
return buffer.toString();
} else {
throw Exception('No type specified for List. Are you sure you imported necessary data models?');
}
return buffer.toString();
} else if (isEnum) {
return '$name.index';
} else {
Expand All @@ -258,7 +262,7 @@ class FileGenerator {
}

final buffer = new StringBuffer();
buffer.writeln('Map toFirebaseMap() => {');
buffer.writeln('Map<String, dynamic> toMap() => {');

final params = c.props.map((p) {
var assignment = _getConverter(p.name, p.type, p.isEnum);
Expand All @@ -273,4 +277,16 @@ class FileGenerator {
buffer.writeln('};');
return buffer;
}

static StringBuffer _generateFromJson(DataClass c) {
final buffer = new StringBuffer();
buffer.writeln('factory ${c.name}.fromJson(String json) => ${c.name}.fromMap(jsonDecode(json));');
return buffer;
}

static StringBuffer _generateToJson(DataClass c) {
final buffer = new StringBuffer();
buffer.writeln('String toJson() => jsonEncode(toMap());');
return buffer;
}
}
4 changes: 2 additions & 2 deletions auto_data_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ name: auto_data_generator
description: >
Automatically generate simple data class files for Dart.
This is the dev dependency.
version: 0.0.2
version: 0.0.3
author: Paul DeMarco <[email protected]>
homepage: https://www.github.com/pauldemarco/auto_data

environment:
sdk: ">=2.1.0 <3.0.0"

dependencies:
auto_data: ^0.0.2
auto_data: ^0.0.3
build: ^1.0.0
source_gen: ^0.9.4+1

Expand Down
1 change: 1 addition & 0 deletions example/lib/bar.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:meta/meta.dart';
import 'package:auto_data/auto_data.dart';
import 'dart:convert';
import 'foo.dart';

part 'bar.g.dart';
Expand Down
63 changes: 0 additions & 63 deletions example/lib/bar.g.dart

This file was deleted.

1 change: 1 addition & 0 deletions example/lib/foo.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:meta/meta.dart';
import 'package:auto_data/auto_data.dart';
import 'package:collection/collection.dart';
import 'dart:convert';

part 'foo.g.dart';

Expand Down
69 changes: 0 additions & 69 deletions example/lib/foo.g.dart

This file was deleted.

1 change: 1 addition & 0 deletions example/lib/person.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:meta/meta.dart';
import 'package:auto_data/auto_data.dart';
import 'dart:convert';

part 'person.g.dart';

Expand Down
Loading

0 comments on commit 35ef6e5

Please sign in to comment.