Ejemplo n.º 1
0
        public static Bundle ToBundle(object root, out SerializationInfo info, CancellationToken cancellationToken = default(CancellationToken))
        {
            StaticCache.UpdateRegisteredTypes();

            var mapper = new Mapper {
                CancellationToken = cancellationToken
            };
            var bundle = new Bundle();

            info = new SerializationInfo();

            var sw = new Stopwatch();

            sw.Start();

            bundle.RootBoxId = mapper.GetBoxId(root);

            while (mapper.objectsToProcess.Any())
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(bundle);
                }
                var tuple       = mapper.objectsToProcess.Dequeue();
                var o           = tuple.Item1;
                var box         = tuple.Item2;
                var transformer = mapper.GetTransformer(mapper.GetTypeMetadata(box.TypeMetadataId).TransformerId);
                transformer.FillBox(box, o, mapper);
            }

            bundle.TransformerGuids.AddRange(mapper.transformers.GetValues().Select(x => x.Guid).Select(x => ByteString.CopyFrom(x.ToByteArray())));
            bundle.TypeGuids.AddRange(mapper.types.GetValues().Select(x => ByteString.CopyFrom(StaticCache.GetGuid(x).ToByteArray())));
            bundle.StorableTypeMetadata.AddRange(mapper.storableTypeLayouts.GetValues().Select(l => StorableTypeLayoutToMetadata(l, mapper)));
            bundle.ArrayMetadata.AddRange(mapper.arrayMetadata.GetValues());
            bundle.TypeMetadata.AddRange(mapper.typeMetadata.GetValues());
            bundle.Boxes.AddRange(mapper.boxId2Box.OrderBy(x => x.Key).Select(x => x.Value));
            bundle.Strings.AddRange(mapper.strings.GetValues());

            sw.Stop();

            info.Duration = sw.Elapsed;
            info.NumberOfSerializedObjects = mapper.object2BoxId.Keys.Count;
            info.SerializedTypes           = mapper.types.GetValues();

            return(bundle);
        }
Ejemplo n.º 2
0
        public static object ToObject(Bundle bundle, out SerializationInfo info, CancellationToken cancellationToken = default(CancellationToken))
        {
            StaticCache.UpdateRegisteredTypes();

            var mapper = new Mapper {
                CancellationToken = cancellationToken
            };

            info = new SerializationInfo();

            var sw = new Stopwatch();

            sw.Start();

            mapper.transformers = new Index <ITransformer>(bundle.TransformerGuids.Select(x => new Guid(x.ToByteArray())).Select(StaticCache.GetTransformer));

            var types            = new List <Type>();
            var unknownTypeGuids = new List <Guid>();

            for (int i = 0; i < bundle.TypeGuids.Count; i++)
            {
                var x    = bundle.TypeGuids[i];
                var guid = new Guid(x.ToByteArray());
                if (StaticCache.TryGetType(guid, out Type type))
                {
                    types.Add(type);
                }
                else
                {
                    unknownTypeGuids.Add(guid);
                    types.Add(null);
                }
            }

            mapper.types               = new Index <Type>(types);
            mapper.typeMetadata        = new Index <TypeMetadata>(bundle.TypeMetadata);
            mapper.boxId2Box           = bundle.Boxes.Select((b, i) => new { Box = b, Index = i }).ToDictionary(k => (uint)k.Index + 1, v => v.Box);
            mapper.strings             = new Index <string>(bundle.Strings);
            mapper.storableTypeLayouts = new Index <StorableTypeLayout>(bundle.StorableTypeMetadata.Select(l => StorableTypeMetadataToLayout(l, mapper)));
            mapper.arrayMetadata       = new Index <ArrayMetadata>(bundle.ArrayMetadata);

            var boxes = bundle.Boxes;

            for (int i = boxes.Count - 1; i >= 0; i--)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }
                mapper.GetObject((uint)i + 1);
            }

            for (int i = boxes.Count; i > 0; i--)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }
                var box = mapper.boxId2Box[(uint)i];
                var o   = mapper.boxId2Object[(uint)i];
                if (o == null)
                {
                    continue;
                }
                var transformer = mapper.GetTransformer(mapper.GetTypeMetadata(box.TypeMetadataId).TransformerId);
                transformer.FillFromBox(o, box, mapper);
            }

            var root = mapper.GetObject(bundle.RootBoxId);

            ExecuteAfterDeserializationHooks(mapper.boxId2Object.Values.GetEnumerator());

            sw.Stop();

            info.Duration = sw.Elapsed;
            info.NumberOfSerializedObjects = mapper.boxId2Object.Values.Count;
            info.SerializedTypes           = types;
            info.UnknownTypeGuids          = unknownTypeGuids;

            return(root);
        }