{"id":6887,"date":"2022-10-25T11:08:15","date_gmt":"2022-10-25T10:08:15","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=6887"},"modified":"2022-10-25T11:08:15","modified_gmt":"2022-10-25T10:08:15","slug":"c-simple-json-persistent-store","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-simple-json-persistent-store\/","title":{"rendered":"C# Simple Json Persistent Store"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">using System;\r\nusing System.Collections.Generic;\r\nusing System.IO;\r\nusing System.Threading;\r\nusing Newtonsoft.Json;\r\n\r\nnamespace Helpers\r\n{\r\n    public class JsonPersistentStore&lt;T>\r\n    {\r\n        private readonly string FileName;\r\n        private readonly SemaphoreSlim Semaphore = new SemaphoreSlim(1, 1);\r\n        public readonly List&lt;T> Items = new List&lt;T>();\r\n\r\n        public JsonPersistentStore(string fileName)\r\n        {\r\n            FileName = fileName;\r\n        }\r\n\r\n        public void Load()\r\n        {\r\n            if (!File.Exists(FileName)) return;\r\n            var json = File.ReadAllText(FileName);\r\n            try\r\n            {\r\n                Items.AddRange(JsonConvert.DeserializeObject&lt;IEnumerable&lt;T>>(json));\r\n            }\r\n            catch (Exception ex)\r\n            {\r\n                DomainPorts.Logger.Warning($\"Failed to load items from file: {FileName}\", ex);\r\n            }\r\n        }\r\n\r\n        public void Save()\r\n        {\r\n            Semaphore.Wait();\r\n            try\r\n            {\r\n                var json = JsonConvert.SerializeObject(Items);\r\n                File.WriteAllText(FileName, json);\r\n            }\r\n            catch (Exception ex)\r\n            {\r\n                DomainPorts.Logger.Warning($\"Failed to save items to file: {FileName}\", ex);\r\n            }\r\n            finally\r\n            {\r\n                Semaphore.Release();\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6887","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6887","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=6887"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6887\/revisions"}],"predecessor-version":[{"id":6888,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/6887\/revisions\/6888"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=6887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=6887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=6887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}