| OLD | NEW |
| 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; | |
| 9 | 8 |
| 10 final _typeHandlers = () { | 9 final _typeHandlers = { |
| 11 // TODO(jmesserly): switch to map and symbol literal form when supported. | 10 String: (x, _) => x, |
| 12 var m = new Map(); | 11 Null: (x, _) => x, |
| 13 m[#dart.core.String] = (x, _) => x; | 12 DateTime: (x, def) { |
| 14 m[#dart.core.Null] = (x, _) => x; | |
| 15 m[#dart.core.DateTime] = (x, def) { | |
| 16 // TODO(jmesserly): shouldn't need to try-catch here | 13 // TODO(jmesserly): shouldn't need to try-catch here |
| 17 // See: https://code.google.com/p/dart/issues/detail?id=1878 | 14 // See: https://code.google.com/p/dart/issues/detail?id=1878 |
| 18 try { | 15 try { |
| 19 return DateTime.parse(x); | 16 return DateTime.parse(x); |
| 20 } catch (e) { | 17 } catch (e) { |
| 21 return def; | 18 return def; |
| 22 } | 19 } |
| 23 }; | 20 }, |
| 24 m[#dart.core.bool] = (x, _) => x != 'false'; | 21 bool: (x, _) => x != 'false', |
| 25 m[#dart.core.int] = | 22 int: (x, def) => int.parse(x, onError: (_) => def), |
| 26 (x, def) => int.parse(x, onError: (_) => def); | 23 double: (x, def) => double.parse(x, (_) => def), |
| 27 m[#dart.core.double] = | 24 }; |
| 28 (x, def) => double.parse(x, (_) => def); | |
| 29 return m; | |
| 30 }(); | |
| 31 | 25 |
| 32 /** | 26 /// Convert representation of [value] based on [type] and [currentValue]. |
| 33 * Convert representation of [value] based on type of [currentValue]. | 27 Object deserializeValue(String value, Object currentValue, Type type) { |
| 34 */ | 28 var handler = _typeHandlers[type]; |
| 35 Object deserializeValue(String value, Object currentValue, TypeMirror type) { | |
| 36 var handler = _typeHandlers[type.qualifiedName]; | |
| 37 if (handler != null) return handler(value, currentValue); | 29 if (handler != null) return handler(value, currentValue); |
| 38 | 30 |
| 39 try { | 31 try { |
| 40 // If the string is an object, we can parse is with the JSON library. | 32 // 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 | 33 // include convenience replace for single-quotes. If the author omits |
| 42 // quotes altogether, parse will fail. | 34 // quotes altogether, parse will fail. |
| 43 return JSON.decode(value.replaceAll("'", '"')); | 35 return JSON.decode(value.replaceAll("'", '"')); |
| 44 | 36 |
| 45 // TODO(jmesserly): deserialized JSON is not assignable to most objects in | 37 // TODO(jmesserly): deserialized JSON is not assignable to most objects in |
| 46 // Dart. We should attempt to convert it appropriately. | 38 // Dart. We should attempt to convert it appropriately. |
| 47 } catch(e) { | 39 } catch(e) { |
| 48 // The object isn't valid JSON, return the raw value | 40 // The object isn't valid JSON, return the raw value |
| 49 return value; | 41 return value; |
| 50 } | 42 } |
| 51 } | 43 } |
| OLD | NEW |