private ICompositeStorage TrySerializeInternal(object input, ISerializerContext context) { Ensure.NotNull(input, "input"); Ensure.NotNull(context, "context"); ICompositeModel model = input as ICompositeModel; if (model == null) { return(null); } ICompositeStorage storage = storageFactory.Create(); if (!typeNameMapper.TryGet(model.GetType(), out string typeName)) { return(null); } storage.Add(Name.TypeName, typeName); ICompositeStorage childStorage = storage.Add(Name.Payload); model.Save(childStorage); return(storage); }
private KeyValuePair <long, long> CompositeStorage(int count) { Stopwatch streamSw = new Stopwatch(); Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < count; i++) { ICompositeStorage storage = new JsonCompositeStorage(); storage.Add("Name", "Test.UserModel"); storage.Add("Version", 1); ICompositeStorage payloadStorage = storage.Add("Payload"); payloadStorage.Add("FirstName", "John"); payloadStorage.Add("LastName", "Doe"); payloadStorage.Add("Direction", ListSortDirection.Descending); payloadStorage.Add("IDs", new int[] { 1, 2, 3 }); payloadStorage.Add("Keys", new List <int>() { 4, 5, 6 }); streamSw.Start(); using (MemoryStream stream = new MemoryStream()) { storage.StoreAsync(stream).Wait(); stream.Seek(0, SeekOrigin.Begin); string json = null; using (StreamReader reader = new StreamReader(stream, Encoding.UTF8, true, 1024, true)) json = reader.ReadToEnd(); stream.Seek(0, SeekOrigin.Begin); storage.LoadAsync(stream).Wait(); } streamSw.Stop(); string value; storage.TryGet("Name", out value); int version; storage.TryGet("Version", out version); storage.TryGet("Payload", out payloadStorage); payloadStorage.TryGet("FirstName", out value); payloadStorage.TryGet("LastName", out value); ListSortDirection direction; Assert.AreEqual(true, payloadStorage.TryGet("Direction", out direction)); int[] ids; payloadStorage.TryGet("IDs", out ids); List <int> keys; payloadStorage.TryGet("Keys", out keys); } sw.Stop(); return(new KeyValuePair <long, long>(sw.ElapsedMilliseconds, streamSw.ElapsedMilliseconds)); }
private void SaveItem(ICompositeStorage storage, AdditionalApplicationModel model) { storage.Add("Name", model.Name); storage.Add("Path", model.Path); storage.Add("Arguments", model.Arguments); if (model.HotKey != Key.None) { storage.Add("HotKey", model.HotKey); } SaveCollection(storage, model.Commands); }
public ICompositeStorage Add(string key, object value) { log.Debug($"Add: Key: '{key}', ValueType: '{value?.GetType()?.FullName}', Value: '{value}'."); if (value == null) { storage[key] = null; } else if (value is GuidKey guidKey) { ICompositeStorage inner = Add(key); inner.Add("Type", guidKey.Type); if (guidKey.IsEmpty) { inner.Add("Guid", null); } else { inner.Add("Guid", guidKey.Guid.ToString()); } } else if (value is StringKey stringKey) { ICompositeStorage inner = Add(key); inner.Add("Type", stringKey.Type); if (stringKey.IsEmpty) { inner.Add("Identifier", null); } else { inner.Add("Identifier", stringKey.Identifier.ToString()); } } else if (value is Color color) { storage[key] = color.A + ";" + color.R + ";" + color.G + ";" + color.B; } else if (value is Price price) { ICompositeStorage inner = Add(key); inner.Add("Value", price.Value); inner.Add("Currency", price.Currency); } else { storage[key] = value; } return(this); }
private void SaveCollection(ICompositeStorage storage, IReadOnlyList <AdditionalApplicationModel> models) { if (models == null || models.Count == 0) { return; } storage.Add("Count", models.Count); for (int i = 0; i < models.Count; i++) { AdditionalApplicationModel model = models[i]; ICompositeStorage itemStorage = storage.Add(i.ToString()); SaveItem(itemStorage, model); } }
protected override bool TryStore(object input, ISerializerContext context, CompositeType type, CompositeVersion typeVersion, ICompositeStorage storage) { if (base.TryStore(input, context, type, typeVersion, storage)) { ICompositeStorage payloadStorage = GetOrAddPayloadStorage(storage); foreach (ICompositeFormatterExtender extender in extenders) { extender.Store(payloadStorage, input); } IReadOnlyKeyValueCollection metadata; if (context.TryGetEnvelopeMetadata(out metadata)) { ICompositeStorage metadataStorage = storage.Add(MetadataKey); foreach (string key in metadata.Keys) { metadataStorage.Add(key, metadata.Get <object>(key)); } } return(true); } return(false); }
protected override bool TryStore(object input, ISerializerContext context, CompositeType type, CompositeVersion typeVersion, ICompositeStorage storage) { if (base.TryStore(input, context, type, typeVersion, storage)) { if (input is AggregateRootException e) { ICompositeStorage inner = storage.Add(Name.InnerStorage); inner.Add(Name.AggregateRootKey, e.Key); inner.Add(Name.CommandKey, e.CommandKey); inner.Add(Name.SourceCommandKey, e.SourceCommandKey); } return(true); } return(false); }
/// <summary> /// Tries to store the <paramref name="input"/> to the <paramref name="context"/>. The <paramref name="input"/> is described by the <paramref name="type"/> /// and in the <paramref name="typeVersion"/>. /// </summary> /// <param name="input">The object to store.</param> /// <param name="context">The serialization context.</param> /// <param name="type">The <paramref name="input"/> composite type descriptor.</param> /// <param name="typeVersion">The version of the <paramref name="input"/>.</param> /// <param name="storage">The composite storage to store values to.</param> /// <returns><c>true</c> if the <paramref name="input"/> was serialized to the <paramref name="context"/>; <c>false</c> otherwise.</returns> protected virtual bool TryStore(object input, ISerializerContext context, CompositeType type, CompositeVersion typeVersion, ICompositeStorage storage) { storage.Add(Name.TypeName, type.Name); storage.Add(Name.Version, type.VersionProperty.Getter(input)); ICompositeStorage valueStorage = storage.Add(Name.Payload); foreach (CompositeProperty property in typeVersion.Properties) { object propertyValue = property.Getter(input); if (!TryStoreValue(valueStorage, property.Name, propertyValue)) { throw new NotSupportedValueException(type.Type, property.Name, property.Type, propertyValue); } } return(true); }
private ICompositeStorage GetOrAddPayloadStorage(ICompositeStorage storage) { ICompositeStorage payloadStorage; if (storage.TryGet(CompositeTypeFormatter.Name.Payload, out payloadStorage)) { return(payloadStorage); } return(storage.Add(CompositeTypeFormatter.Name.Payload)); }
protected override bool TryStore(object input, ISerializerContext context, CompositeType type, CompositeVersion typeVersion, ICompositeStorage storage) { if (input is IList list) { storage.Add("Count", list.Count); for (int i = 0; i < list.Count; i++) { object item = list[i]; ICompositeStorage innerStorage = storage.Add(i.ToString()); if (!base.TryStore(item, context, type, typeVersion, innerStorage)) { return(false); } } return(true); } return(base.TryStore(input, context, type, typeVersion, storage)); }
/// <summary> /// Tries to store hte <paramref name="value"/> to the <paramref name="storage"/> with the <paramref name="key"/>. /// </summary> /// <param name="storage">The composite storage to store the <paramref name="value"/> to.</param> /// <param name="key">The key to associate the <paramref name="value"/> to.</param> /// <param name="value">The value to store.</param> /// <returns><c>true</c> if the <paramref name="value"/> was stored to the <paramref name="storage"/>; <c>false</c> otherwise.</returns> protected virtual bool TryStoreValue(ICompositeStorage storage, string key, object value) { storage.Add(key, value); return(true); }
public void Save(ICompositeStorage storage) { storage.Add("ConfigurationVersion", 1); storage.Add("FileSearchPattern", FileSearchPattern); storage.Add("IsAutoSelectApplicationVersion", IsAutoSelectApplicationVersion); storage.Add("IsDismissedWhenLostFocus", IsDismissedWhenLostFocus); storage.Add("IsDisplayedPathTrimmedToLastFolderName", IsDisplayedPathTrimmedToLastFolderName); storage.Add("IsFileNameRemovedFromDisplayedPath", IsFileNameRemovedFromDisplayedPath); storage.Add("IsFileSearchPatternSaved", IsFileSearchPatternSaved); storage.Add("IsHiddentOnStartup", IsHiddentOnStartup); storage.Add("IsLastUsedApplicationSavedAsPrefered", IsLastUsedApplicationSavedAsPrefered); storage.Add("IsProjectCountEnabled", IsProjectCountEnabled); storage.Add("IsStatisticsCounted", IsStatisticsCounted); storage.Add("IsTrayIcon", IsTrayIcon); storage.Add("PinnedFiles", PinnedFiles); storage.Add("PositionTop", PositionTop); storage.Add("PositionLeft", PositionLeft); storage.Add("PositionMode", PositionMode); storage.Add("PreferedApplicationPath", PreferedApplicationPath); storage.Add("RunKey", RunKey); storage.Add("SourceDirectoryPath", SourceDirectoryPath); storage.Add("ThemeMode", ThemeMode); AdditionalApplications.Save(storage.Add("AdditionalApplications")); storage.Add("HiddenMainApplications", HiddenMainApplications); storage.Add("FileSearchCount", FileSearchCount); storage.Add("FileSearchMode", FileSearchMode); storage.Add("AutoSelectApplicationMinimalVersion", AutoSelectApplicationMinimalVersion); storage.Add("LogLevel", LogLevel); }
public void Base() { Converts.Repository .AddJsonEnumSearchHandler() .AddJsonPrimitivesSearchHandler() .AddJsonObjectSearchHandler(); ICompositeStorage storage = new JsonCompositeStorage(); storage.Add("Name", "Test.UserModel"); storage.Add("Version", 1); ICompositeStorage payloadStorage = storage.Add("Payload"); payloadStorage.Add("FirstName", "John"); payloadStorage.Add("LastName", "Doe"); payloadStorage.Add("Direction", ListSortDirection.Descending); payloadStorage.Add("IDs", new int[] { 1, 2, 3 }); payloadStorage.Add("Keys", new List <int>() { 4, 5, 6 }); using (MemoryStream stream = new MemoryStream()) { storage.StoreAsync(stream).Wait(); stream.Seek(0, SeekOrigin.Begin); string json = null; using (StreamReader reader = new StreamReader(stream, Encoding.UTF8, true, 1024, true)) json = reader.ReadToEnd(); stream.Seek(0, SeekOrigin.Begin); storage.LoadAsync(stream).Wait(); } string value; Assert.AreEqual(true, storage.TryGet("Name", out value)); Assert.AreEqual("Test.UserModel", value); int version; Assert.AreEqual(true, storage.TryGet("Version", out version)); Assert.AreEqual(1, version); Assert.AreEqual(true, storage.TryGet("Payload", out payloadStorage)); Assert.AreEqual(true, payloadStorage.TryGet("FirstName", out value)); Assert.AreEqual("John", value); Assert.AreEqual(true, payloadStorage.TryGet("LastName", out value)); Assert.AreEqual("Doe", value); ListSortDirection direction; Assert.AreEqual(true, payloadStorage.TryGet("Direction", out direction)); Assert.AreEqual(ListSortDirection.Descending, direction); int[] ids; Assert.AreEqual(true, payloadStorage.TryGet("IDs", out ids)); Assert.IsNotNull(ids); Assert.AreEqual(1, ids[0]); Assert.AreEqual(2, ids[1]); Assert.AreEqual(3, ids[2]); List <int> keys; Assert.AreEqual(true, payloadStorage.TryGet("Keys", out keys)); Assert.IsNotNull(ids); Assert.AreEqual(4, keys[0]); Assert.AreEqual(5, keys[1]); Assert.AreEqual(6, keys[2]); }