/// <summary> /// Creates a new instance from a protocol-buffer stream /// </summary> /// <param name="type">The type to be created.</param> /// <param name="source">The binary stream to apply to the new instance (cannot be null).</param> /// <returns>A new, initialized instance.</returns> public static object Deserialize(System.Type type, Stream source) { if (typeof(IParseable).IsAssignableFrom(type)) { ProtoReader reader = null; object obj = null; try { reader = ProtoReader.Create(source, RuntimeTypeModel.Default, null, -1); //if (value != null) reader.SetRootObject(value); //object obj = DeserializeCore(reader, type, value, autoCreate); obj = Activator.CreateInstance(type); IParseable objProxy = obj as IParseable; objProxy.Parse(reader); reader.CheckFullyConsumed(); return(obj); } finally { ProtoReader.Recycle(reader); } return(obj); } UnityEngine.Debug.LogWarning("Old Form Load Table " + type.Name + "!!"); return(RuntimeTypeModel.Default.Deserialize(source, null, type)); }
public void ReadWriteAutomated_StreamReaderWriter(bool withState) { var model = RuntimeTypeModel.Create(); model.AutoCompile = false; using var ms = new MemoryStream(); var obj = new C { AVal = 123, BVal = 456, CVal = 789 }; if (withState) { using var writeState = ProtoWriter.State.Create(ms, model); writeState.SerializeRoot(obj); Assert.Equal(0, writeState.Depth); writeState.Close(); } else { #pragma warning disable CS0618 using var writer = ProtoWriter.Create(ms, model); model.Serialize(writer, obj); #pragma warning restore CS0618 Assert.Equal(0, writer.Depth); } var hex = BitConverter.ToString(ms.GetBuffer(), 0, (int)ms.Length); Assert.Equal("22-08-2A-03-18-95-06-10-C8-03-08-7B", hex); // 22 = field 4, type String // 08 = length 8 // 2A = field 5, type String // 03 = length 3 // 18 = field 3, type Variant // 95-06 = 789 (raw) or -395 (zigzag) // 10 = field 2, type Variant // C8-03 = 456(raw) or 228(zigzag) // 08 = field 1, type Variant // 7B = 123(raw) or - 62(zigzag) ms.Position = 0; A raw; if (withState) { using var readState = ProtoReader.State.Create(ms, model); raw = readState.DeserializeRoot <A>(null); } else { #pragma warning disable CS0618 using var reader = ProtoReader.Create(ms, model); #pragma warning restore CS0618 raw = reader.DefaultState().DeserializeRoot <A>(null); } var clone = Assert.IsType <C>(raw); Assert.NotSame(obj, clone); Assert.Equal(123, clone.AVal); Assert.Equal(456, clone.BVal); Assert.Equal(789, clone.CVal); }
/// <summary> /// Creates a new reader against a stream /// </summary> /// <param name="source">The source stream</param> /// <param name="model">The model to use for serialization; this can be null, but this will impair the ability to deserialize sub-objects</param> /// <param name="userState">Additional context about this serialization operation</param> /// <param name="length">The number of bytes to read, or -1 to read until the end of the stream</param> public static State Create(Stream source, TypeModel model, object userState = null, long length = TO_EOF) { #if PREFER_SPANS if (TryConsumeSegmentRespectingPosition(source, out var segment, length)) { return(Create(new System.Buffers.ReadOnlySequence <byte>( segment.Array, segment.Offset, segment.Count), model, userState)); } #endif var reader = ProtoReader.Create(source, model, userState, length); return(new State(reader)); }
internal static IEnumerable GetExtendedValues(TypeModel model, Type type, IExtensible instance, int tag, DataFormat format, bool singleton, bool allowDefinedTag) { if (instance == null) { throw new ArgumentNullException("instance"); } if (tag <= 0) { throw new ArgumentOutOfRangeException("tag"); } IExtension extensionObject = instance.GetExtensionObject(false); if (extensionObject != null) { Stream stream = extensionObject.BeginQuery(); object obj = null; ProtoReader protoReader = null; try { SerializationContext serializationContext = new SerializationContext(); protoReader = ProtoReader.Create(stream, model, serializationContext, -1); while (model.TryDeserializeAuxiliaryType(protoReader, format, tag, type, ref obj, true, false, false, false) && obj != null) { if (singleton) { continue; } yield return(obj); obj = null; } if (!singleton || obj == null) { goto Label0; } yield return(obj); } finally { ProtoReader.Recycle(protoReader); extensionObject.EndQuery(stream); } } Label0: yield break; }
/// <summary> /// All this does is call GetExtendedValuesTyped with the correct type for "instance"; /// this ensures that we don't get issues with subclasses declaring conflicting types - /// the caller must respect the fields defined for the type they pass in. /// </summary> internal static IEnumerable GetExtendedValues(TypeModel model, Type type, IExtensible instance, int tag, DataFormat format, bool singleton, bool allowDefinedTag) { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } if (tag <= 0) { throw new ArgumentOutOfRangeException(nameof(tag)); } IExtension extn = instance.GetExtensionObject(false); if (extn == null) { yield break; } Stream stream = extn.BeginQuery(); object value = null; ProtoReader reader = null; try { SerializationContext ctx = new SerializationContext(); reader = ProtoReader.Create(stream, model, ctx, ProtoReader.TO_EOF); while (model.TryDeserializeAuxiliaryType(reader, format, tag, type, ref value, true, false, false, false, null) && value != null) { if (!singleton) { yield return(value); value = null; // fresh item each time } } if (singleton && value != null) { yield return(value); } } finally { ProtoReader.Recycle(reader); extn.EndQuery(stream); } }
internal static IEnumerable GetExtendedValues(TypeModel model, Type type, IExtensible instance, int tag, DataFormat format, bool singleton, bool allowDefinedTag) { if (instance == null) { throw new ArgumentNullException("instance"); } if (tag <= 0) { throw new ArgumentOutOfRangeException("tag"); } IExtension extn = instance.GetExtensionObject(createIfMissing: false); if (extn != null) { Stream stream = extn.BeginQuery(); object value = null; ProtoReader reader = null; try { SerializationContext context = new SerializationContext(); reader = ProtoReader.Create(stream, model, context, -1); while (model.TryDeserializeAuxiliaryType(reader, format, tag, type, ref value, skipOtherFields: true, asListItem: false, autoCreate: false, insideList: false) && value != null) { if (!singleton) { yield return(value); value = null; } } if (singleton && value != null) { yield return(value); } } finally { ProtoReader.Recycle(reader); extn.EndQuery(stream); } } }
public void ReadWriteManual_StreamReaderWriter(bool withState) { using var ms = new MemoryStream(); var obj = new C { AVal = 123, BVal = 456, CVal = 789 }; using (var writeState = ProtoWriter.State.Create(ms, null)) { writeState.SerializeRoot(obj, ModelSerializer.Default); Assert.Equal(0, writeState.Depth); writeState.Close(); } Assert.Equal(12, ms.Length); var hex = BitConverter.ToString(ms.GetBuffer(), 0, (int)ms.Length); Assert.Equal("22-08-2A-03-18-95-06-10-C8-03-08-7B", hex); ms.Position = 0; A raw; if (withState) { using var readState = ProtoReader.State.Create(ms, null); raw = readState.DeserializeRoot <A>(serializer: ModelSerializer.Default); } else { #pragma warning disable CS0618 using var reader = ProtoReader.Create(ms, null); #pragma warning restore CS0618 raw = reader.DefaultState().DeserializeRoot <A>(serializer: ModelSerializer.Default); } var clone = Assert.IsType <C>(raw); Assert.NotSame(obj, clone); Assert.Equal(123, clone.AVal); Assert.Equal(456, clone.BVal); Assert.Equal(789, clone.CVal); }
public static object ParseEx(System.Type type, Stream source) { ProtoReader reader = null; object obj = null; try { reader = ProtoReader.Create(source, RuntimeTypeModel.Default, null, -1); //if (value != null) reader.SetRootObject(value); //object obj = DeserializeCore(reader, type, value, autoCreate); obj = Activator.CreateInstance(type); IParseable objProxy = obj as IParseable; objProxy.Parse(reader); reader.CheckFullyConsumed(); return(obj); } finally { ProtoReader.Recycle(reader); } return(obj); }
/// <summary> /// All this does is call GetExtendedValuesTyped with the correct type for "instance"; /// this ensures that we don't get issues with subclasses declaring conflicting types - /// the caller must respect the fields defined for the type they pass in. /// </summary> internal static IEnumerable GetExtendedValues(TypeModel model, Type type, IExtensible instance, int tag, DataFormat format, bool singleton, bool allowDefinedTag) { #if FEAT_IKVM throw new NotSupportedException(); #else if (instance == null) { throw new ArgumentNullException("instance"); } if (tag <= 0) { throw new ArgumentOutOfRangeException("tag"); } IExtension extn = instance.GetExtensionObject(false); if (extn == null) { #if FX11 return(new object[0]); #else yield break; #endif } #if FX11 BasicList result = new BasicList(); #endif Stream stream = extn.BeginQuery(); object value = null; ProtoReader reader = null; try { SerializationContext ctx = new SerializationContext(); reader = ProtoReader.Create(stream, model, ctx, ProtoReader.TO_EOF); while (model.TryDeserializeAuxiliaryType(reader, format, tag, type, ref value, true, false, false, false) && value != null) { if (!singleton) { #if FX11 result.Add(value); #else yield return(value); #endif value = null; // fresh item each time } } if (singleton && value != null) { #if FX11 result.Add(value); #else yield return(value); #endif } #if FX11 object[] resultArr = new object[result.Count]; result.CopyTo(resultArr, 0); return(resultArr); #endif } finally { ProtoReader.Recycle(reader); extn.EndQuery(stream); } #endif }