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

Unified Diff: pkg/polymer/lib/deserialize.dart

Issue 24149003: Port of github.com/polymer/polymer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: pkg/polymer/lib/deserialize.dart
diff --git a/pkg/polymer/lib/deserialize.dart b/pkg/polymer/lib/deserialize.dart
new file mode 100644
index 0000000000000000000000000000000000000000..7f72c2c83aba4eec8ba4abb4c797d14ef23f9c78
--- /dev/null
+++ b/pkg/polymer/lib/deserialize.dart
@@ -0,0 +1,51 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library polymer.deserialize;
+
+import 'dart:convert' show JSON;
+import 'dart:mirrors' show reflect, TypeMirror;
+
+final _typeHandlers = () {
+ // TODO(jmesserly): switch to map and symbol literal form when supported.
+ var m = new Map();
+ m[const Symbol('dart.core.String')] = (x, _) => x;
+ m[const Symbol('dart.core.Null')] = (x, _) => x;
+ m[const Symbol('dart.core.DateTime')] = (x, _) {
+ // TODO(jmesserly): shouldn't need to try-catch here
+ // See: https://code.google.com/p/dart/issues/detail?id=1878
+ try {
+ return DateTime.parse(x);
+ } catch (e) {
+ return new DateTime.now();
blois 2013/09/27 21:40:52 not the default value?
Jennifer Messerly 2013/09/30 17:44:37 Yeah, agreed. I filed https://code.google.com/p/da
+ }
+ };
+ m[const Symbol('dart.core.bool')] = (x, _) => x != 'false';
+ m[const Symbol('dart.core.int')] =
+ (x, def) => int.parse(x, onError: (_) => def);
+ m[const Symbol('dart.core.double')] =
+ (x, def) => double.parse(x, (_) => def);
+ return m;
+}();
+
+/**
+ * Convert representation of [value] based on type of [defaultValue].
+ */
+Object deserializeValue(String value, Object defaultValue, TypeMirror type) {
+ var handler = _typeHandlers[type.qualifiedName];
blois 2013/09/27 21:40:52 Is this extensible? What would it take for someone
Jennifer Messerly 2013/09/30 17:44:37 agreed, added to #13666
+ if (handler != null) return handler(value, defaultValue);
+
+ try {
+ // If the string is an object, we can parse is with the JSON library.
+ // include convenience replace for single-quotes. If the author omits
+ // quotes altogether, parse will fail.
+ return JSON.decode(value.replaceAll("'", '"'));
+
+ // TODO(jmesserly): deserialized JSON is not assignable to most objects in
+ // Dart. We should attempt to convert it appropriately.
+ } catch(e) {
+ // The object isn't valid JSON, return the raw value
+ return value;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698