예제 #1
0
        public IContext LoadContext(Type contextType)
        {
            JsonInit.InitConverter();

            IConfigProvider configProvider = Provider.Get();
            IConfigSection  section        = configProvider.LoadSingleSetting(SectionId, contextType.FullName);

            if (section.ContainsSetting(contextType.FullName))
            {
                var jsonString = section.GetSetting <string>(contextType.FullName, null).Value;
                try
                {
                    if (!string.IsNullOrEmpty(jsonString))
                    {
                        return((IContext)JsonConvert.DeserializeObject(jsonString, contextType));
                    }
                    return(null);
                }
                catch (JsonSerializationException e)
                {
                    throw new JsonSerializationException(string.Format("Error deserializing type {0}", contextType), e);
                }
            }
            return(null);
        }
예제 #2
0
        public void SaveContext(IContext context)
        {
            JsonInit.InitConverter();

            IConfigProvider configProvider = Provider.Get();
            IConfigSection  section        = configProvider.LoadSingleSetting(SectionId, context.GetType().FullName);

            section.GetSetting <string>(context.GetType().FullName, null, true).Value =
                JsonConvert.SerializeObject(context);
            configProvider.SaveSection(section);
        }
예제 #3
0
        public void SaveContext(IContext context)
        {
            string filename = Path.Combine(_path, GetJsonFilename(context.GetType()));

            using (StreamWriter file = File.CreateText(filename))
            {
                var serializer = new JsonSerializer();
                JsonInit.InitSerializer(serializer);
                serializer.Serialize(file, context);
            }
        }
예제 #4
0
        public IContext LoadContext(Type contextType)
        {
            string filename = Path.Combine(_path, GetJsonFilename(contextType));

            if (File.Exists(filename))
            {
                using (StreamReader file = File.OpenText(filename))
                {
                    var serializer = new JsonSerializer();
                    JsonInit.InitSerializer(serializer);
                    try
                    {
                        return((IContext)serializer.Deserialize(file, contextType));
                    }
                    catch (JsonSerializationException e)
                    {
                        throw new JsonSerializationException(
                                  string.Format("Error deserializing type {0}", contextType), e);
                    }
                }
            }

            return(null);
        }