在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:ttu/json-flatfile-datastore开源软件地址:https://github.com/ttu/json-flatfile-datastore开源编程语言:C# 100.0%开源软件介绍:JSON Flat File Data Store
Simple data store that saves the data in JSON format to a single file.
InstallationYou can install the latest version via NuGet. # .NET Core CLI
$ dotnet add package JsonFlatFileDataStore
# Package Manager Console
PM> Install-Package JsonFlatFileDataStore Example projectFake JSON Server is an ASP.NET Core Web App which uses JSON Flat File Data Store with dynamic data. ExampleTyped datapublic class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
// Open database (create new if file doesn't exist)
var store = new DataStore("data.json");
// Get employee collection
var collection = store.GetCollection<Employee>();
// Create new employee instance
var employee = new Employee { Id = 1, Name = "John", Age = 46 };
// Insert new employee
// Id is updated automatically to correct next value
await collection.InsertOneAsync(employee);
// Update employee
employee.Name = "John Doe";
await collection.UpdateOneAsync(employee.Id, employee);
// Use LINQ to query items
var results = collection.AsQueryable().Where(e => e.Age > 30);
// Save instance as a single item
await store.InsertItemAsync("selected_employee", employee);
// Single items can be of any type
await store.InsertItemAsync("counter", 1);
var counter = await store.GetItem<int>("counter"); Dynamically typed dataDynamic data can be // Open database (create new if file doesn't exist)
var store = new DataStore(pathToJson);
// Get employee collection
var collection = store.GetCollection("employee");
// Create new employee
var employee = new { id = 1, name = "John", age = 46 };
// Create new employee from JSON
var employeeJson = JToken.Parse("{ 'id': 2, 'name': 'Raymond', 'age': 32 }");
// Create new employee from dictionary
var employeeDict = new Dictionary<string, object>
{
["id"] = 3,
["name"] = "Andy",
["age"] = 32
};
// Insert new employee
// Id is updated automatically if object is updatable
await collection.InsertOneAsync(employee);
await collection.InsertOneAsync(employeeJson);
await collection.InsertOneAsync(employeeDict);
// Update data from anonymous type
var updateData = new { name = "John Doe" };
// Update data from JSON
var updateJson = JToken.Parse("{ 'name': 'Raymond Doe' }");
// Update data from dictionary
var updateDict = new Dictionary<string, object> { ["name"] = "Andy Doe" };
await collection.UpdateOneAsync(e => e.id == 1, updateData);
await collection.UpdateOneAsync(e => e.id == 2, updateJson);
await collection.UpdateOneAsync(e => e.id == 3, updateDict);
// Use LINQ to query items
var results = collection.AsQueryable().Where(x => x.age > 30); FunctionalityCollectionsExample user collection in JSON: {
"user": [
{ "id": 1, "name": "Phil", "age": 40, "city": "NY" },
{ "id": 2, "name": "Larry", "age": 37, "city": "London" }
]
} QueryCollection can be queried with LINQ by getting queryable from the collection with NOTE:
var store = new DataStore(pathToJson);
var collection = store.GetCollection("user");
// Find item with name
var userDynamic = collection
.AsQueryable()
.FirstOrDefault(p => p.name == "Phil");
var store = new DataStore(pathToJson);
var collection = store.GetCollection<User>();
// Find item with name
var userTyped = collection
.AsQueryable()
.FirstOrDefault(p => p.Name == "Phil"); Full-text searchFull-text search can be performed with var store = new DataStore(pathToJson);
var collection = store.GetCollection("user");
// Find all users that contain text Alabama in any of property values
var matches = collection.Find("Alabama");
// Perform case sensitive search
var caseSensitiveMatches = collection.Find("Alabama", true);
Insert
// Asynchronous method and dynamic data
// Before update : { }
// After update : { "id": 3, "name": "Raymond", "age": 32, "city" = "NY" }
await collection.InsertOneAsync(new { id = 3, name = "Raymond", age = 32, city = "NY" });
// Dynamic item can also be JSON object
var user = JToken.Parse("{ 'id': 3, 'name': 'Raymond', 'age': 32, 'city': 'NY' }");
await collection.InsertOneAsync(user);
// Synchronous method and typed data
// Before update : { }
// After update : { "id": 3, "name": "Raymond", "age": 32, "city" = "NY" }
collection.InsertOne(new User { Id = 3, Name = "Raymond", Age = 32, City = "NY" });
var newItems = new[]
{
new User { Id = 3, Name = "Raymond", Age = 32, City = "NY" },
new User { Id = 4, Name = "Ted", Age = 43, City = "NY" }
};
collection.InsertMany(newItems);
var newItems = new[]
{
new { id = 14, name = "Raymond", age = 32, city = "NY" },
new { id = 68, name = "Ted", age = 43, city = "NY" },
new { name = "Bud", age = 43, city = "NY" }
};
// Last user will have id 69
collection.InsertMany(newItems);
// Item in newItems collection won't have id property as anonymous types are read only If the // Latest id in the collection is hello5
var user = JToken.Parse("{ 'id': 'wrongValue', 'name': 'Raymond', 'age': 32, 'city': 'NY' }");
await collection.InsertOneAsync(user);
// After addition: user["id"] == "hello6"
// User data doesn't have an id field
var userNoId = JToken.Parse("{ 'name': 'Raymond', 'age': 32, 'city': 'NY' }");
await collection.InsertOneAsync(userNoId);
// After addition: userNoId["id"] == "hello7" If collection is empty and the type of the id-field is number, then first id will be Replace
// Sync and dynamic
// Before update : { "id": 3, "name": "Raymond", "age": 32, "city": "NY" }
// After update : { "id": 3, "name": "Barry", "age": 42 }
collection.ReplaceOne(3, new { id = 3, name = "Barry", age = 33 });
// or with predicate
collection.ReplaceOne(e => e.id == 3, new { id = 3, name = "Barry", age = 33 });
// Async and typed
// Before update : { "id": 3, "name": "Raymond", "age": 32, "city": "NY" }
// After update : { "id": 3, "name": "Barry", "age": 42 }
await collection.ReplaceOneAsync(3, new User { Id = 3, Name = "Barry", Age = 33 });
collection.ReplaceMany(e => e.City == "NY", new { City = "New York" });
// New item will be inserted with id 11
collection.ReplaceOne(11, new { id = 11, name = "Theodor" }, true); Update
// Dynamic
// Before update : { "id": 1, "name": "Barry", "age": 33 }
// After update : { "id": 1, "name": "Barry", "age": 42 }
dynamic source = new ExpandoObject();
source.age = 42;
await collection.UpdateOneAsync(1, source as object);
// or with predicate
await collection.UpdateOneAsync(e => e.id == 1, source as object);
// Typed
// Before update : { "id": 1, "name": "Phil", "age": 40, "city": "NY" }
// After update : { "id": 1, "name": "Phil", "age": 42, "city": "NY" }
await collection.UpdateOneAsync(e => e.Name == "Phil", new { age = 42 });
await collection.UpdateManyAsync(e => e.Age == 30, new { age = 31 }); Update can also update items from the collection and add new items to the collection. var family = new Family
{
Id = 12,
FamilyName = "Andersen",
Parents = new List<Parent>
{
new Parent { FirstName = "Jim", Age = 52 }
},
Address = new Address { City = "Helsinki" }
};
await collection.InsertOneAsync(family);
// Adds a second parent to the list
await collection.UpdateOneAsync(e => e.Id == 12, new { Parents = new[] { null, new { FirstName = "Sally", age = 41 } } });
// Updates the first parent's age to 42
await collection.UpdateOneAsync(e => e.Id == 12, new { Parents = new[] { new { age = 42 } } }); Easy way to create a patch var user = new User
{
Id = 12,
Name = "Timmy",
Age = 30,
Work = new WorkPlace { Name = "EMACS" }
};
// JSON: { "Age": 41, "Name": "James", "Work": { "Name": "ACME" } }
// Anonymous type: new { Age = 41, Name = "James", Work = new { Name = "ACME" } };
var patchData = new Dictionary<string, object>();
patchData.Add("Age", 41);
patchData.Add("Name", "James");
patchData.Add("Work", new Dictionary<string, object> { { "Name", "ACME" } });
var jobject = JObject.FromObject(patchData);
dynamic patchExpando = JsonConvert.DeserializeObject<ExpandoObject>(jobject.ToString());
await collection.UpdateOneAsync(e => e.Id == 12, patchExpando); LimitationsDictionaries won't work when serializing JSON or data to If the update var player = new Player
{
Id = 423,
Scores = new Dictionary<string, int>
{
{ "Blue Max", 1256 },
{ "Pacman", 3221 }
},
};
var patchData = new ExpandoObject();
var items = patchData as IDictionary<string, object>;
items.Add("Scores", new Dictionary<string, string> { { "Blue Max", 1345 }, { "Outrun", 1234 }, { "Pacman", 3221 }, });
await collection.UpdateOneAsync(e => e.Id == 423, patchData); Delete
全部评论
专题导读
上一篇:jsonresume/resume-cli: CLI tool to easily setup a new resume 发布时间:2022-07-09下一篇:willdurand/JsonpCallbackValidator: JSONP callback validator.发布时间:2022-07-09热门推荐
热门话题
阅读排行榜
|
请发表评论