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(); } } }
/// 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); }
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(); } } }
///---------------------------------------------------------------------------------------------- //Cleanup cycle references. //This is done to ensure that a problem in one serialization does not transfer to others. public void PurgeTemporaryData() { _references.Clear(); _lazyReferenceWriter.Clear(); _collectors.Clear(); }