Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(484)

Side by Side Diff: pkg/serialization/lib/src/serialization_rule.dart

Issue 14578008: Add a DateTimeRule to default serialization (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixes from review Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of serialization; 5 part of serialization;
6 6
7 // TODO(alanknight): We should have an example and tests for subclassing 7 // TODO(alanknight): We should have an example and tests for subclassing
8 // serialization rule rather than using the hard-coded ClosureToMap rule. And 8 // serialization rule rather than using the hard-coded ClosureToMap rule. And
9 // possibly an abstract superclass that's designed to be subclassed that way. 9 // possibly an abstract superclass that's designed to be subclassed that way.
10 /** 10 /**
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 /** A hard-coded rule for serializing Symbols. */ 485 /** A hard-coded rule for serializing Symbols. */
486 class SymbolRule extends CustomRule { 486 class SymbolRule extends CustomRule {
487 bool appliesTo(instance, _) => instance is Symbol; 487 bool appliesTo(instance, _) => instance is Symbol;
488 getState(instance) => [MirrorSystem.getName(instance)]; 488 getState(instance) => [MirrorSystem.getName(instance)];
489 create(state) => new Symbol(state[0]); 489 create(state) => new Symbol(state[0]);
490 void setState(symbol, state) {} 490 void setState(symbol, state) {}
491 int get dataLength => 1; 491 int get dataLength => 1;
492 bool get hasVariableLengthEntries => false; 492 bool get hasVariableLengthEntries => false;
493 } 493 }
494 494
495 /** A hard-coded rule for DateTime. */
496 class DateTimeRule extends CustomRule {
497 bool appliesTo(instance, _) => instance is DateTime;
498 List getState(DateTime date) => [date.millisecondsSinceEpoch, date.isUtc];
499 create(List state) =>
500 new DateTime.fromMillisecondsSinceEpoch(state[0], isUtc: state[1]);
501 void setState(date, state) {}
502 // Let the system know we don't have to store a length for these.
503 int get dataLength => 2;
504 bool get hasVariableLengthEntries => false;
505 }
506
495 /** Create a lazy list/map that will inflate its items on demand in [r]. */ 507 /** Create a lazy list/map that will inflate its items on demand in [r]. */
496 _lazy(l, Reader r) { 508 _lazy(l, Reader r) {
497 if (l is List) return new _LazyList(l, r); 509 if (l is List) return new _LazyList(l, r);
498 if (l is Map) return new _LazyMap(l, r); 510 if (l is Map) return new _LazyMap(l, r);
499 throw new SerializationException("Invalid type: must be Map or List - $l"); 511 throw new SerializationException("Invalid type: must be Map or List - $l");
500 } 512 }
501 513
502 /** 514 /**
503 * This provides an implementation of Map that wraps a list which may 515 * This provides an implementation of Map that wraps a list which may
504 * contain references to (potentially) non-inflated objects. If these 516 * contain references to (potentially) non-inflated objects. If these
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 operator [](x) => _reader.inflateReference(_raw[x]); 566 operator [](x) => _reader.inflateReference(_raw[x]);
555 567
556 int get length => _raw.length; 568 int get length => _raw.length;
557 bool get isEmpty => _raw.isEmpty; 569 bool get isEmpty => _raw.isEmpty;
558 get first => _reader.inflateReference(_raw.first); 570 get first => _reader.inflateReference(_raw.first);
559 get last => _reader.inflateReference(_raw.last); 571 get last => _reader.inflateReference(_raw.last);
560 572
561 // These operations, and other inherited methods that iterate over the whole 573 // These operations, and other inherited methods that iterate over the whole
562 // list will work, but may be expensive, and are probably 574 // list will work, but may be expensive, and are probably
563 // best avoided. 575 // best avoided.
564 List get _inflated => _raw.map(_reader.inflateReference); 576 Iterable get _inflated => _raw.map(_reader.inflateReference);
565 Iterator get iterator => _inflated.iterator; 577 Iterator get iterator => _inflated.iterator;
566 indexOf(x, [pos = 0]) => _inflated.toList().indexOf(x); 578 indexOf(x, [pos = 0]) => _inflated.toList().indexOf(x);
567 lastIndexOf(x, [pos]) => _inflated.toList().lastIndexOf(x); 579 lastIndexOf(x, [pos]) => _inflated.toList().lastIndexOf(x);
568 sublist(start, [end]) => _inflated.sublist(start, end); 580 sublist(start, [end]) => _inflated.sublist(start, end);
569 581
570 Map<int, dynamic> asMap() => _inflated.asMap(); 582 Map<int, dynamic> asMap() => _inflated.asMap();
571 583
572 // These operations are all invalid 584 // These operations are all invalid
573 _throw() { 585 _throw() {
574 throw new UnsupportedError("Not modifiable"); 586 throw new UnsupportedError("Not modifiable");
(...skipping 14 matching lines...) Expand all
589 removeWhere(x) => _throw(); 601 removeWhere(x) => _throw();
590 retainWhere(x) => _throw(); 602 retainWhere(x) => _throw();
591 replaceRange(x, y, z) => _throw(); 603 replaceRange(x, y, z) => _throw();
592 getRange(x, y) => _throw(); 604 getRange(x, y) => _throw();
593 setRange(x, y, z, [a = 0]) => _throw(); 605 setRange(x, y, z, [a = 0]) => _throw();
594 setAll(x, y) => _throw(); 606 setAll(x, y) => _throw();
595 removeRange(x, y) => _throw(); 607 removeRange(x, y) => _throw();
596 get reversed => _throw(); 608 get reversed => _throw();
597 void set length(x) => _throw(); 609 void set length(x) => _throw();
598 } 610 }
OLDNEW
« no previous file with comments | « pkg/serialization/lib/serialization.dart ('k') | pkg/serialization/test/serialization_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698