/// <summary> /// Stores the given value into the instance's stream; the serializer /// is inferred from TValue and format. /// </summary> /// <remarks>Needs to be public to be callable thru reflection in Silverlight</remarks> public static void AppendExtendValueTyped <TSource, TValue>( TSource instance, int tag, DataFormat format, TValue value) where TSource : class, IExtensible { Serializer <TSource> .CheckTagNotInUse(tag); Property <TValue, TValue> prop = PropertyFactory.CreatePassThru <TValue>(tag, ref format); IExtension extn = instance.GetExtensionObject(true); if (extn == null) { throw new InvalidOperationException("No extension object available; appended data would be lost."); } Stream stream = extn.BeginAppend(); try { SerializationContext ctx = new SerializationContext(stream, null); ctx.Push(instance); // for recursion detection prop.Serialize(value, ctx); ctx.Pop(instance); ctx.Flush(); extn.EndAppend(stream, true); } catch { extn.EndAppend(stream, false); throw; } }
internal static int Serialize(T instance, SerializationContext context) { // check for inheritance; if the instance is a subclass, then we // should serialize the sub-type first, allowing for more efficient // deserialization; note that we don't push the instance onto the // stack yet - we'll do that within each instance (otherwise deep // items could incorrectly count as cyclic). Type actualType = instance.GetType(); int total = 0, len; Callback(CallbackType.BeforeSerialization, instance); if (actualType != typeof(T)) { bool subclassFound = false; foreach (KeyValuePair <Type, Property <T, T> > subclass in subclasses) { if (subclass.Key.IsAssignableFrom(actualType)) { total += subclass.Value.Serialize(instance, context); subclassFound = true; break; } } if (!subclassFound) { throw new ProtoException("Unexpected type found during serialization; types must be included with ProtoIncludeAttribute; " + "found " + actualType.Name + " passed as " + typeof(T).Name); } } context.Push(instance); for (int i = 0; i < writeProps.Length; i++) { // note that this serialization includes the headers... total += writeProps[i].Serialize(instance, context); } IExtensible extensible = instance as IExtensible; IExtension extra = extensible == null ? null : extensible.GetExtensionObject(false); if (extra != null && (len = extra.GetLength()) > 0) { Stream extraStream = extra.BeginQuery(); try { context.WriteFrom(extraStream, len); total += len; } finally { extra.EndQuery(extraStream); } } context.Pop(instance); Callback(CallbackType.AfterSerialization, instance); return(total); }
internal static void Deserialize <TCreation>(ref T instance, SerializationContext context) where TCreation : class, T { uint prefix = 0; if (context == null) { throw new ArgumentNullException("context"); } #if !CF try { #endif if (instance != null) { Callback(CallbackType.BeforeDeserialization, instance); } context.Push(); int propCount = readProps.Length; //context.CheckSpace(); IExtensible extensible = instance as IExtensible; IExtension extn = null; Property <T> prop = propCount == 0 ? null : readProps[0]; int lastIndex = prop == null ? -1 : 0; uint lastPrefix = prop == null ? uint.MaxValue : prop.FieldPrefix; while (context.TryReadFieldPrefix(out prefix)) { // scan for the correct property bool foundTag = false; if (prefix == lastPrefix) { foundTag = true; } else if (prefix > lastPrefix) { for (int i = lastIndex + 1; i < propCount; i++) { if (readProps[i].FieldPrefix == prefix) { prop = readProps[i]; lastIndex = i; lastPrefix = prefix; foundTag = true; break; } if (readProps[i].FieldPrefix > prefix) { break; // too far } } } else { for (int i = lastIndex - 1; i >= 0; i--) { if (readProps[i].FieldPrefix == prefix) { prop = readProps[i]; lastIndex = i; lastPrefix = prefix; foundTag = true; break; } if (readProps[i].FieldPrefix < prefix) { break; // too far } } } if (!foundTag) { // check for subclass creation foreach (KeyValuePair <Type, Property <T, T> > subclass in subclasses) { // deserialize the nested data if (prefix == subclass.Value.FieldPrefix) { foundTag = true; instance = subclass.Value.DeserializeImpl(instance, context); break; } } if (foundTag) { continue; // nothing more to do for this... } } // not a sub-class, but *some* data there, so create an object if (instance == null) { instance = ObjectFactory <TCreation> .Create(); Callback(CallbackType.ObjectCreation, instance); extensible = instance as IExtensible; } if (foundTag) { // found it by seeking; deserialize and continue // ReSharper disable PossibleNullReferenceException try { prop.Deserialize(instance, context); } catch (UnexpectedDataException ex) { if (extensible != null) { if (extn == null) { extn = extensible.GetExtensionObject(true); } ex.Serialize(extn); } // DON'T re-throw; we've handled this } // ReSharper restore PossibleNullReferenceException continue; } WireType wireType; int fieldTag; Serializer.ParseFieldToken(prefix, out wireType, out fieldTag); if (wireType == WireType.EndGroup) { context.EndGroup(fieldTag); break; // this ends the entity, so stop the loop } // so we couldn't find it... if (extensible != null) { if (extn == null) { extn = extensible.GetExtensionObject(true); } Stream extraStream = extn.BeginAppend(); try { SerializationContext extraData = new SerializationContext(extraStream, null); // copy the data into the output stream // ReSharper disable PossibleNullReferenceException extraData.EncodeUInt32(prefix); // ReSharper restore PossibleNullReferenceException ProcessExtraData(context, fieldTag, wireType, extraData); extraData.Flush(); extn.EndAppend(extraStream, true); } catch { extn.EndAppend(extraStream, false); throw; } } else { // unexpected fields for an inextensible object; discard the data Serializer.SkipData(context, fieldTag, wireType); } } // final chance to create an instance - this only gets invoked for empty // messages (otherwise instance should already be non-null) if (instance == null) { instance = ObjectFactory <T> .Create(); Callback(CallbackType.ObjectCreation, instance); } context.Pop(); Callback(CallbackType.AfterDeserialization, instance); #if !CF } catch (Exception ex) { const string ErrorDataKey = "protoSource"; if (!ex.Data.Contains(ErrorDataKey)) { ex.Data.Add(ErrorDataKey, string.Format("tag={0}; wire-type={1}; offset={2}; depth={3}; type={4}", (int)(prefix >> 3), (WireType)(prefix & 7), context.Position, context.Depth, typeof(T).FullName)); } throw; } #endif }