first commit
This commit is contained in:
commit
d8c6f40442
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
bin
|
||||
obj
|
10
DevJsonDB.csproj
Normal file
10
DevJsonDB.csproj
Normal file
@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Library</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
22
DevJsonDB.sln
Normal file
22
DevJsonDB.sln
Normal file
@ -0,0 +1,22 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.0.31903.59
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DevJsonDB", "DevJsonDB.csproj", "{23D26ECE-BE59-48D5-9548-9E7C42BD85B4}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{23D26ECE-BE59-48D5-9548-9E7C42BD85B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{23D26ECE-BE59-48D5-9548-9E7C42BD85B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{23D26ECE-BE59-48D5-9548-9E7C42BD85B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{23D26ECE-BE59-48D5-9548-9E7C42BD85B4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
10
IO/IStore.cs
Normal file
10
IO/IStore.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace DevJsonDB.IO;
|
||||
|
||||
public interface IStore<T> : IDisposable
|
||||
{
|
||||
public void Commit();
|
||||
|
||||
public bool Add(T item);
|
||||
|
||||
public IEnumerable<T> GetValues();
|
||||
}
|
84
IO/LocalStore.cs
Normal file
84
IO/LocalStore.cs
Normal file
@ -0,0 +1,84 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace DevJsonDB.IO;
|
||||
|
||||
public class LocalStore<T> : IStore<T>
|
||||
{
|
||||
private static Encoding DefaultEncoding = Encoding.UTF8;
|
||||
private string filePath;
|
||||
private List<T> items;
|
||||
|
||||
public LocalStore(string filePath)
|
||||
{
|
||||
this.filePath = filePath;
|
||||
this.items = new List<T>();
|
||||
|
||||
this.InitDatabase();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
this.Commit();
|
||||
}
|
||||
|
||||
public void Commit()
|
||||
{
|
||||
try
|
||||
{
|
||||
using (FileStream fs = new FileStream(this.filePath, FileMode.OpenOrCreate))
|
||||
using (StreamWriter writer = new StreamWriter(fs, DefaultEncoding))
|
||||
{
|
||||
foreach (T item in this.items)
|
||||
{
|
||||
string json = JsonSerializer.Serialize<T>(item);
|
||||
writer.WriteLine(json);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new JsonDBException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Add(T item)
|
||||
{
|
||||
if (this.items.Contains(item))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
this.items.Add(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
public IEnumerable<T> GetValues()
|
||||
{
|
||||
return this.items.ToList();
|
||||
}
|
||||
|
||||
private void InitDatabase()
|
||||
{
|
||||
try
|
||||
{
|
||||
using (FileStream fs = new FileStream(this.filePath, FileMode.OpenOrCreate))
|
||||
using (StreamReader reader = new StreamReader(fs, DefaultEncoding))
|
||||
{
|
||||
string? line = null;
|
||||
while ((line = reader.ReadLine()) != null)
|
||||
{
|
||||
T? value = JsonSerializer.Deserialize<T>(line);
|
||||
if (value is object)
|
||||
{
|
||||
this.items.Add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new JsonDBException(ex.Message, ex);
|
||||
}
|
||||
}
|
||||
}
|
38
JsonDB.cs
Normal file
38
JsonDB.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using DevJsonDB.IO;
|
||||
|
||||
namespace DevJsonDB;
|
||||
|
||||
public class JsonDB<T> : IDisposable
|
||||
{
|
||||
private IStore<T> store;
|
||||
|
||||
public JsonDB(IStore<T> store)
|
||||
{
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
this.store.Dispose();
|
||||
}
|
||||
|
||||
public void Commit()
|
||||
{
|
||||
this.store.Commit();
|
||||
}
|
||||
|
||||
public bool Add(T item)
|
||||
{
|
||||
return this.store.Add(item);
|
||||
}
|
||||
|
||||
public IEnumerable<T> GetValues()
|
||||
{
|
||||
return this.store.GetValues();
|
||||
}
|
||||
|
||||
public IEnumerable<T> GetValues(Func<T, bool> filter)
|
||||
{
|
||||
return this.store.GetValues().Where(filter).ToList();
|
||||
}
|
||||
}
|
8
JsonDBException.cs
Normal file
8
JsonDBException.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace DevJsonDB;
|
||||
|
||||
public class JsonDBException : Exception
|
||||
{
|
||||
public JsonDBException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
12
JsonDBFactory.cs
Normal file
12
JsonDBFactory.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using DevJsonDB.IO;
|
||||
|
||||
namespace DevJsonDB;
|
||||
|
||||
public static class JsonDBFactory
|
||||
{
|
||||
public static JsonDB<T> CreateDB<T>(string filePath)
|
||||
{
|
||||
IStore<T> store = new LocalStore<T>(filePath);
|
||||
return new JsonDB<T>(store);
|
||||
}
|
||||
}
|
9
LICENSE
Normal file
9
LICENSE
Normal file
@ -0,0 +1,9 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright 2024 Sakurai Ryota
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
31
README.md
Normal file
31
README.md
Normal file
@ -0,0 +1,31 @@
|
||||
# JsonDB
|
||||
|
||||
## 概要
|
||||
|
||||
シンプルなJSONL入出力をサポートするC#ライブラリです。
|
||||
|
||||
## 使い方
|
||||
|
||||
`JsonDBFactory`を使用し、DBを初期化します。
|
||||
|
||||
```csharp
|
||||
// DBを初期化します。
|
||||
string filePath = "fileName.jsonl";
|
||||
JsonDB<T> jsonDB = JsonDBFactory.CreateDB<T>(filePath);
|
||||
|
||||
// DBにアイテムを追加します。
|
||||
bool result = jsonDB.Add(new T());
|
||||
|
||||
// DBから全アイテムを取得します。
|
||||
IEnumerable<T> allItems = jsonDB.GetValues();
|
||||
|
||||
// DBから特定のアイテムを取得します。
|
||||
IEnumerable<T> filteredItems = jsonDB.GetValues((T) => true):
|
||||
|
||||
// ファイルに書き込む
|
||||
jsonDB.Commit();
|
||||
|
||||
// リソースを破棄する
|
||||
jsonDB.Dispose();
|
||||
|
||||
```
|
Loading…
Reference in New Issue
Block a user