public List <DatasourceRecord> Deserialize(Stream sourceStream) { using (var gz = new GZipStream(sourceStream, CompressionMode.Decompress, false)) { var list = new List <DatasourceRecord>(); _serializer.Deserialize(gz, list, typeof(List <DatasourceRecord>)); return(list); } }
/// <summary> /// Deserializes the message from bytes. /// </summary> /// <param name="data"></param> /// <returns></returns> public static BaseMessage Deserialize(byte[] data) { try { using (MemoryStream ms = new System.IO.MemoryStream(data)) { BaseMessage Base = (BaseMessage)TypeModel.Deserialize(ms, null, typeof(BaseMessage)); ms.Position = 0; BaseMessage message = (BaseMessage)TypeModel.Deserialize(ms, null, IDToMessage[Base.ID]); return(message); } } catch (IndexOutOfRangeException e) { Log.Debug(e, "Unknown message type!"); return(null); } }
public void Serialization_ValidNotification() { INotification example = new UserNotification { Email = "*****@*****.**", Name = "Test", }; using (var ms = new MemoryStream()) { _model.Serialize(ms, example); ms.Seek(0, SeekOrigin.Begin); var result = (INotification)_model.Deserialize(ms, null, typeof(INotification)); Assert.IsInstanceOf <UserNotification>(result); Assert.IsInstanceOf <EmailEnvironment>(result.Environment); Assert.AreEqual(example.Email, result.Email); Assert.AreEqual(example.Name, result.Name); } }
/// <summary> /// Deserializes the data on the provided stream and /// reconstitutes the graph of objects. /// </summary> /// <param name="stream">The stream that contains the data to deserialize.</param> /// <returns>The top object of the deserialized graph.</returns> public object Deserialize(Stream stream) { Ensure.NotNull(stream, "stream"); var type = GetType(stream); return(UseLengthPrefix ? _typeModel.DeserializeWithLengthPrefix(stream, null, type, PrefixStyle.Base128, 0) : _typeModel.Deserialize(stream, null, type)); }
public async Task <Message> Deserialize(TransportMessage transportMessage) { using (var source = new MemoryStream(transportMessage.Body)) { var headers = transportMessage.Headers.Clone(); var body = _runtimeTypeModel.Deserialize(source, null, GetMessageType(transportMessage.Headers)); return(new Message(headers, body)); } }
public static T CloneProtoAuto <T>(this T obj) { using (var ms = new MemoryStream()) { RegisterType(GetBaseType(typeof(T))); ProtobufModel.Serialize(ms, obj); ms.Position = 0; var clone = default(T); return((T)ProtobufModel.Deserialize(ms, clone, typeof(T))); } }
public MyObjectBuilder_Base ChangeType(MyObjectBuilderType type, string subtypeName) { using (MemoryStream ms = new MemoryStream()) { Serializer.Serialize(ms, this); ms.Position = 0; var builder = Sandbox.Common.ObjectBuilders.Serializer.MyObjectBuilderSerializer.CreateNewObject(type, subtypeName); return((MyObjectBuilder_Base)Serializer.Deserialize(ms, builder, m_objectFactory.GetProducedType(type))); } }
/// <summary> /// Does the v2 deserialization. /// </summary> /// <param name="stream"></param> /// <param name="lazy"></param> /// <param name="vehicles"></param> /// <returns></returns> protected override IBasicRouterDataSource <CHEdgeData> DoDeserialize( LimitedStream stream, bool lazy, IEnumerable <string> vehicles) { var intBytes = new byte[4]; stream.Read(intBytes, 0, 4); int startOfBlocks = BitConverter.ToInt32(intBytes, 0); stream.Read(intBytes, 0, 4); int startOfTags = BitConverter.ToInt32(intBytes, 0); stream.Read(intBytes, 0, 4); int sizeRegionIndex = BitConverter.ToInt32(intBytes, 0); // deserialize regions index. var chVertexRegionIndex = (CHVertexRegionIndex)_runtimeTypeModel.Deserialize( new CappedStream(stream, stream.Position, sizeRegionIndex), null, typeof(CHVertexRegionIndex)); // deserialize blocks index. stream.Seek(startOfBlocks, SeekOrigin.Begin); stream.Read(intBytes, 0, 4); int sizeBlockIndex = BitConverter.ToInt32(intBytes, 0); var chBlockIndex = (CHBlockIndex)_runtimeTypeModel.Deserialize( new CappedStream(stream, stream.Position, sizeBlockIndex), null, typeof(CHBlockIndex)); // deserialize tags. stream.Seek(startOfTags, SeekOrigin.Begin); ITagsCollectionIndexReadonly tagsIndex = TagIndexSerializer.DeserializeBlocks(stream); return(new v2.CHEdgeDataDataSource(stream, this, vehicles, sizeRegionIndex + 12, chVertexRegionIndex, _regionZoom, startOfBlocks + sizeBlockIndex + 4, chBlockIndex, (uint)_blockVertexSize, tagsIndex)); }
/// <summary> /// Does the v2 deserialization. /// </summary> /// <param name="stream"></param> /// <param name="lazy"></param> /// <returns></returns> protected override IBasicRouterDataSource <CHEdgeData> DoDeserialize( LimitedStream stream, bool lazy) { var intBytes = new byte[4]; stream.Read(intBytes, 0, 4); int startOfBlocks = BitConverter.ToInt32(intBytes, 0); // deserialize regions index. stream.Read(intBytes, 0, 4); int sizeRegionIndex = BitConverter.ToInt32(intBytes, 0); var chVertexRegionIndex = (CHVertexRegionIndex)_runtimeTypeModel.Deserialize( new CappedStream(stream, stream.Position, sizeRegionIndex), null, typeof(CHVertexRegionIndex)); // deserialize blocks index. stream.Seek(startOfBlocks, SeekOrigin.Begin); stream.Read(intBytes, 0, 4); int sizeBlockIndex = BitConverter.ToInt32(intBytes, 0); var chBlockIndex = (CHBlockIndex)_runtimeTypeModel.Deserialize( new CappedStream(stream, stream.Position, sizeBlockIndex), null, typeof(CHBlockIndex)); return(new CHEdgeDataDataSource(stream, this, sizeRegionIndex + 8, chVertexRegionIndex, RegionZoom, startOfBlocks + sizeBlockIndex + 4, chBlockIndex, BlockVertexSize)); }
public T Deserialize <T>(byte[] data) where T : class { var type = typeof(T); if (data == null) { return(default(T)); } using (var stream = new MemoryStream(data)) { return((T)_runtimeTypeModel.Deserialize(stream, null, type)); } }
public static T Deserialize <T>(string data) { try { using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(data))) { return((T)TypeModel.Deserialize(ms, null, typeof(T))); } } catch (Exception e) { MasterLog.DebugWriteLine(e.Message); return(default);
/// <summary> /// Deserializes the given stream into a primitive source. /// </summary> /// <param name="stream"></param> /// <param name="compress"></param> /// <param name="metaData"></param> /// <returns></returns> public static Primitive2DSource Deserialize(Stream stream, bool compress, out TagsCollectionBase metaData) { RuntimeTypeModel typeModel = SceneSerializer.BuildRuntimeTypeModel(); // [MetaIndexLenght:4][Metadata][SeneIndexLength:4][SceneIndex][SceneLengths:4*zoomFactors.length][Scenes] // MetaIndexLenght: int The lenght of the meta index. // Metadata: a number of serialized tags. // SceneIndexLength: int The length of the sceneindex in bytes. // SceneIndex: the serialized scene index. // SceneLengths: int[] The lengths of the scenes per zoom level as in the zoomfactors array. // Scenes: The serialized scenes themselves. // read metaLength. metaData = (new TagsCollectionSerializer()).DeserializeWithSize(stream); // read SeneIndexLength. var intBytes = new byte[4]; stream.Read(intBytes, 0, 4); int indexLength = BitConverter.ToInt32(intBytes, 0); // read SceneIndex byte[] indexBytes = new byte[indexLength]; stream.Read(indexBytes, 0, indexLength); MemoryStream indexStream = new MemoryStream(indexBytes); SceneIndex sceneIndex = typeModel.Deserialize(indexStream, null, typeof(SceneIndex)) as SceneIndex; // read SceneLengths int[] lengths = new int[sceneIndex.ZoomFactors.Length]; for (int idx = 0; idx < lengths.Length; idx++) { stream.Read(intBytes, 0, 4); lengths[idx] = BitConverter.ToInt32(intBytes, 0); } // create the deserializers. int start = (int)stream.Position; ISpatialIndexReadonly <Primitive2D>[] rTrees = new ISpatialIndexReadonly <Primitive2D> [lengths.Length]; for (int idx = 0; idx < lengths.Length; idx++) { Primitive2DRTreeDeserializer deserializer = new Primitive2DRTreeDeserializer( sceneIndex, compress, SceneSerializer.CalculateScaleFactor(sceneIndex.ZoomFactors[idx])); long position = stream.Position; rTrees[idx] = deserializer.Deserialize(new LimitedStream(stream), true); stream.Seek(position + lengths[idx], SeekOrigin.Begin); } return(new Primitive2DSource(sceneIndex.ZoomFactors, rTrees)); }
private static bool ReadConfiguration(string fileName) { if (!File.Exists(fileName)) { return(true); } using (var file = File.OpenRead(fileName)) { try { _instance = (WorkDocumentCollection)Serializer.Deserialize(file, _instance, typeof(WorkDocumentCollection)); TraceLog.WriteLine($"WorkDocumentCollection ({_instance._workingDocuments.Count} document(s)) successfully loaded from '{fileName}'. "); return(true); } catch (Exception e) { TraceLog.WriteLine($"WorkDocumentCollection deserialization from '{fileName}' failed: {e}"); } } return(false); }
/// <inheritdoc /> public sealed override T ReadFromStream <T>(Stream readStream, Encoding effectiveEncoding) { if (readStream == null) { throw new ArgumentNullException(nameof(readStream)); } try { return((T)s_model.Deserialize(readStream, null, typeof(T), null)); } catch (Exception ex) { s_logger.LogError(ex.ToString()); return(default);
/// <summary> /// Deserializes the given stream into a deserializable scene. /// </summary> /// <param name="stream"></param> /// <returns></returns> public Scene2DLayeredSource DeSerialize(Stream stream) { byte[] intBytes = new byte[4]; stream.Read(intBytes, 0, 4); int sceneIndexIndex = BitConverter.ToInt32(intBytes, 0); // move to the index position. stream.Seek(sceneIndexIndex, SeekOrigin.Begin); RuntimeTypeModel typeModel = TypeModel.Create(); typeModel.Add(typeof(Scene2DLayeredIndex), true); // the tile metadata. Scene2DLayeredIndex index = typeModel.Deserialize(stream, null, typeof(Scene2DLayeredIndex)) as Scene2DLayeredIndex; return(new Scene2DLayeredSource(stream, index)); }
public override object ReadObject(Stream data, Type type) { // _dataContractSerializer = new DataContractSerializer(type); //return _dataContractSerializer.ReadObject(data); //TODO: this is hack for async methods with null, should go from here to RPC related stuff if (typeof(IAsyncResult) == type || typeof(AsyncCallback) == type) { return(null); } if (data.Length == 0 && typeof(object) == type) { return(null); } return(_proto.Deserialize(data, null, type)); }
public bool TryDeserialize(ArraySegment <byte> data, out Message msg) { try { using (var stream = new MemoryStream(data.Array, data.Offset, data.Count)) { msg = (Message)_model.Deserialize(stream, null, typeof(Message)); } return(true); } catch (Exception) { msg = null; return(false); } }
protected override Task <object> OnReadFromStreamAsync(Type type, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext) { var tcs = new TaskCompletionSource <object>(); try { object result = model.Deserialize(stream, null, type); tcs.SetResult(result); } catch (Exception ex) { tcs.SetException(ex); } return(tcs.Task); }
/// <summary> /// Deserializes a tags index from the given stream. /// </summary> /// <param name="stream"></param> /// <returns></returns> public static ITagsCollectionIndexReadonly Deserialize(Stream stream) { byte[] intBytes = new byte[4]; stream.Read(intBytes, 0, 4); int startOfTags = BitConverter.ToInt32(intBytes, 0); stream.Read(intBytes, 0, 4); int endOfFile = BitConverter.ToInt32(intBytes, 0); RuntimeTypeModel typeModel = TagIndexSerializer.CreateTypeModel(); byte[] stringTableBytes = new byte[startOfTags - 8]; stream.Read(stringTableBytes, 0, stringTableBytes.Length); MemoryStream memoryStream = new MemoryStream(stringTableBytes); List <string> strings = typeModel.Deserialize(memoryStream, null, typeof(List <string>)) as List <string>; memoryStream.Dispose(); byte[] tagsIndexBytes = new byte[endOfFile - startOfTags]; stream.Read(tagsIndexBytes, 0, tagsIndexBytes.Length); memoryStream = new MemoryStream(tagsIndexBytes); List <KeyValuePair <uint, List <KeyValuePair <uint, uint> > > > tagsIndexTableList = typeModel.Deserialize(memoryStream, null, typeof(List <KeyValuePair <uint, List <KeyValuePair <uint, uint> > > >)) as List <KeyValuePair <uint, List <KeyValuePair <uint, uint> > > >; memoryStream.Dispose(); Dictionary <uint, TagsCollection> tagsIndexList = new Dictionary <uint, TagsCollection>(); for (int idx = 0; idx < tagsIndexTableList.Count; idx++) { KeyValuePair <uint, List <KeyValuePair <uint, uint> > > serializedTagsCollection = tagsIndexTableList[idx]; TagsCollection tagsCollection = new TagsCollection(); if (serializedTagsCollection.Value != null) { foreach (KeyValuePair <uint, uint> pair in serializedTagsCollection.Value) { tagsCollection.Add( new Tag(strings[(int)pair.Key], strings[(int)pair.Value])); } } tagsIndexList.Add(serializedTagsCollection.Key, tagsCollection); } return(new TagsIndexReadonly(tagsIndexList)); }
public ConversationAsset LoadConversation(string filePath) { ConversationAsset conversationAsset = null; RuntimeTypeModel runtimeTypeModel = TypeModel.Create(); try { using (FileStream fileStream = new FileStream(filePath, FileMode.Open)) { Conversation conversation = runtimeTypeModel.Deserialize(fileStream, null, typeof(Conversation)) as Conversation; conversationAsset = new ConversationAsset(filePath, conversation); } } catch (Exception error) { Log.Error(error.ToString()); return(null); } return(conversationAsset); }
public SpeakerListAsset LoadSpeakersList(string filePath) { SpeakerListAsset speakerListAsset = null; RuntimeTypeModel runtimeTypeModel = TypeModel.Create(); try { using (FileStream fileStream = new FileStream(filePath, FileMode.Open)) { ConversationSpeakerList speakerList = runtimeTypeModel.Deserialize(fileStream, null, typeof(ConversationSpeakerList)) as ConversationSpeakerList; speakerListAsset = new SpeakerListAsset(filePath, speakerList); } } catch (Exception error) { Log.Error(error.ToString()); return(null); } return(speakerListAsset); }
/// <summary> /// Deserializes an object from the given stream but uses the first 4 bytes as an indication of size and limits the data accordingly. /// </summary> /// <param name="model"></param> /// <param name="source"></param> /// <param name="value"></param> /// <param name="type"></param> /// <returns></returns> public static object DeserializeWithSize(this RuntimeTypeModel model, Stream source, object value, Type type) { // read only the relevant data from the stream. var sizeBytes = new byte[4]; source.Read(sizeBytes, 0, 4); int size = BitConverter.ToInt32(sizeBytes, 0); var data = new byte[size]; source.Read(data, 0, size); // deserialize. var dataStream = new MemoryStream(data); var deserializedValue = model.Deserialize(dataStream, value, type); dataStream.Dispose(); return(deserializedValue); }
static void Main() { _model = TypeModel.Create(); _model.Add(typeof(Vector3), false).Add(1, "x").Add(2, "y").Add(3, "z"); _model.Add(typeof(SerializableTestClass), true); SerializableTestClass testClass = new SerializableTestClass(); var _serializedObject = new MemoryStream(); testClass.changeMembers(); Console.WriteLine("Original object: " + testClass.ToString()); _model.Serialize(_serializedObject, testClass); _serializedObject.Position = 0; Console.WriteLine(_serializedObject.Length); SerializableTestClass test = (SerializableTestClass)_model.Deserialize(_serializedObject, null, typeof(SerializableTestClass)); Console.WriteLine("Deserialized object: " + test.ToString()); }
public static T deserializeData <T>(byte[] data) { if (_serializer == null) { Init(); } if (data == null) { return(default(T)); } T deserializedObject = default(T); //byte[] bytes = Encoding.UTF8.GetBytes(data); using (MemoryStream m = new MemoryStream(data)) { deserializedObject = (T)_serializer.Deserialize(m, null, typeof(T)); } return(deserializedObject); }
private void Check(bool shuffle) { var otherModel = TypeModel.Create(); using (var ms = new MemoryStream()) { _model.Serialize(ms, _model.ExportTypeRelations()); ms.Position = 0; var rel = _model.Deserialize <ModelTypeRelationsData>(ms); if (shuffle) // types order should not matter (only subtypes) { rel.Types = rel.Types.OrderBy(x => _rnd.Next(0, rel.Types.Length + 1)).ToArray(); } otherModel.ImportTypeRelations(rel, true); } var newTypes = otherModel.Types.Except(_model.Types).ToArray(); var removedTypes = _model.Types.Except(otherModel.Types).ToArray(); Assert.AreEqual(0, newTypes.Length); Assert.AreEqual(0, removedTypes.Length); if (!shuffle) { var inequal = otherModel.Types.Where((t, i) => t != _model.Types[i]).ToArray(); Assert.AreEqual(0, inequal.Length); } MetaType[] modelMetaTypes = _model.MetaTypes; for (int i = 0; i < modelMetaTypes.Length; i++) { Type type = modelMetaTypes[i].Type; SubType[] otherSubTypes = otherModel.FindWithoutAdd(type).GetSubtypes(); SubType[] modelSubTypes = modelMetaTypes[i].GetSubtypes(); Assert.AreEqual(modelSubTypes.Length, otherSubTypes.Length, "Subtypes of " + type.FullName); for (int j = 0; j < otherSubTypes.Length; j++) { Assert.AreEqual(otherSubTypes[j].DerivedType.Type, modelSubTypes[j].DerivedType.Type); Assert.AreEqual(otherSubTypes[j].FieldNumber, modelSubTypes[j].FieldNumber); } } }
public void TestOrderLineGetDeserializedAndAttachedToOrder() { byte[] fileBytes = File.ReadAllBytes(@"NWind\nwind.proto.bin"); RuntimeTypeModel ordersModel = TypeModel.Create(); ordersModel.AutoCompile = false; Database database = (Database)ordersModel.Deserialize(new MemoryStream(fileBytes), null, typeof(Database)); List <Order> orders = database.Orders; DbMetrics("From File", orders); var roundTrippedOrders = (List <Order>)ordersModel.DeepClone(orders); Assert.AreNotSame(orders, roundTrippedOrders); DbMetrics("Round trip", roundTrippedOrders); Assert.AreEqual(orders.SelectMany(o => o.Lines).Count(), roundTrippedOrders.SelectMany(o => o.Lines).Count(), "total count"); }
/// <summary> /// Deserializes the actual data. /// </summary> /// <param name="typeModel"></param> /// <param name="data"></param> /// <param name="boxes"></param> /// <returns></returns> protected override List <string> DeSerialize(RuntimeTypeModel typeModel, byte[] data, out List <BoxF2D> boxes) { // deserialize data. var collection = (typeModel.Deserialize(new MemoryStream(data), null, typeof(PrimitivesCollection)) as PrimitivesCollection); if (collection == null) { throw new Exception("Cannot deserialize data!"); } // fill boxes. boxes = new List <BoxF2D>(collection.Data.Length); for (int idx = 0; idx < collection.Data.Length; idx++) { boxes.Add(new BoxF2D(collection.MinX[idx], collection.MinY[idx], collection.MaxX[idx], collection.MaxY[idx])); } return(new List <string>(collection.Data)); }
/// <summary> /// Deserializes a tags collection from a byte array. /// </summary> /// <param name="tagsBytes"></param> /// <returns></returns> public TagsCollectionBase Deserialize(byte[] tagsBytes) { RuntimeTypeModel typeModel = TypeModel.Create(); typeModel.Add(typeof(List <KeyValuePair <string, string> >), true); List <KeyValuePair <string, string> > tagsList = null; using (MemoryStream stream = new MemoryStream(tagsBytes)) { tagsList = typeModel.Deserialize(stream, null, typeof(List <KeyValuePair <string, string> >)) as List <KeyValuePair <string, string> >; } TagsCollection tagsCollection = new TagsCollection(); foreach (KeyValuePair <string, string> tag in tagsList) { tagsCollection.Add(tag.Key, tag.Value); } return(tagsCollection); }
/// <summary> /// Deserializes an ApplicationLog based on raw data using protobuf /// </summary> /// <param name="data">byte[] data</param> /// <returns>An ApplicationLog instance</returns> public ApplicationLog DeserializeApplicationLog(byte[] data) { RuntimeTypeModel model = RuntimeTypeModel.Create(); Type applicationLogType = typeof(ApplicationLog); model.Add(applicationLogType, true); model[applicationLogType].Add(1, "Message"); model[applicationLogType].Add(2, "LogMessageType"); model[applicationLogType].Add(3, "Timestamp"); model[applicationLogType].Add(4, "AppId"); model[applicationLogType].Add(6, "SourceId"); model[applicationLogType].Add(7, "DrainUrls"); model[applicationLogType].Add(8, "SourceName"); ApplicationLog log = null; using (MemoryStream stream = new MemoryStream(data)) { return((ApplicationLog)model.Deserialize(stream, log, applicationLogType)); } }
public override Task <InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } object model; try { model = _runtimeTypeModel.Deserialize(context.HttpContext.Request.Body, null, context.ModelType); } catch (Exception exception) { _logger.LogDebug(exception, "Exception was occurred during deserialization"); return(InputFormatterResult.FailureAsync()); } return(InputFormatterResult.SuccessAsync(model)); }