Example #1
0
        /// Serialize the given value.
        /// StorageType: field type.
        /// OverideConverter: optional override converter.
        /// Instance: the object instance.
        /// Data: the serialized state.
        public fsResult TrySerialize(Type storageType, Type overrideConverterType, object instance, out fsData data)
        {
            var processors = GetProcessors(instance == null ? storageType : instance.GetType());

            Invoke_OnBeforeSerialize(processors, storageType, instance);

            // We always serialize null directly as null
            if (ReferenceEquals(instance, null))
            {
                data = new fsData();
                Invoke_OnAfterSerialize(processors, storageType, instance, ref data);
                return(fsResult.Success);
            }

            fsResult result;

            try {
                _references.Enter();
                result = Internal_Serialize(storageType, overrideConverterType, instance, out data);
            }

            finally {
                if (_references.Exit())
                {
                    _lazyReferenceWriter.Clear();
                }
            }

            Invoke_OnAfterSerialize(processors, storageType, instance, ref data);
            return(result);
        }
Example #2
0
        private fsResult InternalSerialize_1_ProcessCycles(Type storageType, Type overrideConverterType, object instance, out fsData data)
        {
            // We have an object definition to serialize.
            try {
                // Note that we enter the reference group at the beginning of serialization so that we support
                // references that are at equal serialization levels, not just nested serialization levels, within
                // the given subobject. A prime example is serialization a list of references.
                _references.Enter();

                // This type does not need cycle support.
                // 获得instance.GetType()对应的converter
                var converter = GetConverter(instance.GetType(), overrideConverterType);
                // 判断是否需要循环,这里是不循环的情况
                if (converter.RequestCycleSupport(instance.GetType()) == false)
                {
                    return(InternalSerialize_2_Inheritance(storageType, overrideConverterType, instance, out data));
                }

                // 当instance IsClass || IsInterface; 才会执行下面的

                // We've already serialized this object instance (or it is pending higher up on the call stack).
                // Just serialize a reference to it to escape the cycle.
                //
                // note: We serialize the int as a string to so that we don't lose any information
                //       in a conversion to/from double.

                // 使用了引用实例的情况,直接返回fsResult.Success
                if (_references.IsReference(instance))
                {
                    // 用Dict创建一个fsData,
                    data = fsData.CreateDictionary();
                    _lazyReferenceWriter.WriteReference(_references.GetReferenceId(instance), data.AsDictionary);
                    return(fsResult.Success);
                }

                // 简单理解为记录instance到_references中
                // Mark inside the object graph that we've serialized the instance. We do this *before*
                // serialization so that if we get back into this function recursively, it'll already
                // be marked and we can handle the cycle properly without going into an infinite loop.
                _references.MarkSerialized(instance);

                // We've created the cycle metadata, so we can now serialize the actual object.
                // InternalSerialize will handle inheritance correctly for us.
                var result = InternalSerialize_2_Inheritance(storageType, overrideConverterType, instance, out data);
                if (result.Failed)
                {
                    return(result);
                }

                _lazyReferenceWriter.WriteDefinition(_references.GetReferenceId(instance), data);

                return(result);
            }
            finally {
                if (_references.Exit())
                {
                    _lazyReferenceWriter.Clear();
                }
            }
        }