Beispiel #1
0
            public override void Serialize(ref ParameterCollectionData obj, ArchiveMode mode, SerializationStream stream)
            {
                if (mode == ArchiveMode.Deserialize)
                {
                    // Should be null if it was
                    int count = stream.ReadInt32();
                    for (int i = 0; i < count; ++i)
                    {
                        ParameterKey key          = null;
                        object       value        = null;
                        bool         matchingType = false;

                        stream.SerializeExtended(ref key, mode);
                        stream.Serialize(ref matchingType);

                        var valueType = matchingType ? key.PropertyType : typeof(object);
                        if (reuseReferences)
                        {
                            MemberReuseSerializer.SerializeExtended(stream, valueType, ref value, mode);
                        }
                        else
                        {
                            MemberNonSealedSerializer.SerializeExtended(stream, valueType, ref value, mode);
                        }

                        obj.Add(key, value);
                    }
                }
                else if (mode == ArchiveMode.Serialize)
                {
                    stream.Write(obj.Count);
                    foreach (var item in obj)
                    {
                        var key = item.Key;

                        // When serializing convert the value type to the expecting type
                        // This should probably better done at the source (when creating/filling the ParameterCollectionData)
                        var value = item.Key.ConvertValue(item.Value);

                        stream.SerializeExtended(ref key, mode);
                        bool matchingType = value.GetType().GetTypeInfo().IsAssignableFrom(key.PropertyType.GetTypeInfo());
                        stream.Serialize(ref matchingType);

                        var valueType = matchingType ? key.PropertyType : typeof(object);

                        if (reuseReferences)
                        {
                            MemberReuseSerializer.SerializeExtended(stream, valueType, ref value, mode);
                        }
                        else
                        {
                            MemberNonSealedSerializer.SerializeExtended(stream, valueType, ref value, mode);
                        }
                    }
                }
            }
        public override void Serialize(ContentSerializerContext context, SerializationStream stream, ref TSource obj)
        {
            // Serialize object
            if (context.Mode == ArchiveMode.Deserialize)
            {
                AssetManager.AssetReference assetReference;

                // Recursively call deserialization of this stream as another type.
                // We use special DeserializeObjectRecursive instead of DeserializeObject to avoid reopening this stream.
                var dataObject = context.AssetManager.DeserializeObjectRecursive(null, out assetReference, context.Url, converter.DataType, AssetManagerLoaderSettings.IgnoreReferences, context, stream.NativeStream, SerializationType);

                object source = null;

                // Transfer Context information to the Converter
                ConverterContext converterContext = context.ConverterContext;
                if (converterContext == null)
                {
                    // First time: create context, and register current object
                    converterContext = new ConverterContext {
                        Tags = stream.Context.Tags
                    };
                    context.ConverterContext = converterContext;
                }

                // Pre-construct object (if available)
                converterContext.ConvertFromData(dataObject, ref source, ConvertFromDataFlags.Construct);

                // If object could be constructed, register it so that it can properly be referenced
                if (source != null)
                {
                    context.AssetManager.SetAssetObject(context.AssetReference, source);
                }

                // Actually convert object
                converterContext.ConvertFromData(dataObject, ref source, ConvertFromDataFlags.Convert);

                obj = (TSource)source;

                // Unload data object (not necessary anymore)
                context.AssetManager.Unload(dataObject);
            }
            else
            {
                // Transfer Context information to the Converter
                object dataObject = null;
                converter.ConvertToData(new ConverterContext {
                    Tags = stream.Context.Tags
                }, ref dataObject, obj);

                MemberNonSealedSerializer.SerializeExtended(stream, converter.DataType, ref dataObject, context.Mode);
            }
        }