Created
March 27, 2017 07:55
-
-
Save shammelburg/1e7acace387cffdc52933266e0c16a0d to your computer and use it in GitHub Desktop.
CRUD for .json file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void Delete(string Customer) | |
{ | |
// not tested | |
var array = Read().Item1; | |
var path = Read().Item2; | |
array.RemoveAt(array.FindIndex(x => x.Customer.Equals(Customer))); | |
string jsonData = JsonConvert.SerializeObject(array); | |
File.WriteAllText(path, jsonData); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private CustomerMapLocations Find(string Customer) | |
{ | |
var array = ReadJson().Item1; | |
var customer = array.Where(c => c.Customer.Equals(Customer)).FirstOrDefault(); | |
return customer; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Tuple<List<CustomerMapLocations>, string> Read() | |
{ | |
// get base directory | |
var dir = AppDomain.CurrentDomain.BaseDirectory; | |
string path = $"{dir}/addresses.json"; | |
//deserialize JSON from file | |
string Json = System.IO.File.ReadAllText(path); | |
JavaScriptSerializer ser = new JavaScriptSerializer(); | |
var array = ser.Deserialize<List<CustomerMapLocations>>(Json); | |
return Tuple.Create(array, path); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void Update(string Customer, double Long, double Lat) | |
{ | |
var array = ReadJson().Item1; | |
var path = ReadJson().Item2; | |
array.Add(new CustomerMapLocations { Customer = Customer, Latitude = Lat, Longitude = Long }); | |
string jsonData = JsonConvert.SerializeObject(array); | |
File.WriteAllText(path, jsonData); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment