-
Notifications
You must be signed in to change notification settings - Fork 412
Support generating a custom map and using non-copying wrappers for serialization #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
||
// TODO(kevmoo): Add documentation | ||
abstract class JsonMapWrapper extends MapBase<String, dynamic> { | ||
/// This operation is not supported by an unmodifiable map. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, is there a mixin for this? Otherwise this has to keep up to date with any new apis that the sdk adds....
Also, I think this should throw on putIfAbsent
and addAll
as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dude! WIP!
MapBase handles everything but what's overloaded here – it's nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok looking at the docs for MapBase
this should be fine.
@@ -36,6 +36,8 @@ class JsonSerializableGenerator | |||
|
|||
final List<TypeHelper> _typeHelpers; | |||
|
|||
final bool _doNewThing = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to be a bit more descriptive here :)
buffer.writeln('}'); | ||
|
||
if (_doNewThing) { | ||
buffer.writeln(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: it is probably worth breaking this out into its own function
|
||
if (_doNewThing) { | ||
buffer.writeln(); | ||
// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: extra comment line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack
|
||
if (fieldsList.every((e) => _includeIfNull(e, classIncludeIfNull))) { | ||
// write simple `toJson` method that includes all keys... | ||
//_writeToJsonSimple(buffer, fields); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cruft?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup
} else { | ||
// At least one field should be excluded if null | ||
//_writeToJsonWithNullChecks(buffer, fields, includeIfNull); | ||
buffer.writeln('@override\nIterable<String> get keys sync* {'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth having the diverging code paths here? I would avoid it unless you have a compelling reason (performance?) to do this optimization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sync* is pretty expensive. Going to benchmark to find out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...in principle I agree
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also not use sync* and just build up a list or w/e, if you are concerned about the performance
var jsonKeys = fields.values.map(_safeNameAccess).join(', '); | ||
|
||
// TODO(kevmoo): maybe put this in a static field instead? | ||
// const lists have unfortunate overhead |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A static final field would be lazy initialized right? That might help startup performance.
But then I'm allocating a List on every call.
Again – will benchmark. :-)
…On Tue, Oct 3, 2017 at 12:54 PM, Jacob MacDonald ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In json_serializable/lib/src/json_serializable_generator.dart
<#48 (comment)>
:
> +
+ if (fieldsList.every((e) => _includeIfNull(e, classIncludeIfNull))) {
+ // write simple `toJson` method that includes all keys...
+ //_writeToJsonSimple(buffer, fields);
+
+ var jsonKeys = fields.values.map(_safeNameAccess).join(', ');
+
+ // TODO(kevmoo): maybe put this in a static field instead?
+ // const lists have unfortunate overhead
+ buffer.writeln(''' @OverRide
+ Iterable<String> get keys => const [${jsonKeys}];
+''');
+ } else {
+ // At least one field should be excluded if null
+ //_writeToJsonWithNullChecks(buffer, fields, includeIfNull);
+ ***@***.***\nIterable<String> get keys sync* {');
You could also not use sync* and just build up a list or w/e, if you are
concerned about the performance
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#48 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AABCipl_HNEEJFU4YH48TBQFTivwRspVks5sopENgaJpZM4PsZW3>
.
|
99ac290
to
2188446
Compare
@jakemac53 – this is ready for review. |
@@ -6,102 +6,84 @@ import 'package:test/test.dart'; | |||
|
|||
import 'package:json_serializable/src/utils.dart'; | |||
|
|||
import 'test_files/kitchen_sink.dart'; | |||
import 'test_files/kitchen_sink.dart' as nullable; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, this prefix is a bit inconsistent (not abbreviated). Maybe just opt for no prefix? Or come up with a prefix and add it to wrapped
as well.
No description provided.