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

Side by Side Diff: pkg/polymer/lib/deserialize.dart

Issue 30183005: port polymer - 00e2982c78fcd396adaebff3118e94029a2b9fb0 (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 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
« no previous file with comments | « no previous file | pkg/polymer/lib/src/boot.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 library polymer.deserialize; 5 library polymer.deserialize;
6 6
7 import 'dart:convert' show JSON; 7 import 'dart:convert' show JSON;
8 import 'dart:mirrors' show TypeMirror; 8 import 'dart:mirrors' show TypeMirror;
9 9
10 final _typeHandlers = () { 10 final _typeHandlers = () {
(...skipping 12 matching lines...) Expand all
23 }; 23 };
24 m[#dart.core.bool] = (x, _) => x != 'false'; 24 m[#dart.core.bool] = (x, _) => x != 'false';
25 m[#dart.core.int] = 25 m[#dart.core.int] =
26 (x, def) => int.parse(x, onError: (_) => def); 26 (x, def) => int.parse(x, onError: (_) => def);
27 m[#dart.core.double] = 27 m[#dart.core.double] =
28 (x, def) => double.parse(x, (_) => def); 28 (x, def) => double.parse(x, (_) => def);
29 return m; 29 return m;
30 }(); 30 }();
31 31
32 /** 32 /**
33 * Convert representation of [value] based on type of [defaultValue]. 33 * Convert representation of [value] based on type of [currentValue].
34 */ 34 */
35 Object deserializeValue(String value, Object defaultValue, TypeMirror type) { 35 Object deserializeValue(String value, Object currentValue, TypeMirror type) {
36 var handler = _typeHandlers[type.qualifiedName]; 36 var handler = _typeHandlers[type.qualifiedName];
37 if (handler != null) return handler(value, defaultValue); 37 if (handler != null) return handler(value, currentValue);
38 38
39 try { 39 try {
40 // If the string is an object, we can parse is with the JSON library. 40 // If the string is an object, we can parse is with the JSON library.
41 // include convenience replace for single-quotes. If the author omits 41 // include convenience replace for single-quotes. If the author omits
42 // quotes altogether, parse will fail. 42 // quotes altogether, parse will fail.
43 return JSON.decode(value.replaceAll("'", '"')); 43 return JSON.decode(value.replaceAll("'", '"'));
44 44
45 // TODO(jmesserly): deserialized JSON is not assignable to most objects in 45 // TODO(jmesserly): deserialized JSON is not assignable to most objects in
46 // Dart. We should attempt to convert it appropriately. 46 // Dart. We should attempt to convert it appropriately.
47 } catch(e) { 47 } catch(e) {
48 // The object isn't valid JSON, return the raw value 48 // The object isn't valid JSON, return the raw value
49 return value; 49 return value;
50 } 50 }
51 } 51 }
OLDNEW
« no previous file with comments | « no previous file | pkg/polymer/lib/src/boot.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698