Exemplo n.º 1
0
        public void TestJsonSerializationStrategyWithCustomSettings()
        {
            var customSettings = new JsonSerializerSettings
            {
                Formatting        = Formatting.None,
                NullValueHandling = NullValueHandling.Ignore,
                ContractResolver  = new DefaultContractResolver
                {
                    NamingStrategy = new CamelCaseNamingStrategy()
                }
            };

            var strategy = new JsonSerializationStrategy(customSettings);

            var serializeRequest = new SecretAgent {
                Id = "007", Name = "Bond. James Bond"
            };

            var serializeResponse = strategy.Serialize(serializeRequest);
            var expectedResponse  = "{\"name\":\"Bond. James Bond\",\"id\":\"007\"}";

            serializeResponse.Equals(expectedResponse).Should().BeTrue();

            var deserializeRequest  = serializeResponse;
            var deserializeResponse = strategy.Deserialize <SecretAgent>(deserializeRequest);

            deserializeResponse.Should().NotBeNull();
            deserializeResponse.Name.Should().Be("Bond. James Bond");
            deserializeResponse.Id.Should().Be("007");
        }
        protected override void CoreWriteObject(object obj, string objectName)
        {
            IXmlObject xmlObject;
            ITextSerializationStrategy serializationStrategy;

            if ((object)obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            xmlObject = obj as IXmlObject;

            // this should support XPE, XML, JSON

            /*
             *      BACKLOG([email protected] / 2015 - 12 - 18):
             *      Refactor this logic that is common between this File and TextWriter Outputs.
             */
            if ((object)xmlObject != null)
            {
                serializationStrategy = new XpeSerializationStrategy(this.Xpe);
            }
            else if ((object)SolderFascadeAccessor.ReflectionFascade.GetOneAttribute <XmlRootAttribute>(obj.GetType()) != null)
            {
                serializationStrategy = new XmlSerializationStrategy();
            }
            else
            {
                serializationStrategy = new JsonSerializationStrategy();
            }

            serializationStrategy.SetObjectToWriter(this.CurrentTextWriter, obj);
        }
Exemplo n.º 3
0
        public override void Configure(IKernel kernel)
        {
            // this relies on specific Area binding order, as set in Bootstrapper.
            // todo: remove once current persistence is deprecated.

            kernel.Bind <PersistenceManager>().ToMethod(context =>
            {
                var dataDir = kernel.Get <IWurmAssistantDataDirectory>();
                var logger  = kernel.Get <ILogger>();

                var config = new PersistenceManagerConfig()
                {
                    DataStoreDirectoryPath = Path.Combine(dataDir.DirectoryPath, "Data")
                };
                var errorStrategy         = new JsonExtendedErrorHandlingStrategy(logger);
                var serializationStrategy = new JsonSerializationStrategy
                {
                    ErrorStrategy = errorStrategy
                };
                var persistenceManager = new PersistenceManager(config,
                                                                serializationStrategy,
                                                                new FlatFilesPersistenceStrategy(config));

                return(persistenceManager);
            }).InSingletonScope();

            kernel.Bind <PersistenceEnabler>().ToSelf().InSingletonScope();

            kernel.Bind <IPersistentObjectResolver>().To <PersistentObjectResolver>().InSingletonScope();
            kernel.Bind(typeof(IPersistentObjectResolver <>)).To(typeof(PersistentObjectResolver <>)).InSingletonScope();

            kernel.Bind <ISerializer>().To <DefaultJsonSerializer>();
        }
Exemplo n.º 4
0
		public static Configuration FromJsonFile(string jsonFile)
		{
			Configuration configuration;

			configuration = new JsonSerializationStrategy().GetObjectFromFile<Configuration>(jsonFile);

			return configuration;
		}
        public ObfuscationConfiguration GetObfuscationConfiguration()
        {
            ObfuscationConfiguration obfuscationConfiguration;

            if (DataTypeFascade.Instance.IsNullOrWhiteSpace(this.ObfuscationConfigurationJsonText))
            {
                obfuscationConfiguration = new ObfuscationConfiguration()
                {
                    ConfigurationVersion = ObfuscationConfiguration.CurrentConfigurationVersion,
                    EngineVersion        = ObfuscationConfiguration.CurrentEngineVersion
                }
            }
            ;
            else
            {
                obfuscationConfiguration = new JsonSerializationStrategy().GetObjectFromString <ObfuscationConfiguration>(this.ObfuscationConfigurationJsonText);
            }

            if ((object)obfuscationConfiguration != null)
            {
                if ((object)obfuscationConfiguration.SourceAdapterConfiguration != null)
                {
                    obfuscationConfiguration.SourceAdapterConfiguration.ResetAdapterSpecificConfiguration();
                    obfuscationConfiguration.SourceAdapterConfiguration.AdapterAqtn = typeof(DtsSourceAdapter).AssemblyQualifiedName;
                }

                if ((object)obfuscationConfiguration.DestinationAdapterConfiguration != null)
                {
                    obfuscationConfiguration.DestinationAdapterConfiguration.ResetAdapterSpecificConfiguration();
                    obfuscationConfiguration.DestinationAdapterConfiguration.AdapterAqtn = typeof(DtsDestinationAdapter).AssemblyQualifiedName;
                }

                if ((object)obfuscationConfiguration.DictionaryConfigurations != null)
                {
                    foreach (DictionaryConfiguration dictionaryConfiguration in obfuscationConfiguration.DictionaryConfigurations)
                    {
                        if ((object)dictionaryConfiguration.DictionaryAdapterConfiguration != null)
                        {
                            var items = dictionaryConfiguration.DictionaryAdapterConfiguration.AdapterSpecificConfiguration.Select(kvp => new { KEY = kvp.Key, VAL = kvp.Value }).ToArray();

                            dictionaryConfiguration.DictionaryAdapterConfiguration.ResetAdapterSpecificConfiguration();
                            dictionaryConfiguration.DictionaryAdapterConfiguration.AdapterAqtn = typeof(DtsDictionaryAdapter).AssemblyQualifiedName;

                            foreach (var item in items)
                            {
                                dictionaryConfiguration.DictionaryAdapterConfiguration.AdapterSpecificConfiguration.Add(item.KEY, item.VAL);
                            }

                            dictionaryConfiguration.DictionaryAdapterConfiguration.AdapterSpecificConfiguration.Add("DictionaryUnitOfWorkCallback", this.DictionaryUnitOfWorkCallback);
                        }
                    }
                }
            }

            return(obfuscationConfiguration);
        }
Exemplo n.º 6
0
        private static TConfiguration FromJsonFile <TConfiguration>(string jsonFilePath)
            where TConfiguration : class, IConfigurationObject, new()
        {
            TConfiguration         configuration;
            ISerializationStrategy serializationStrategy;

            serializationStrategy = new JsonSerializationStrategy();
            configuration         = serializationStrategy.GetObjectFromFile <TConfiguration>(jsonFilePath);

            return(configuration);
        }
        PersistenceManager CreatePersistenceManager(JsonSerializationStrategy ss = null)
        {
            if (ss == null)
            {
                ss = new JsonSerializationStrategy();
            }
            var cfg = new PersistenceManagerConfig()
            {
                DataStoreDirectoryPath = TempDir.FullName
            };
            var m = new PersistenceManager(cfg, ss, new FlatFilesPersistenceStrategy(cfg));

            return(m);
        }
Exemplo n.º 8
0
        protected override void CoreWriteObject(object obj, string objectName)
        {
            string                 fullFilePath;
            IXmlObject             xmlObject;
            ISerializationStrategy serializationStrategy;

            if ((object)obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if ((object)objectName == null)
            {
                throw new ArgumentNullException(nameof(objectName));
            }

            if (SolderFascadeAccessor.DataTypeFascade.IsWhiteSpace(objectName))
            {
                throw new ArgumentOutOfRangeException(nameof(objectName));
            }

            fullFilePath = Path.GetFullPath(Path.Combine(this.BaseDirectoryPath, objectName));
            xmlObject    = obj as IXmlObject;

            // this should support XPE, XML, JSON

            /*
             *      BACKLOG([email protected] / 2015 - 12 - 18):
             *      Refactor this logic that is common between this File and TextWriter Outputs.
             */
            if ((object)xmlObject != null)
            {
                serializationStrategy = new XpeSerializationStrategy(this.Xpe);
            }
            else if ((object)SolderFascadeAccessor.ReflectionFascade.GetOneAttribute <XmlRootAttribute>(obj.GetType()) != null)
            {
                serializationStrategy = new XmlSerializationStrategy();
            }
            else
            {
                serializationStrategy = new JsonSerializationStrategy();
            }

            serializationStrategy.SetObjectToFile(fullFilePath, obj);
        }
Exemplo n.º 9
0
        public void TestJsonSerializationStrategyWithDefaultSettings()
        {
            var strategy = new JsonSerializationStrategy();

            var serializeRequest = new SecretAgent {
                Id = "007", Name = "Bond. James Bond"
            };

            var serializeResponse = strategy.Serialize(serializeRequest);
            var expectedResponse  = "{\"Name\":\"Bond. James Bond\",\"Id\":\"007\",\"Drinks\":null}";

            serializeResponse.Equals(expectedResponse).Should().BeTrue();

            var deserializeRequest  = serializeResponse;
            var deserializeResponse = strategy.Deserialize <SecretAgent>(deserializeRequest);

            deserializeResponse.Should().NotBeNull();
            deserializeResponse.Name.Should().Be("Bond. James Bond");
            deserializeResponse.Id.Should().Be("007");
        }
        public void CustomErrorHandlerOverrides()
        {
            {
                var o  = new SimpleObj("1");
                var pm = CreatePersistenceManager();
                pm.LoadAndStartTracking(o);
                o.Data = "test";
                pm.SaveAll();
            }

            {
                //bool invoked = false;
                var oInt     = new SimpleObjWithIntData("1");
                var strategy = new CustomErrorHandlingStrategy();
                var ss       = new JsonSerializationStrategy()
                {
                    ErrorStrategy = strategy
                };
                var pm = CreatePersistenceManager(ss);
                pm.LoadAndStartTracking(oInt);
                Expect(oInt.Data, EqualTo(SimpleObjWithIntData.DataDefaultValue));
                Expect(strategy.Invoked, True);
            }
        }
Exemplo n.º 11
0
		protected override void CoreWriteObject(object obj, string objectName)
		{
			IXmlObject xmlObject;
			ITextSerializationStrategy serializationStrategy;

			if ((object)obj == null)
				throw new ArgumentNullException(nameof(obj));

			xmlObject = obj as IXmlObject;

			// this should support XPE, XML, JSON
			/*
				BACKLOG([email protected] / 2015 - 12 - 18):
				Refactor this logic that is common between this File and TextWriter Outputs.
			*/
			if ((object)xmlObject != null)
				serializationStrategy = new XpeSerializationStrategy(this.Xpe);
			else if ((object)SolderLegacyInstanceAccessor.ReflectionFascadeLegacyInstance.GetOneAttribute<XmlRootAttribute>(obj.GetType()) != null)
				serializationStrategy = new XmlSerializationStrategy();
			else
				serializationStrategy = new JsonSerializationStrategy();

			serializationStrategy.SetObjectToWriter(this.CurrentTextWriter, obj);
		}
Exemplo n.º 12
0
		protected override void CoreWriteObject(object obj, string objectName)
		{
			string fullFilePath;
			IXmlObject xmlObject;
			ISerializationStrategy serializationStrategy;

			if ((object)obj == null)
				throw new ArgumentNullException(nameof(obj));

			if ((object)objectName == null)
				throw new ArgumentNullException(nameof(objectName));

			if (SolderLegacyInstanceAccessor.DataTypeFascadeLegacyInstance.IsWhiteSpace(objectName))
				throw new ArgumentOutOfRangeException(nameof(objectName));

			fullFilePath = Path.GetFullPath(Path.Combine(this.BaseDirectoryPath, objectName));
			xmlObject = obj as IXmlObject;

			// this should support XPE, XML, JSON
			/*
				BACKLOG([email protected] / 2015 - 12 - 18):
				Refactor this logic that is common between this File and TextWriter Outputs.
			*/
			if ((object)xmlObject != null)
				serializationStrategy = new XpeSerializationStrategy(this.Xpe);
			else if ((object)SolderLegacyInstanceAccessor.ReflectionFascadeLegacyInstance.GetOneAttribute<XmlRootAttribute>(obj.GetType()) != null)
				serializationStrategy = new XmlSerializationStrategy();
			else
				serializationStrategy = new JsonSerializationStrategy();

			serializationStrategy.SetObjectToFile(fullFilePath, obj);
		}