public static void RunDeserializationPerformanceTest(int numberOfRuns, TestType testType, int testSize, ISerializerFactory serializerFactory) { var obj = GetSerializationObject(testType, testSize); var serializer = serializerFactory.GetSerializer(); serializer.Init(obj); using (var stream = new MemoryStream()) { serializer.Serialize(obj, stream); stream.Position = 0; foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < numberOfRuns; i++) { serializer.Deserialize(stream); stream.Position = 0; } } } } }
/// <summary> /// Saves the specified <paramref name="tailCount"/> number of stack elements from the top of the stack (in default stack enumeration order, i.e. moving towards the bottom of the stack) to the streams yielded by the <paramref name="streams"/> sequence. /// </summary> /// <param name="serializerFactory">The factory to use to obtain a statically typed serializer.</param> /// <param name="tailCount">The number of stack elements to save starting from the top of the queue.</param> /// <param name="streams">The streams to save the stack elements to. This sequence has a length equal to <paramref name="tailCount"/>.</param> void IStackPersistence.Save(ISerializerFactory serializerFactory, int tailCount, IEnumerable <Stream> streams) { // // Get the serializer once to reduce overheads. // var serializer = serializerFactory.GetSerializer <T>(); // // Zip the stack elements (most recent first) with the corresponding streams to persist to. // // PERF: While the enumeration below is O(tailCount), it results in a reverse order for persistence operations. Should we consider a custom Stack<T> implementation for fast in-order random access? // using var stackEnumerator = _stack.GetEnumerator(); using var streamEnumerator = streams.GetEnumerator(); for (var i = 0; i < tailCount; i++) { Invariant.Assert(stackEnumerator.MoveNext(), "No more elements in stack."); Invariant.Assert(streamEnumerator.MoveNext(), "No more streams to write to."); var element = stackEnumerator.Current; using var stream = streamEnumerator.Current; serializer.Serialize(element, stream); } Invariant.Assert(!streamEnumerator.MoveNext(), "Should have consumed all streams."); }
/// <summary> /// Saves the set element stored at the specified <paramref name="key"/> to the specified <paramref name="stream"/>. /// </summary> /// <param name="serializerFactory">The factory to use to obtain a statically typed serializer.</param> /// <param name="stream">The stream to save the set element to.</param> /// <param name="key">The key of the set element to save.</param> void IHeapPersistence.Save(ISerializerFactory serializerFactory, Stream stream, string key) { var keyValue = GetIndexForKey(key); var value = _storageKeys.GetByValue(keyValue); serializerFactory.GetSerializer <T>().Serialize(value, stream); }
public T DeepClone(T source) { var binarySerializer = serializerFactory.GetSerializer(_defaultCloneOptions.SerializerType); var serialized = binarySerializer.Serialize(source); return(binarySerializer.Deserialize <T>(serialized)); }
private static async Task <HttpMessageSendChangesResponse> DeserializeAsync(ISerializerFactory serializerFactory, string fileName, string directoryFullPath, BaseOrchestrator orchestrator = null) { var fullPath = Path.Combine(directoryFullPath, fileName); using var fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read); var httpMessageContent = await serializerFactory.GetSerializer <HttpMessageSendChangesResponse>().DeserializeAsync(fileStream); return(httpMessageContent); }
/// <summary> /// Saves the specified <paramref name="tailCount"/> number of queue elements from the tail of the queue (in default queue enumeration order, i.e. moving towards the tail) to the streams yielded by the <paramref name="streams"/> sequence. /// </summary> /// <param name="serializerFactory">The factory to use to obtain a statically typed serializer.</param> /// <param name="tailCount">The number of queue elements to save starting from the tail of the queue.</param> /// <param name="streams">The streams to save the queue elements to. This sequence has a length equal to <paramref name="tailCount"/>.</param> void IQueuePersistence.Save(ISerializerFactory serializerFactory, long tailCount, IEnumerable <Stream> streams) { // // The obtain the specified number of elements from the tail in dequeue order, we have to enumerate over the whole queue and skip from the front. // // PERF: Is this O(queueCount) traversal in lieu of an O(tailCount) lookup cost tolerable? If not, we should re-implement Queue<T> to enable fast random access. // var skip = _queue.Count - tailCount; Invariant.Assert(skip >= 0, "Requested tail count greater than available queue size."); using var queueEnumerator = _queue.GetEnumerator(); // // Skip the elements we don't care about. // for (var i = 0; i < skip; i++) { Invariant.Assert(queueEnumerator.MoveNext(), "Failed to skip."); } // // Get the serializer once to reduce overheads. // var serializer = serializerFactory.GetSerializer <T>(); // // Save the remaining tailCount elements to the streams provided. // using var streamEnumerator = streams.GetEnumerator(); for (var i = 0L; i < tailCount; i++) { Invariant.Assert(queueEnumerator.MoveNext(), "No more elements in queue."); Invariant.Assert(streamEnumerator.MoveNext(), "No more streams to write to."); var element = queueEnumerator.Current; using var stream = streamEnumerator.Current; serializer.Serialize(element, stream); } Invariant.Assert(!queueEnumerator.MoveNext(), "Should have reached last element of the queue."); Invariant.Assert(!streamEnumerator.MoveNext(), "Should have consumed all streams."); }
/// <summary> /// Возвращает строчное представление объекта в указанном формате /// </summary> /// <param name="value">сериализуемый объект</param> /// <param name="format">формат</param> /// <param name="objectname">имя для сериализуемого объекта</param> /// <param name="options">Опции сериализации</param> public static string SerializeAsFormat(this object value, SerializationFormat format, string objectname = DefaultObjectName, object options = null) { var serializer = #if EMBEDQPT sfactory.GetSerializer(format); #else Applications.Application.Current.Serialization.GetSerializer(format); #endif if (string.IsNullOrWhiteSpace(objectname)) { objectname = DefaultObjectName; } return(serializer.Serialize(value, objectname)); }
/// <summary> /// Serialize a container set instance /// </summary> private static async Task SerializeAsync(ContainerSet set, string fileName, string directoryFullPath, ISerializerFactory serializerFactory = default, BaseOrchestrator orchestrator = null) { if (set == null) { return; } var fullPath = Path.Combine(directoryFullPath, fileName); var fi = new FileInfo(fullPath); if (!Directory.Exists(fi.Directory.FullName)) { Directory.CreateDirectory(fi.Directory.FullName); } if (serializerFactory == default) { serializerFactory = SerializersCollection.JsonSerializer; } var serializer = serializerFactory.GetSerializer <ContainerSet>(); using var f = new FileStream(fullPath, FileMode.CreateNew, FileAccess.ReadWrite); byte[] serializedBytes = null; if (orchestrator != null) { var interceptorArgs = new SerializingSetArgs(orchestrator.GetContext(), set, serializerFactory, fileName, directoryFullPath); await orchestrator.InterceptAsync(interceptorArgs, default); serializedBytes = interceptorArgs.Result; } if (serializedBytes == null) { serializedBytes = await serializer.SerializeAsync(set); } f.Write(serializedBytes, 0, serializedBytes.Length); //await f.FlushAsync(); }
/// <summary>Deserializes the response stream.</summary> /// <param name="response">The response.</param> /// <param name="serializerFactory">The serializer factory.</param> /// <param name="gzipInflator">The GZIP inflator.</param> /// <typeparam name="TResult">The type of the response content.</typeparam> /// <returns>An instance of the specified type.</returns> private TResult DeserializeResponse <TResult>(HttpWebResponse response, ISerializerFactory serializerFactory, IConverter <Stream, Stream> gzipInflator) { Debug.Assert(response != null, "response != null"); Debug.Assert(serializerFactory != null, "serializerFactory != null"); Debug.Assert(gzipInflator != null, "gzipInflator != null"); // Get the response content var stream = response.GetResponseStream(); // Ensure that there is response content if (stream == null) { return(default(TResult)); } Debug.Assert(response != null, "response != null"); Debug.Assert(response.Headers != null, "response.Headers != null"); // Ensure that we are operating on decompressed data var contentEncoding = response.Headers["Content-Encoding"]; if (contentEncoding != null) { var compressed = contentEncoding.Equals("gzip", StringComparison.OrdinalIgnoreCase); if (compressed) { var uncompressed = gzipInflator.Convert(stream, null); if (uncompressed != null) { stream = uncompressed; } } } Debug.Assert(stream.CanRead, "stream.CanRead"); // Create a serializer var serializer = serializerFactory.GetSerializer <TResult>(); // Deserialize the response content return(serializer.Deserialize(stream)); }
private void ShowOutput(ClassInfo classInfo, string menuItemName) { IClassInfoSerializer serializer; try { serializer = _serializerFactory.GetSerializer(menuItemName); } catch (ArgumentException ex) { ShowMessageBox(ServiceProvider, $"Error retrieving '{menuItemName}' serializer:" + Environment.NewLine + Environment.NewLine + ex.Message); return; } foreach (var handler in _outputHandlers) { if (handler.Handle(serializer, classInfo)) { break; } } }
private void ShowOutput(ClassInfo classInfo, string menuItemName) { IClassInfoSerializer serializer; try { serializer = _serializerFactory.GetSerializer(menuItemName); } catch (ArgumentException ex) { ShowMessageBox(ServiceProvider, $"Error retrieving '{menuItemName}' serializer:" + Environment.NewLine + Environment.NewLine + ex.Message); return; } if (!serializer.CanSerialize(classInfo)) { ShowMessageBox(ServiceProvider, $"Could not serialize {classInfo.Name} to {serializer.DisplayName}"); return; } foreach (var handler in _outputHandlers) { try { if (handler.Handle(serializer, classInfo)) { return; } } catch (Exception e) { ShowMessageBox(ServiceProvider, $"Error serializing {classInfo.Name}: {e}"); return; } } ShowMessageBox(ServiceProvider, $"Could not find a handler for {classInfo.Name}"); }
public string SerializeSomeData(MyData data) { ISerializer serializer = serializerFactory.GetSerializer("Json"); return(serializer.Serialize(data)); }
/// <summary> /// Process a request message with HttpClient object. /// </summary> public async Task <U> ProcessRequestAsync <U>(HttpClient client, string baseUri, byte[] data, HttpStep step, Guid sessionId, ISerializerFactory serializerFactory, IConverter converter, int batchSize, CancellationToken cancellationToken) { if (client is null) { throw new ArgumentNullException(nameof(client)); } if (baseUri == null) { throw new ArgumentException("BaseUri is not defined"); } HttpResponseMessage response = null; var responseMessage = default(U); try { if (cancellationToken.IsCancellationRequested) { cancellationToken.ThrowIfCancellationRequested(); } // Get response serializer var responseSerializer = serializerFactory.GetSerializer <U>(); var requestUri = new StringBuilder(); requestUri.Append(baseUri); requestUri.Append(baseUri.EndsWith("/", StringComparison.CurrentCultureIgnoreCase) ? string.Empty : "/"); // Add params if any if (ScopeParameters != null && ScopeParameters.Count > 0) { string prefix = "?"; foreach (var kvp in ScopeParameters) { requestUri.AppendFormat("{0}{1}={2}", prefix, Uri.EscapeUriString(kvp.Key), Uri.EscapeUriString(kvp.Value)); if (prefix.Equals("?")) { prefix = "&"; } } } // get byte array content var arrayContent = data == null ? new ByteArrayContent(new byte[] { }) : new ByteArrayContent(data); // reinit client client.DefaultRequestHeaders.Clear(); // add it to the default header //if (this.Cookie != null) // client.DefaultRequestHeaders.Add("Cookie", this.Cookie.ToString()); // Create the request message var requestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri.ToString()) { Content = arrayContent }; // Adding the serialization format used and session id requestMessage.Headers.Add("dotmim-sync-session-id", sessionId.ToString()); requestMessage.Headers.Add("dotmim-sync-step", ((int)step).ToString()); // serialize the serialization format and the batchsize we want. var ser = JsonConvert.SerializeObject(new { f = serializerFactory.Key, s = batchSize }); requestMessage.Headers.Add("dotmim-sync-serialization-format", ser); // if client specifies a converter, add it as header if (converter != null) { requestMessage.Headers.Add("dotmim-sync-converter", converter.Key); } // Adding others headers if (this.CustomHeaders != null && this.CustomHeaders.Count > 0) { foreach (var kvp in this.CustomHeaders) { if (!requestMessage.Headers.Contains(kvp.Key)) { requestMessage.Headers.Add(kvp.Key, kvp.Value); } } } // If Json, specify header if (serializerFactory.Key == SerializersCollection.JsonSerializer.Key && !requestMessage.Content.Headers.Contains("content-type")) { requestMessage.Content.Headers.Add("content-type", "application/json"); } // Eventually, send the request response = await client.SendAsync(requestMessage, cancellationToken).ConfigureAwait(false); if (cancellationToken.IsCancellationRequested) { cancellationToken.ThrowIfCancellationRequested(); } // throw exception if response is not successfull // get response from server if (!response.IsSuccessStatusCode && response.Content != null) { await HandleSyncError(response, serializerFactory); } // try to set the cookie for http session var headers = response?.Headers; if (headers != null) { if (headers.TryGetValues("Set-Cookie", out var tmpList)) { var cookieList = tmpList.ToList(); // var cookieList = response.Headers.GetValues("Set-Cookie").ToList(); if (cookieList != null && cookieList.Count > 0) { #if NETSTANDARD // Get the first cookie this.Cookie = CookieHeaderValue.ParseList(cookieList).FirstOrDefault(); #else //try to parse the very first cookie if (CookieHeaderValue.TryParse(cookieList[0], out var cookie)) { this.Cookie = cookie; } #endif } } } if (response.Content == null) { throw new HttpEmptyResponseContentException(); } using (var streamResponse = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) if (streamResponse.CanRead && streamResponse.Length > 0) { responseMessage = responseSerializer.Deserialize(streamResponse); } return(responseMessage); } catch (SyncException) { throw; } catch (Exception e) { if (response == null || response.Content == null) { throw new HttpResponseContentException(e.Message); } var exrror = await response.Content.ReadAsStringAsync().ConfigureAwait(false); throw new HttpResponseContentException(exrror); } }
private async Task <ContainerSet> DeserializeAsync(string fileName, string directoryFullPath, ISerializerFactory serializerFactory = default, BaseOrchestrator orchestrator = null) { if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(fileName); } if (string.IsNullOrEmpty(directoryFullPath)) { throw new ArgumentNullException(directoryFullPath); } var fullPath = Path.Combine(directoryFullPath, fileName); if (!File.Exists(fullPath)) { throw new MissingFileException(fullPath); } // backward compatibility if (serializerFactory == default) { serializerFactory = SerializersCollection.JsonSerializer; } // backward compatibility if (this.SerializedType == default) { this.SerializedType = typeof(ContainerSet); } Debug.WriteLine($"Deserialize file {fileName}"); using var fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read); ContainerSet set = null; if (orchestrator != null) { var interceptorArgs = new DeserializingSetArgs(orchestrator.GetContext(), fs, serializerFactory, fileName, directoryFullPath); await orchestrator.InterceptAsync(interceptorArgs, default); set = interceptorArgs.Result; } if (set == null) { if (this.SerializedType == typeof(ContainerSet)) { var serializer = serializerFactory.GetSerializer <ContainerSet>(); set = await serializer.DeserializeAsync(fs); } else { var serializer = serializerFactory.GetSerializer <ContainerSetBoilerPlate>(); var jobject = await serializer.DeserializeAsync(fs); set = jobject.Changes; } } return(set); }
/// <summary> /// Process a request message with HttpClient object. /// </summary> public async Task <U> ProcessRequestAsync <U>(HttpClient client, string baseUri, byte[] data, HttpStep step, Guid sessionId, string scopeName, ISerializerFactory serializerFactory, IConverter converter, int batchSize, CancellationToken cancellationToken) { if (client is null) { throw new ArgumentNullException(nameof(client)); } if (baseUri == null) { throw new ArgumentException("BaseUri is not defined"); } HttpResponseMessage response = null; var responseMessage = default(U); try { if (cancellationToken.IsCancellationRequested) { cancellationToken.ThrowIfCancellationRequested(); } // Get response serializer var responseSerializer = serializerFactory.GetSerializer <U>(); var requestUri = new StringBuilder(); requestUri.Append(baseUri); requestUri.Append(baseUri.EndsWith("/", StringComparison.CurrentCultureIgnoreCase) ? string.Empty : "/"); // Add params if any if (ScopeParameters != null && ScopeParameters.Count > 0) { string prefix = "?"; foreach (var kvp in ScopeParameters) { requestUri.AppendFormat("{0}{1}={2}", prefix, Uri.EscapeUriString(kvp.Key), Uri.EscapeUriString(kvp.Value)); if (prefix.Equals("?")) { prefix = "&"; } } } // Check if data is null data = data == null ? new byte[] { } : data; // get byte array content var arrayContent = new ByteArrayContent(data); // reinit client client.DefaultRequestHeaders.Clear(); // Create the request message var requestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri.ToString()) { Content = arrayContent }; // Adding the serialization format used and session id and scope name requestMessage.Headers.Add("dotmim-sync-session-id", sessionId.ToString()); requestMessage.Headers.Add("dotmim-sync-scope-name", scopeName); requestMessage.Headers.Add("dotmim-sync-step", ((int)step).ToString()); // serialize the serialization format and the batchsize we want. var ser = JsonConvert.SerializeObject(new { f = serializerFactory.Key, s = batchSize }); requestMessage.Headers.Add("dotmim-sync-serialization-format", ser); // if client specifies a converter, add it as header if (converter != null) { requestMessage.Headers.Add("dotmim-sync-converter", converter.Key); } // calculate hash var hash = HashAlgorithm.SHA256.Create(data); var hashString = Convert.ToBase64String(hash); requestMessage.Headers.Add("dotmim-sync-hash", hashString); // Adding others headers if (this.CustomHeaders != null && this.CustomHeaders.Count > 0) { foreach (var kvp in this.CustomHeaders) { if (!requestMessage.Headers.Contains(kvp.Key)) { requestMessage.Headers.Add(kvp.Key, kvp.Value); } } } // If Json, specify header if (serializerFactory.Key == SerializersCollection.JsonSerializer.Key && !requestMessage.Content.Headers.Contains("content-type")) { requestMessage.Content.Headers.Add("content-type", "application/json"); } // Eventually, send the request response = await client.SendAsync(requestMessage, cancellationToken).ConfigureAwait(false); if (cancellationToken.IsCancellationRequested) { cancellationToken.ThrowIfCancellationRequested(); } // throw exception if response is not successfull // get response from server if (!response.IsSuccessStatusCode && response.Content != null) { await HandleSyncError(response, serializerFactory); } // try to set the cookie for http session var headers = response?.Headers; // Ensure we have a cookie this.EnsureCookie(headers); if (response.Content == null) { throw new HttpEmptyResponseContentException(); } using (var streamResponse = await response.Content.ReadAsStreamAsync().ConfigureAwait(false)) { if (streamResponse.CanRead && streamResponse.Length > 0) { // if Hash is present in header, check hash if (TryGetHeaderValue(headers, "dotmim-sync-hash", out string hashStringRequest)) { HashAlgorithm.SHA256.EnsureHash(streamResponse, hashStringRequest); } responseMessage = await responseSerializer.DeserializeAsync(streamResponse); } } return(responseMessage); } catch (SyncException) { throw; } catch (Exception e) { if (response == null || response.Content == null) { throw new HttpResponseContentException(e.Message); } var exrror = await response.Content.ReadAsStringAsync().ConfigureAwait(false); throw new HttpResponseContentException(exrror); } }