public ISerializerService BuildSerializerServiceWithConfig( SerializationConfiguration config = null) { var services = new ServiceCollection(); services.UseRegistration(); services.UseDomain(); services.UseSerialization(config ?? SerializationConfiguration.DefaultConfig); var serviceProvider = services.BuildServiceProvider(); return(serviceProvider.GetService <ISerializerService>()); }
public void Deserialize(Stream stream, SerializationConfiguration settings) { IEnumerable <(string, object)> res; var keys = (IEnumerable <RecordDefinition>)records.Keys; settings.Serializer.Deserialize(stream, in keys, out res); ClearValues(); foreach (var v in res.ToArray()) { SetValue(v.Item1, v.Item2); } }
public StripeWriter(Type pocoType, Stream outputStream, bool shouldAlignNumericValues, double uniqueStringThresholdRatio, int defaultDecimalPrecision, int defaultDecimalScale, Compression.OrcCompressedBufferFactory bufferFactory, int strideLength, long stripeLength, SerializationConfiguration serializationConfiguration) { _typeName = pocoType.Name; _outputStream = outputStream; _shouldAlignNumericValues = shouldAlignNumericValues; _uniqueStringThresholdRatio = uniqueStringThresholdRatio; _defaultDecimalPrecision = defaultDecimalPrecision; _defaultDecimalScale = defaultDecimalScale; _bufferFactory = bufferFactory; _strideLength = strideLength; _stripeLength = stripeLength; _serializationConfiguration = serializationConfiguration; CreateColumnWriters(pocoType); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); Random rnd = new Random(); Config config = new Config(); var settings = new SerializationConfiguration(); settings.UseJsonNetSerializer(); config.Load(Path.Combine(Environment.CurrentDirectory, "a.json"), settings); Console.WriteLine(config.Test); config.Test = rnd.Next().ToString(); config.Save(Path.Combine(Environment.CurrentDirectory, "a.json"), settings); Console.WriteLine(config.Test); Console.ReadLine(); }
public static MyCouchStore Create(CouchDbConnection cn, string dbName) { // configure the JSON serializers to include the $type field in the JSON to support deserializing to the // concrete classes where we used inheritance and interfaces / base classes. var bootstrapper = new MyCouchClientBootstrapper(); bootstrapper.SerializationConfigurationFn = () => { var cfg = new SerializationConfiguration(new SerializationContractResolver()); cfg.Settings.TypeNameHandling = TypeNameHandling.Objects; return(cfg); }; var client = new MyCouchClient(CouchDbConnection.Current.ServerUrl, dbName, bootstrapper); var store = new MyCouchStore(client); return(store); }
//[TestCase] //public void CorrectlySerializesToJsonStringWithInvariantCulture() //{ // TestSerializationUsingCulture(CultureInfo.InvariantCulture); //} //[TestCase] //public void CorrectlySerializesToJsonStringWithDutchCulture() //{ // TestSerializationUsingCulture(new CultureInfo("nl-NL")); //} //[TestCase] //public void CorrectlySerializesToJsonStringWithCustomCulture() //{ // var cultureInfo = new CultureInfo("en-US", true); // cultureInfo.DateTimeFormat = new DateTimeFormatInfo // { // } // TestSerializationUsingCulture(cultureInfo); //} private void TestSerializationUsingCulture(CultureInfo culture) { var testModel = new TestModel(); var currentDateTime = DateTime.Now; testModel.DateTimeProperty = currentDateTime; var configuration = new SerializationConfiguration { Culture = culture }; var json = testModel.ToJson(configuration); Assert.IsTrue(json.Contains(currentDateTime.ToString(culture))); }
public void CustomNegotiation() { const String contentType = "application/custom"; var configuration = new SerializationConfiguration(); configuration.Map(_ => _.MediaType == "application/custom", new FakeSerializer()); var negotiator = new Mock <IContentNegotiator>(); var @set = new SortedSet <ContentNegotiator.MediaTypeHeader>(); @set.Add(ContentNegotiator.MediaTypeHeader.Parse(contentType)); negotiator.Setup(_ => _.Negotiate(contentType)) .Returns(@set); configuration.NegotiateBy(negotiator.Object); var serializer = configuration.Create(contentType); Assert.IsType <FakeSerializer>(serializer); }
private async Task OnSaveConfigurationExecuteAsync() { try { _saveFileService.Filter = "Kflop Plot File|*.kfp"; if (!string.IsNullOrEmpty(Config.SavePath)) { _saveFileService.InitialDirectory = Path.GetDirectoryName(Config.SavePath); if (!Directory.Exists(_saveFileService.InitialDirectory)) { _saveFileService.InitialDirectory = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\Data")); } _saveFileService.FileName = Path.GetFileNameWithoutExtension(Config.SavePath); } else { _saveFileService.InitialDirectory = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\Data")); } if (_saveFileService.DetermineFile()) { if (Config.SavePath != _saveFileService.FileName) { Config.SavePath = _saveFileService.FileName; } using (var fileStream = File.Create(_saveFileService.FileName)) { var configuration = new SerializationConfiguration { Culture = new System.Globalization.CultureInfo("en-US") }; Config.Save(fileStream, Catel.Data.SerializationMode.Xml, configuration); } this.ClearIsDirtyOnAllChilds(); this.IsDirty = false; ViewModelCommandManager.InvalidateCommands(true); LogTo.Info("Saved Plot configuration to: {0}", Config.SavePath); } } catch (Exception ex) { string errmsg = string.Format("Error saving Plot configuration: {0}", ex.Message); LogTo.Error(errmsg); await Shared.Utility.ShowErrorMsgAsync(this, errmsg); } }
public void ExceptionOnResolve() { const String type = "fake-type"; var serializationConfiguration = new SerializationConfiguration(); var resolver = new Mock <IMessageTypeResolver>(); resolver.Setup(_ => _.Resolve(It.Is <ConsumedMessageContext>(__ => __.MessageType == type))) .Throws <Exception>(); var builder = new ConsumedMessageBuilder(serializationConfiguration, resolver.Object); var message = builder.Build(new BasicDeliverEventArgs { BasicProperties = new BasicProperties { Type = type } }); Assert.IsType <UnresolvedMessage>(message); }
/// <summary> /// Wrapper to all object to byte[] conversions /// </summary> internal static byte[] GetBytesFromObject(object obj, SerializationConfiguration serializationConfig) { switch (obj) { case bool bo: return(BitConverter.GetBytes(bo)); case byte b: return(new[] { b }); case string str: return(System.Text.Encoding.UTF8.GetBytes(str)); case char c: return(BitConverter.GetBytes(c)); case double d: return(BitConverter.GetBytes(d)); case float f: return(BitConverter.GetBytes(f)); case int i: return(BitConverter.GetBytes(i)); case long l: return(BitConverter.GetBytes(l)); case short sh: return(BitConverter.GetBytes(sh)); case uint ui: return(BitConverter.GetBytes(ui)); case ulong ul: return(BitConverter.GetBytes(ul)); case ushort us: return(BitConverter.GetBytes(us)); default: { if (serializationConfig != null) { Type type = obj.GetType(); if (serializationConfig.ContainsSerializationRule(type)) { return(serializationConfig.SerializeAsObject(type, obj)); } } using MemoryStream ms = new(); FORMATTER.Serialize(ms, obj); ms.Seek(0, SeekOrigin.Begin); return(ms.ToArray()); } } }
public void FluentSerialization_IdentifiesReferenceProperties() { var conf = new SerializationConfiguration(); conf.ConfigureType <TestType>() .ConfigureProperty(x => x.StrColumn, x => x.ExcludeFromSerialization = true) .ConfigureProperty(x => x.ClassColumn, x => x.ExcludeFromSerialization = true) ; var properties = conf.Types[typeof(TestType)].Properties; Assert.Contains(typeof(TestType).GetProperty("StrColumn"), properties.Keys); Assert.Contains(typeof(TestType).GetProperty("ClassColumn"), properties.Keys); foreach (var propertyConfiguration in properties.Values) { Assert.True(propertyConfiguration.ExcludeFromSerialization); } }
/// <summary> /// Method to invoke when the SaveConfigCommand command is executed. /// </summary> private async Task OnSaveConfigCommandExecuteAsync() { try { var saveFileService = ServiceLocator.Default.ResolveType <ISaveFileService>(); saveFileService.Filter = "Kflop Loggging Configuration File|*.kfl"; if (!string.IsNullOrEmpty(SaveConfigPath)) { saveFileService.InitialDirectory = Path.GetDirectoryName(SaveConfigPath); if (!Directory.Exists(saveFileService.InitialDirectory)) { saveFileService.InitialDirectory = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\Data")); } saveFileService.FileName = Path.GetFileNameWithoutExtension(SaveConfigPath); } else { saveFileService.InitialDirectory = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\Data")); } if (saveFileService.DetermineFile()) { if (SaveConfigPath != saveFileService.FileName) { SaveConfigPath = saveFileService.FileName; } using (var fileStream = File.Create(saveFileService.FileName)) { var configuration = new SerializationConfiguration { Culture = new System.Globalization.CultureInfo("en-US") }; WindowModel.Save(fileStream, Catel.Data.SerializationMode.Xml, configuration); } LogTo.Info("Saved configuration to: {0}", SaveConfigPath); } } catch (Exception ex) { string errmsg = string.Format("Error saving configuration: {0}", ex.Message); LogTo.Error(errmsg); await Shared.Utility.ShowErrorMsgAsync(this, errmsg); } }
public static void Main(string[] args) { var baseTime = new DateTime(2017, 3, 16, 0, 0, 0, DateTimeKind.Utc); var rand = new Random(123); var testElements = new List <TestClass>(); for (int i = 0; i < 80000; i++) { var random = rand.Next(); var set = i / 10000; var randomInRange = (random % 10000) + set * 10000 - 40000; var dec = (DateTime.Now - DateTime.Today).Ticks / (decimal)TimeSpan.TicksPerSecond; var timestamp = baseTime.AddTicks(random); var element = new TestClass { Random = random, RandomInRange = randomInRange, Incrementing = i, SetNumber = set, Double = (double)i / (set + 1), Float = (float)i / (set + 1), Dec = dec, Timestamp = timestamp, Str = $"Random={random}, RandomInRange={randomInRange}, Incrementing={i}, SetNumber={set}, Dec={dec}, Timestamp={timestamp:MM/dd/yyyy hh:mm:ss.fffffff}", DictionaryStr = $"SetNumber={set}" }; testElements.Add(element); } var serializationConfiguration = new SerializationConfiguration() .ConfigureType <TestClass>() .ConfigureProperty(x => x.Dec, x => { x.DecimalPrecision = 14; x.DecimalScale = 9; }) .Build(); using (var fileStream = new FileStream("test.orc", FileMode.Create, FileAccess.Write)) using (var writer = new OrcWriter <TestClass>(fileStream, new WriterConfiguration(), serializationConfiguration)) //Use the default configuration { writer.AddRows(testElements); } } }
/// <summary> /// Create IStreamWriter that writes its data to an internal byte[] buffer. It will grow as needed. /// Call 'GetReader' to get a IStreamReader for the written bytes. /// /// Call 'GetBytes' call to get the raw array. Only the first 'Length' bytes are valid /// </summary> public MemoryStreamWriter(int initialSize = 64, SerializationConfiguration config = null) { bytes = new byte[initialSize]; SerializationConfiguration = config != null ? config : new SerializationConfiguration(); if (SerializationConfiguration.StreamLabelWidth == StreamLabelWidth.FourBytes) { writeLabel = (value) => { Debug.Assert((long)value <= int.MaxValue); Write((int)value); }; } else { writeLabel = (value) => { Debug.Assert((long)value <= long.MaxValue); Write((long)value); }; } }
public SegmentedMemoryStreamWriter(long initialSize, SerializationConfiguration config = null) { SerializationConfiguration = config ?? new SerializationConfiguration(); if (SerializationConfiguration.StreamLabelWidth == StreamLabelWidth.FourBytes) { writeLabel = (value) => { Debug.Assert((long)value <= int.MaxValue, "The StreamLabel overflowed, it should not be treated as a 32bit value."); Write((int)value); }; } else { writeLabel = (value) => { Write((long)value); }; } bytes = new SegmentedList <byte>(65_536, initialSize); }
private void OpenConfiguration(string path) { using (var fileStream = File.Open(path, FileMode.Open)) { var configuration = new SerializationConfiguration { Culture = new System.Globalization.CultureInfo("en-US") }; WindowModel = MainWindowModel.Load(fileStream, SerializationMode.Xml, configuration); } //open the plots foreach (PlotModel pm in Plots) { var viewModel = new PlotViewModel(pm); viewModel.ClosedAsync += PlotViewModel_ClosedAsync; ServiceLocator.Default.ResolveType <IUIVisualizerService>().Show(viewModel); } SaveConfigPath = path; this.ClearIsDirtyOnAllChilds(); this.IsDirty = false; ViewModelCommandManager.InvalidateCommands(true); LogTo.Info("Loaded Plot configuration from: {0}", SaveConfigPath); }
public void FluentSerialization_IdentifiesNullableProperties() { var conf = new SerializationConfiguration(); conf.ConfigureType <TestType>() .ConfigureProperty(x => x.NullableIntColumn, x => x.ExcludeFromSerialization = true) .ConfigureProperty(x => x.NullableDecColumn, x => x.ExcludeFromSerialization = true) .ConfigureProperty(x => x.NullableTimeColumn, x => x.ExcludeFromSerialization = true) .ConfigureProperty(x => x.NullableEnumColumn, x => x.ExcludeFromSerialization = true) ; var properties = conf.Types[typeof(TestType)].Properties; Assert.Contains(typeof(TestType).GetProperty("NullableIntColumn"), properties.Keys); Assert.Contains(typeof(TestType).GetProperty("NullableDecColumn"), properties.Keys); Assert.Contains(typeof(TestType).GetProperty("NullableTimeColumn"), properties.Keys); Assert.Contains(typeof(TestType).GetProperty("NullableEnumColumn"), properties.Keys); foreach (var propertyConfiguration in properties.Values) { Assert.True(propertyConfiguration.ExcludeFromSerialization); } }
public void DeserializeDateTimeAsTextAttribute() { var config = new SerializationConfiguration { DeserializeDateTimeAttributesAsString = true }; var serializerService = this.serializationFixture.BuildSerializerServiceWithConfig(config); var serialized = @" { ""id"": 1, ""key"": ""newKey"", ""attributes"": [ { ""name"": ""text-attribute"", ""value"": ""2021-10-12 05:50:06"" } ] } "; var deserialized = serializerService.Deserialize <ProductVariant>(serialized); Assert.IsAssignableFrom <Attribute <string> >(deserialized.Attributes[0]); }
/// <summary> /// Create a IStreamReader (reads binary data) from a given subregion of a byte buffer. /// </summary> public MemoryStreamReader(byte[] data, int start, int length, SerializationConfiguration config = null) { bytes = data; position = start; endPosition = length; SerializationConfiguration = config != null ? config : new SerializationConfiguration(); if (SerializationConfiguration.StreamLabelWidth == StreamLabelWidth.FourBytes) { readLabel = () => { return((StreamLabel)(uint)ReadInt32()); }; sizeOfSerializedStreamLabel = 4; } else { readLabel = () => { return((StreamLabel)(ulong)ReadInt64()); }; sizeOfSerializedStreamLabel = 8; } }
/// <summary> /// Create a new PinnedStreamReader that gets its data from a given file. You can optionally set the size of the read buffer. /// </summary> public PinnedStreamReader(string fileName, int bufferSize = defaultBufferSize, SerializationConfiguration config = null) : this(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete), bufferSize, config) { }
public static void Write <T>(System.IO.Stream outputStream, IEnumerable <T> values, out Footer footer, SerializationConfiguration serializationConfiguration = null) where T : class { var bufferFactory = new OrcCompressedBufferFactory(256 * 1024, CompressionKind.Zlib, CompressionStrategy.Size); var stripeWriter = new StripeWriter(typeof(T), outputStream, false, 0.8, 18, 6, bufferFactory, 10000, 512 * 1024 * 1024, serializationConfiguration); stripeWriter.AddRows(values); stripeWriter.RowAddingCompleted(); footer = stripeWriter.GetFooter(); outputStream.Seek(0, SeekOrigin.Begin); }
internal ConsumedMessageBuilder(SerializationConfiguration serializarionconfiguration, IMessageTypeResolver resolver) { _serializarionconfiguration = serializarionconfiguration; _resolver = resolver; }
/// <summary> /// Create a new IOStreamStreamReader from the given System.IO.Stream. Optionally you can specify the size of the read buffer /// The stream will be closed by the IOStreamStreamReader when it is closed. /// </summary> public IOStreamStreamReader(Stream inputStream, int bufferSize = defaultBufferSize, bool leaveOpen = false, SerializationConfiguration config = null, StreamReaderAlignment alignment = StreamReaderAlignment.EightBytes) : base(new byte[bufferSize + (int)alignment], 0, 0, config) { align = (int)alignment; Debug.Assert(bufferSize % align == 0); this.inputStream = inputStream; this.leaveOpen = leaveOpen; }
protected ViewQueryResponseRowsDeserializerTests(SerializationConfiguration serializationConfiguration) { SerializationConfiguration = serializationConfiguration; SUT = new DefaultSerializer(SerializationConfiguration); }
public void Serialize(Stream stream, SerializationConfiguration settings) { settings.Serializer.Serialize(stream, records.Select((x) => (x.Key.Key, x.Value))); }
/// <summary> /// Adds concrete implementations necessary for running of the application to the service collection for multiple client. /// </summary> /// <param name="services">The service collection.</param> /// <param name="configuration">The configuration.</param> /// <param name="clients">The clients with the client name as the key and the token flow as they value.</param> /// <param name="serializationConfiguration">The configuration of serialization services</param> public static IDictionary <string, IHttpClientBuilder> UseCommercetools(this IServiceCollection services, IConfiguration configuration, IDictionary <string, TokenFlow> clients, SerializationConfiguration serializationConfiguration = null) { services.UseRegistration(); services.UseLinq(); services.UseDomain(); services.UseSerialization(serializationConfiguration); return(services.UseHttpApi(configuration, clients)); }
/// <summary> /// Adds concrete implementations necessary for running of the application to the service collection for a single client. /// </summary> /// <param name="services">The service collection.</param> /// <param name="configuration">The configuration.</param> /// <param name="clientName">The name of the client.</param> /// <param name="tokenFlow">The token flow.</param> /// <param name="serializationConfiguration">The configuration of serialization services</param> public static IHttpClientBuilder UseCommercetools(this IServiceCollection services, IConfiguration configuration, string clientName = DefaultClientNames.Api, TokenFlow tokenFlow = TokenFlow.ClientCredentials, SerializationConfiguration serializationConfiguration = null) { var clients = new ConcurrentDictionary <string, TokenFlow>(); clients.TryAdd(clientName, tokenFlow); return(services.UseCommercetools(configuration, clients, serializationConfiguration).Single().Value); }
internal static object GetObject(Type t, byte[] bytes, SerializationConfiguration serializationConfig) { object obj; if (t == typeof(bool)) { obj = BitConverter.ToBoolean(bytes, 0); } else if (t == typeof(byte)) { obj = bytes[0]; } else if (t == typeof(string)) { obj = System.Text.Encoding.UTF8.GetString(bytes); } else if (t == typeof(char)) { obj = BitConverter.ToChar(bytes, 0); } else if (t == typeof(double)) { obj = BitConverter.ToDouble(bytes, 0); } else if (t == typeof(float)) { obj = BitConverter.ToSingle(bytes, 0); } else if (t == typeof(int)) { obj = BitConverter.ToInt32(bytes, 0); } else if (t == typeof(long)) { obj = BitConverter.ToInt64(bytes, 0); } else if (t == typeof(short)) { obj = BitConverter.ToInt16(bytes, 0); } else if (t == typeof(uint)) { obj = BitConverter.ToUInt32(bytes, 0); } else if (t == typeof(ulong)) { obj = BitConverter.ToUInt64(bytes, 0); } else if (t == typeof(ushort)) { obj = BitConverter.ToUInt16(bytes, 0); } else { try { if (serializationConfig != null) { if (serializationConfig.ContainsSerializationRule(t)) { return(serializationConfig.DeserializeAsObject(t, bytes)); } } using MemoryStream ms = new(bytes); obj = FORMATTER.Deserialize(ms); } catch (Exception) { obj = bytes; } } return(obj); }
public OrcWriter(Stream outputStream, WriterConfiguration configuration, SerializationConfiguration serializationConfiguration = null) { _underlyingOrcWriter = new OrcWriter(typeof(T), outputStream, configuration, serializationConfiguration); }
internal static T GetObject <T>(byte[] bytes, SerializationConfiguration serializationConfig) where T : new() { Type tType = typeof(T); if (tType == typeof(bool)) { return((T)Convert.ChangeType(BitConverter.ToBoolean(bytes, 0), typeof(T))); } if (tType == typeof(char)) { return((T)Convert.ChangeType(BitConverter.ToChar(bytes, 0), typeof(T))); } if (tType == typeof(double)) { return((T)Convert.ChangeType(BitConverter.ToDouble(bytes, 0), typeof(T))); } if (tType == typeof(float)) { return((T)Convert.ChangeType(BitConverter.ToSingle(bytes, 0), typeof(T))); } if (tType == typeof(Int32)) { return((T)Convert.ChangeType(BitConverter.ToInt32(bytes, 0), typeof(T))); } if (tType == typeof(Int64)) { return((T)Convert.ChangeType(BitConverter.ToInt64(bytes, 0), typeof(T))); } if (tType == typeof(Int16)) { return((T)Convert.ChangeType(BitConverter.ToInt16(bytes, 0), typeof(T))); } if (tType == typeof(UInt32)) { return((T)Convert.ChangeType(BitConverter.ToUInt32(bytes, 0), typeof(T))); } if (tType == typeof(UInt64)) { return((T)Convert.ChangeType(BitConverter.ToUInt64(bytes, 0), typeof(T))); } if (tType == typeof(UInt16)) { return((T)Convert.ChangeType(BitConverter.ToUInt16(bytes, 0), typeof(T))); } try { if (serializationConfig != null) { if (serializationConfig.ContainsSerializationRule <T>()) { return(serializationConfig.Get <T>().Deserialize(bytes)); } } using MemoryStream ms = new(); ms.Write(bytes, 0, bytes.Length); ms.Seek(0, SeekOrigin.Begin); return((T)FORMATTER.Deserialize(ms)); } catch (Exception) { throw new SerializationException($"Unable to deserialize stream into type '{tType}'" + " possibly the stream was not serialized using the internal serializer," + " in that case you have to write your own deserializer as well."); } }