Beispiel #1
0
        internal static void SaveToJson(ChatStorage storage)
        {
            var _path = GetDirectoryPath(storage.Chat);

            if (!Directory.Exists(_path))
            {
                Directory.CreateDirectory(_path);
            }

            var settings = new JsonSerializerSettings();

            settings.NullValueHandling     = NullValueHandling.Ignore;
            settings.DefaultValueHandling  = DefaultValueHandling.IgnoreAndPopulate;
            settings.MissingMemberHandling = MissingMemberHandling.Error;
            settings.Formatting            = Formatting.Indented;

            storage.Variables.SerializeAllCache();
            storage.UsersStorage.SerializeAllCache();

            storage.UsersStorage.RemoveAllEmpty();

            string path = GetFullFilePath(storage.Chat);
            string json = JsonConvert.SerializeObject(storage, settings);

            if (storage._cache == json)
            {
                return;
            }
            storage._cache = json;

            File.WriteAllText(path, json);
        }
Beispiel #2
0
        internal static ChatStorage ReadFromJson(Chat chat)
        {
            string      path    = GetFullFilePath(chat);
            ChatStorage storage = null;

            if (File.Exists(path))
            {
                var settings = new JsonSerializerSettings();
                settings.NullValueHandling     = NullValueHandling.Ignore;
                settings.DefaultValueHandling  = DefaultValueHandling.IgnoreAndPopulate;
                settings.MissingMemberHandling = MissingMemberHandling.Error;
                settings.Formatting            = Formatting.Indented;

                string json = File.ReadAllText(path);

                storage        = JsonConvert.DeserializeObject <ChatStorage>(json, settings);
                storage.Chat   = chat;
                storage._cache = json;
            }

            return(storage ?? new ChatStorage()
            {
                Chat = chat
            });
        }