Example #1
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.
                var converter = GetConverter(instance.GetType(), overrideConverterType);
                if (converter.RequestCycleSupport(instance.GetType()) == false)
                {
                    return(InternalSerialize_2_Inheritance(storageType, overrideConverterType, instance, out data));
                }

                // 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.
                if (_references.IsReference(instance))
                {
                    data = fsData.CreateDictionary();
                    _lazyReferenceWriter.WriteReference(_references.GetReferenceId(instance), data.AsDictionary);
                    return(fsResult.Success);
                }

                // 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();
                }
            }
        }
Example #2
0
        //...
        fsResult Internal_Serialize(Type storageType, object instance, out fsData data, Type overrideConverterType)
        {
            var instanceType          = instance.GetType();
            var instanceTypeConverter = GetConverter(instanceType, overrideConverterType);

            if (instanceTypeConverter == null)
            {
                data = new fsData();
                // return fsResult.Warn(string.Format("No converter for {0}", instanceType));
                return(fsResult.Success);
            }

            var needsCycleSupport = instanceType.RTIsDefined <fsSerializeAsReference>(true);

            if (needsCycleSupport)
            {
                // 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.
                if (_references.IsReference(instance))
                {
                    data = fsData.CreateDictionary();
                    _lazyReferenceWriter.WriteReference(_references.GetReferenceId(instance), data.AsDictionary);
                    return(fsResult.Success);
                }

                // 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);
            }

            //push collector
            TryPush(instance);

            // Serialize the instance with it's actual instance type, not storageType.
            var serializeResult = instanceTypeConverter.TrySerialize(instance, out data, instanceType);

            //pop collector
            TryPop(instance);

            if (serializeResult.Failed)
            {
                return(serializeResult);
            }

            // Do we need to add type information? If the field type and the instance type are different.
            if (storageType != instanceType && GetConverter(storageType, overrideConverterType).RequestInheritanceSupport(storageType))
            {
                EnsureDictionary(ref data);
                data.AsDictionary[KEY_INSTANCE_TYPE] = new fsData(instanceType.FullName);
            }

            if (needsCycleSupport)
            {
                _lazyReferenceWriter.WriteDefinition(_references.GetReferenceId(instance), data);
            }

            return(serializeResult);
        }
Example #3
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.
                //このタイプはサイクルサポートを必要としません。
                var converter = GetConverter(instance.GetType(), overrideConverterType);
                if (converter.RequestCycleSupport(instance.GetType()) == false)
                {
                    return(InternalSerialize_2_Inheritance(storageType, overrideConverterType, instance, out data));
                }

                // 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.
                // 注:intを文字列としてシリアル化して、doubleから/への変換で情報が失われないようにします。
                if (_references.IsReference(instance))
                {
                    data = fsData.CreateDictionary();
                    _lazyReferenceWriter.WriteReference(_references.GetReferenceId(instance), data.AsDictionary);
                    return(fsResult.Success);
                }

                // 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.
                // サイクルメタデータを作成したので、実際のオブジェクトをシリアル化できます。 InternalSerializeは継承を正しく処理します。
                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();
                }
            }
        }