Example #1
0
        /// <summary>
        /// Serializes the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="context">The context.</param>
        protected virtual void Serialize(object model, ISerializationContext <TSerializationContext> context)
        {
            Argument.IsNotNull("model", model);
            Argument.IsNotNull("context", context);

            var serializerModifiers = SerializationManager.GetSerializerModifiers(context.ModelType);

            Log.Debug("Using '{0}' serializer modifiers to deserialize type '{1}'", serializerModifiers.Length,
                      context.ModelTypeName);

            var serializingEventArgs = new SerializationEventArgs(context);

            Serializing.SafeInvoke(this, serializingEventArgs);

            foreach (var serializerModifier in serializerModifiers)
            {
                serializerModifier.OnSerializing(context, model);
            }

            BeforeSerialization(context);

            var members = GetSerializableMembers(context, model);

            SerializeMembers(context, members);

            AfterSerialization(context);

            foreach (var serializerModifier in serializerModifiers)
            {
                serializerModifier.OnSerialized(context, model);
            }

            Serialized.SafeInvoke(this, serializingEventArgs);
        }
        /// <summary>
        /// Serializes the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="context">The context.</param>
        public virtual void Serialize(ModelBase model, TSerializationContext context)
        {
            Argument.IsNotNull("model", model);
            Argument.IsNotNull("context", context);

            using (var finalContext = GetContext(model, context, SerializationContextMode.Serialization))
            {
                var serializerModifiers = SerializationManager.GetSerializerModifiers(finalContext.ModelType);

                Log.Debug("Using '{0}' serializer modifiers to deserialize type '{1}'", serializerModifiers.Length, finalContext.ModelType.GetSafeFullName());

                var serializingEventArgs = new SerializationEventArgs(finalContext);

                Serializing.SafeInvoke(this, serializingEventArgs);

                foreach (var serializerModifier in serializerModifiers)
                {
                    serializerModifier.OnSerializing(finalContext, model);
                }

                BeforeSerialization(finalContext);

                var members = GetSerializableMembers(model);
                SerializeMembers(finalContext, members);

                AfterSerialization(finalContext);

                foreach (var serializerModifier in serializerModifiers)
                {
                    serializerModifier.OnSerialized(finalContext, model);
                }

                Serialized.SafeInvoke(this, serializingEventArgs);
            }
        }
Example #3
0
        /// <summary>
        /// Deserializes the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="serializationContext">The serialization context.</param>
        public virtual void Deserialize(ModelBase model, TSerializationContext serializationContext)
        {
            Argument.IsNotNull("model", model);
            Argument.IsNotNull("context", serializationContext);

            using (var finalContext = GetContext(model, serializationContext, SerializationContextMode.Deserialization))
            {
                bool?previousLeanAndMeanValue = null;
                if (!ModelBase.GlobalLeanAndMeanModel)
                {
                    previousLeanAndMeanValue = model.LeanAndMeanModel;
                    model.LeanAndMeanModel   = true;
                }

                var serializerModifiers = SerializationManager.GetSerializerModifiers(finalContext.ModelType);

                Log.Debug("Using '{0}' serializer modifiers to deserialize type '{1}'", serializerModifiers.Length, finalContext.ModelType.GetSafeFullName());

                var serializingEventArgs = new SerializationEventArgs(finalContext);

                Deserializing.SafeInvoke(this, serializingEventArgs);

                foreach (var serializerModifier in serializerModifiers)
                {
                    serializerModifier.OnDeserializing(finalContext, model);
                }

                BeforeDeserialization(finalContext);

                DeserializeMembers(finalContext);

                AfterDeserialization(finalContext);

                foreach (var serializerModifier in serializerModifiers)
                {
                    serializerModifier.OnDeserialized(finalContext, model);
                }

                Deserialized.SafeInvoke(this, serializingEventArgs);

                model.FinishDeserialization();

                if (previousLeanAndMeanValue.HasValue)
                {
                    model.LeanAndMeanModel = previousLeanAndMeanValue.Value;
                }
            }
        }
Example #4
0
        /// <summary>
        /// Deserializes the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="context">The serialization context.</param>
        protected virtual object Deserialize(object model, ISerializationContext <TSerializationContextInfo> context)
        {
            Argument.IsNotNull("model", model);
            Argument.IsNotNull("context", context);

            var serializerModifiers = SerializationManager.GetSerializerModifiers(context.ModelType);

            Log.Debug("Using '{0}' serializer modifiers to deserialize type '{1}'", serializerModifiers.Length, context.ModelTypeName);

            var serializingEventArgs = new SerializationEventArgs(context);

            Deserializing?.Invoke(this, serializingEventArgs);

            foreach (var serializerModifier in serializerModifiers)
            {
                serializerModifier.OnDeserializing(context, model);
            }

            BeforeDeserialization(context);

            var deserializedMemberValues = DeserializeMembers(context);

            PopulateModel(context, deserializedMemberValues);

            // Always use the deserialized model (might be a value type)
            model = context.Model;

            AfterDeserialization(context);

            foreach (var serializerModifier in serializerModifiers)
            {
                serializerModifier.OnDeserialized(context, model);
            }

            Deserialized?.Invoke(this, serializingEventArgs);

            return(model);
        }