new public static void GetObjectData(DObject /*!*/ instance, SerializationInfo /*!*/ info, StreamingContext strctx) { info.SetType(typeof(SPLDeserializer)); SerializationContext context = SerializationContext.CreateFromStreamingContext(strctx); object res = PhpVariable.Dereference(instance.InvokeMethod("serialize", null, context.ScriptContext)); if (res == null) { // serialize returned NULL -> this instance will deserialize as NULL info.AddValue(__PHP_Incomplete_Class.ClassNameFieldName, String.Empty); } else { string res_str = PhpVariable.AsString(res); if (res_str == null) { // serialize did not return NULL nor a string -> throw an exception Library.SPL.Exception.ThrowSplException( _ctx => new Library.SPL.Exception(_ctx, true), context.ScriptContext, string.Format(CoreResources.serialize_must_return_null_or_string, instance.TypeName), 0, null); } info.AddValue(SerializedDataFieldName, res_str); info.AddValue(__PHP_Incomplete_Class.ClassNameFieldName, instance.TypeName); } }
/// <include file='Doc/Common.xml' path='/docs/method[@name="serialization.ctor"]/*'/> protected Deserializer(SerializationInfo info, StreamingContext context) { this.serInfo = info; this.context = SerializationContext.CreateFromStreamingContext(context); string class_name_str = info.GetString(__PHP_Incomplete_Class.ClassNameFieldName); if (String.IsNullOrEmpty(class_name_str)) { // note that we must never return null from GetRealObject (formatters do not like it) return; } instance = Serialization.GetUninitializedInstance(class_name_str, this.context.ScriptContext); if (instance == null) { throw new SerializationException(CoreResources.GetString("class_instantiation_failed", class_name_str)); } }
public static void GetObjectData(DObject /*!*/ instance, SerializationInfo /*!*/ info, StreamingContext strctx) { info.SetType(typeof(Deserializer)); SerializationContext context = SerializationContext.CreateFromStreamingContext(strctx); bool sleep_called; PhpArray sleep_result; // try to get the caches __sleep result if (context.SleepResults.TryGetValue(instance, out sleep_result)) { if (Object.ReferenceEquals(sleep_result, SerializationContext.NoSleepResultSingleton)) { sleep_called = false; sleep_result = null; } else { sleep_called = true; } } else { sleep_result = instance.Sleep(context.ClassContext, context.ScriptContext, out sleep_called); context.SleepResults.Add(instance, (sleep_called ? sleep_result : SerializationContext.NoSleepResultSingleton)); } if (sleep_called && sleep_result == null) { // __sleep did not return an array -> this instance will deserialize as NULL info.AddValue(__PHP_Incomplete_Class.ClassNameFieldName, String.Empty); } else { // if we have a sleep result, serialize fields according to it, otherwise serialize all fields IEnumerable <KeyValuePair <string, object> > serializable_properties; object real_object = null; if (sleep_result == null) { serializable_properties = Serialization.EnumerateSerializableProperties( instance, true); // get PHP fields only // serialize CLR real object in the "CLR way" if (!(instance is PhpObject)) { real_object = instance.RealObject; } } else { serializable_properties = Serialization.EnumerateSerializableProperties( instance, sleep_result, context.ScriptContext); } bool type_name_serialized = false; bool real_object_serialized = false; foreach (KeyValuePair <string, object> pair in serializable_properties) { if (pair.Key == __PHP_Incomplete_Class.ClassNameFieldName) { type_name_serialized = true; } if (pair.Key == ClrRealObjectSerializationInfoKey) { // unwrap the possibly wrapped CLR real object info.AddValue(pair.Key, PhpVariable.Unwrap(pair.Value)); real_object_serialized = true; } else { PhpReference reference = pair.Value as PhpReference; info.AddValue(pair.Key, WrapPropertyValue(pair.Value)); } } // if the type name has not been serialized, do it now if (!type_name_serialized) { info.AddValue(__PHP_Incomplete_Class.ClassNameFieldName, instance.TypeName); } // if the real object has not been serialized, do it now if (!real_object_serialized) { info.AddValue(ClrRealObjectSerializationInfoKey, real_object); } } }