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

Unified Diff: observatory_pub_packages/polymer/deserialize.dart

Issue 816693004: Add observatory_pub_packages snapshot to third_party (Closed) Base URL: http://dart.googlecode.com/svn/third_party/
Patch Set: Created 6 years 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: observatory_pub_packages/polymer/deserialize.dart
===================================================================
--- observatory_pub_packages/polymer/deserialize.dart (revision 0)
+++ observatory_pub_packages/polymer/deserialize.dart (working copy)
@@ -0,0 +1,43 @@
+// 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;
+
+final _typeHandlers = {
+ String: (x, _) => x,
+ Null: (x, _) => x,
+ DateTime: (x, def) {
+ // 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 def;
+ }
+ },
+ bool: (x, _) => x != 'false',
+ int: (x, def) => int.parse(x, onError: (_) => def),
+ double: (x, def) => double.parse(x, (_) => def),
+};
+
+/// Convert representation of [value] based on [type] and [currentValue].
+Object deserializeValue(String value, Object currentValue, Type type) {
+ var handler = _typeHandlers[type];
+ if (handler != null) return handler(value, currentValue);
+
+ 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;
+ }
+}
« no previous file with comments | « observatory_pub_packages/polymer/deploy.dart ('k') | observatory_pub_packages/polymer/html_element_names.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698