Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SerializationContext{TContext}" /> class.
        /// </summary>
        /// <param name="model">The model, can be <c>null</c> for value types.</param>
        /// <param name="modelType">Type of the model.</param>
        /// <param name="context">The context.</param>
        /// <param name="contextMode">The context mode.</param>
        /// <param name="configuration">The configuration.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="modelType" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="context" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="configuration" /> is <c>null</c>.</exception>
        public SerializationContext(object model, Type modelType, TContext context,
                                    SerializationContextMode contextMode, ISerializationConfiguration configuration = null)
        {
            Argument.IsNotNull("modelType", modelType);
            Argument.IsNotNull("context", context);
            Argument.IsNotNull("configuration", configuration);

            Model         = model;
            ModelType     = modelType;
            ModelTypeName = modelType.GetSafeFullName(false);
            Context       = context;
            ContextMode   = contextMode;
            TypeStack     = new Stack <Type>();
            Configuration = configuration;

            var scopeName = SerializationContextHelper.GetSerializationReferenceManagerScopeName();

            _typeStackScopeManager = ScopeManager <Stack <Type> > .GetScopeManager(scopeName, () => new Stack <Type>());

            TypeStack = _typeStackScopeManager.ScopeObject;

            _referenceManagerScopeManager = ScopeManager <ReferenceManager> .GetScopeManager(scopeName);

            ReferenceManager = _referenceManagerScopeManager.ScopeObject;

            _serializableToken = CreateSerializableToken();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the current serialization scope.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <returns></returns>
        protected virtual ScopeManager <SerializationScope> GetCurrentSerializationScopeManager(ISerializationConfiguration configuration)
        {
            var scopeName    = SerializationContextHelper.GetSerializationReferenceManagerScopeName();
            var scopeManager = ScopeManager <SerializationScope> .GetScopeManager(scopeName, () => new SerializationScope(this, configuration ?? DefaultSerializationConfiguration));

            return(scopeManager);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Serializes the specified model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="context">The context.</param>
        public virtual void Serialize(object model, TSerializationContext context)
        {
            Argument.IsNotNull("model", model);
            Argument.IsNotNull("context", context);

            var scopeName = SerializationContextHelper.GetSerializationReferenceManagerScopeName();

            using (ScopeManager <ISerializer> .GetScopeManager(scopeName, () => this))
            {
                using (var finalContext = GetContext(model, model.GetType(), context, SerializationContextMode.Serialization))
                {
                    Serialize(model, finalContext);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SerializationContext{TContext}" /> class.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="context">The context.</param>
        /// <param name="contextMode">The context mode.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="model" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="context" /> is <c>null</c>.</exception>
        public SerializationContext(ModelBase model, TContext context, SerializationContextMode contextMode)
        {
            Argument.IsNotNull("model", model);
            Argument.IsNotNull("context", context);

            Model       = model;
            ModelType   = model.GetType();
            Context     = context;
            ContextMode = contextMode;

            var scopeName = SerializationContextHelper.GetSerializationReferenceManagerScopeName();

            _referenceManagerScopeManager = ScopeManager <ReferenceManager> .GetScopeManager(scopeName);

            ReferenceManager = _referenceManagerScopeManager.ScopeObject;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Serializes the members.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="membersToSerialize">The members to serialize.</param>
        protected virtual void SerializeMembers(ISerializationContext <TSerializationContext> context, List <MemberValue> membersToSerialize)
        {
            ApiCop.UpdateRule <InitializationApiCopRule>("SerializerBase.WarmupAtStartup",
                                                         x => x.SetInitializationMode(InitializationMode.Lazy, GetType().GetSafeFullName()));

            var scopeName = SerializationContextHelper.GetSerializationReferenceManagerScopeName();

            using (ScopeManager <ISerializer> .GetScopeManager(scopeName, () => this))
            {
                var serializerModifiers = SerializationManager.GetSerializerModifiers(context.ModelType);

                foreach (var member in membersToSerialize)
                {
                    bool skipByModifiers = false;
                    foreach (var serializerModifier in serializerModifiers)
                    {
                        if (serializerModifier.ShouldIgnoreMember(context, context.Model, member))
                        {
                            skipByModifiers = true;
                            break;
                        }
                    }

                    if (skipByModifiers)
                    {
                        continue;
                    }

                    var memberSerializationEventArgs = new MemberSerializationEventArgs(context, member);

                    SerializingMember.SafeInvoke(this, memberSerializationEventArgs);

                    BeforeSerializeMember(context, member);

                    foreach (var serializerModifier in serializerModifiers)
                    {
                        serializerModifier.SerializeMember(context, member);
                    }

                    SerializeMember(context, member);

                    AfterSerializeMember(context, member);

                    SerializedMember.SafeInvoke(this, memberSerializationEventArgs);
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SerializationContext{TContext}" /> class.
        /// </summary>
        /// <param name="model">The model, can be <c>null</c> for value types.</param>
        /// <param name="modelType">Type of the model.</param>
        /// <param name="context">The context.</param>
        /// <param name="contextMode">The context mode.</param>
        /// <param name="configuration">The configuration.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="modelType" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="modelType" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="modelType" /> is <c>null</c>.</exception>
        public SerializationContext(object model, Type modelType, TSerializationContextInfo context,
                                    SerializationContextMode contextMode, ISerializationConfiguration configuration = null)
        {
            Argument.IsNotNull("modelType", modelType);
            Argument.IsNotNull("context", context);
            Argument.IsNotNull("configuration", configuration);

            Model         = model;
            ModelType     = modelType;
            ModelTypeName = modelType.GetSafeFullName(false);
            Context       = context;
            ContextMode   = contextMode;
            TypeStack     = new Stack <Type>();
            Configuration = configuration;

            var scopeName = SerializationContextHelper.GetSerializationScopeName();

            _scopeManager = ScopeManager <SerializationContextScope <TSerializationContextInfo> > .GetScopeManager(scopeName, () => new SerializationContextScope <TSerializationContextInfo>());

            var contextScope = _scopeManager.ScopeObject;

            TypeStack        = contextScope.TypeStack;
            ReferenceManager = contextScope.ReferenceManager;
            Contexts         = contextScope.Contexts;

            var contexts = contextScope.Contexts;

            if (contexts.Count > 0)
            {
                Parent = contexts.Peek();
            }

            var serializationContextInfoParentSetter = context as ISerializationContextContainer;

            if (serializationContextInfoParentSetter != null)
            {
                serializationContextInfoParentSetter.SetSerializationContext(this);
            }

            Initialize();
        }