|
| 1 | +// Copyright (c) 2015, Google Inc. Please see the AUTHORS file for details. |
| 2 | +// All rights reserved. Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +library built_json; |
| 6 | + |
| 7 | +import 'src/bool_serializer.dart'; |
| 8 | +import 'src/built_list_serializer.dart'; |
| 9 | +import 'src/built_map_serializer.dart'; |
| 10 | +import 'src/built_set_serializer.dart'; |
| 11 | +import 'src/double_serializer.dart'; |
| 12 | +import 'src/int_serializer.dart'; |
| 13 | +import 'src/string_serializer.dart'; |
| 14 | + |
| 15 | +/// Serializes a single class. |
| 16 | +/// |
| 17 | +/// See <https://github.com/google/built_json.dart/tree/master/example> |
| 18 | +abstract class BuiltJsonSerializer<T> { |
| 19 | + Type get type; |
| 20 | + String get typeName; |
| 21 | + |
| 22 | + Object serialize(BuiltJsonSerializers builtJsonSerializers, T object, |
| 23 | + {String expectedType}); |
| 24 | + T deserialize(BuiltJsonSerializers builtJsonSerializers, Object object, |
| 25 | + {String expectedType}); |
| 26 | +} |
| 27 | + |
| 28 | +/// Serializes all transitive dependencies of a class. |
| 29 | +/// |
| 30 | +/// See <https://github.com/google/built_json.dart/tree/master/example> |
| 31 | +// TODO(davidmorgan): make immutable. |
| 32 | +class BuiltJsonSerializers { |
| 33 | + Map<String, String> _deobfuscatedNames = <String, String>{}; |
| 34 | + Map<String, BuiltJsonSerializer> _serializersByName = |
| 35 | + <String, BuiltJsonSerializer>{}; |
| 36 | + |
| 37 | + BuiltJsonSerializers() { |
| 38 | + add(new BoolSerializer()); |
| 39 | + add(new DoubleSerializer()); |
| 40 | + add(new IntSerializer()); |
| 41 | + add(new StringSerializer()); |
| 42 | + |
| 43 | + add(new BuiltListSerializer()); |
| 44 | + add(new BuiltMapSerializer()); |
| 45 | + add(new BuiltSetSerializer()); |
| 46 | + } |
| 47 | + |
| 48 | + void addAll(BuiltJsonSerializers builtJsonSerializers) { |
| 49 | + for (final serializer in builtJsonSerializers._serializersByName.values) { |
| 50 | + add(serializer); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + void add(BuiltJsonSerializer builtJsonSerializer) { |
| 55 | + _deobfuscatedNames[_getName(builtJsonSerializer.type)] = |
| 56 | + builtJsonSerializer.typeName; |
| 57 | + _serializersByName[builtJsonSerializer.typeName] = builtJsonSerializer; |
| 58 | + } |
| 59 | + |
| 60 | + Object serialize(Object object, {String expectedType}) { |
| 61 | + final rawName = _deobfuscatedNames[_getName(object.runtimeType)]; |
| 62 | + if (rawName == null) throw new StateError( |
| 63 | + "No serializer for '${object.runtimeType}'."); |
| 64 | + |
| 65 | + var genericType = _getGenericName(object.runtimeType); |
| 66 | + |
| 67 | + // TODO(davidmorgan): handle this generically. |
| 68 | + if (genericType == 'BuiltList<int>') { |
| 69 | + genericType = 'List<int>'; |
| 70 | + } |
| 71 | + if (genericType == 'BuiltSet<int>') { |
| 72 | + genericType = 'Set<int>'; |
| 73 | + } |
| 74 | + |
| 75 | + final genericName = |
| 76 | + genericType == null ? rawName : '$rawName<$genericType>'; |
| 77 | + |
| 78 | + if (genericName == expectedType) { |
| 79 | + return _serializersByName[rawName] |
| 80 | + .serialize(this, object, expectedType: genericType); |
| 81 | + } else { |
| 82 | + return <String, Object>{ |
| 83 | + genericName: _serializersByName[rawName] |
| 84 | + .serialize(this, object, expectedType: genericType) |
| 85 | + }; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + Object deserialize(Object object, {String expectedType}) { |
| 90 | + if (object is Map) { |
| 91 | + if (object.keys.length > 1) { |
| 92 | + // Must be expectedType. |
| 93 | + // TODO(davidmorgan): distinguish in the one field case. |
| 94 | + if (expectedType == null) { |
| 95 | + throw new StateError('Need an expected type here.'); |
| 96 | + } |
| 97 | + final typeName = _makeRaw(expectedType); |
| 98 | + final genericName = _getGeneric(expectedType); |
| 99 | + return _serializersByName[typeName] |
| 100 | + .deserialize(this, object, expectedType: genericName); |
| 101 | + } else { |
| 102 | + final typeName = _makeRaw(object.keys.single); |
| 103 | + final genericName = _getGeneric(object.keys.single); |
| 104 | + return _serializersByName[typeName] |
| 105 | + .deserialize(this, object.values.single, expectedType: genericName); |
| 106 | + } |
| 107 | + } else { |
| 108 | + final serializer = _serializersByName[_makeRaw(expectedType)]; |
| 109 | + if (serializer == null) { |
| 110 | + throw new StateError('No serializer for $expectedType'); |
| 111 | + } |
| 112 | + return serializer.deserialize(this, object, |
| 113 | + expectedType: _getGeneric(expectedType)); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + String _getName(Type type) => _makeRaw(type.toString()); |
| 118 | + |
| 119 | + String _makeRaw(String name) { |
| 120 | + final genericsStart = name.indexOf('<'); |
| 121 | + return genericsStart == -1 ? name : name.substring(0, genericsStart); |
| 122 | + } |
| 123 | + |
| 124 | + String _getGenericName(Type type) => _getGeneric(type.toString()); |
| 125 | + |
| 126 | + String _getGeneric(String name) { |
| 127 | + final genericsStart = name.indexOf('<'); |
| 128 | + return genericsStart == -1 |
| 129 | + ? null |
| 130 | + : name.substring(genericsStart + 1, name.length - 1); |
| 131 | + } |
| 132 | +} |
0 commit comments