Skip to content

Instantly share code, notes, and snippets.

@slofurno
Created May 6, 2015 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slofurno/2cd9e9b249a3b6aac9e1 to your computer and use it in GitHub Desktop.
Save slofurno/2cd9e9b249a3b6aac9e1 to your computer and use it in GitHub Desktop.
append json
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Jil;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
var movies = Load();
var highrated = movies.Where(x => x.Rating > 8).ToList();
}
static void Save(IEnumerable<Movie> items)
{
using (var fs = File.Open("store.json", FileMode.Append))
using (var writer = new StreamWriter(fs))
{
foreach (var item in items)
{
var json = JSON.Serialize<Movie>(item);
writer.WriteLine(json);
}
}
}
static IEnumerable<Movie> Load()
{
var lines = File.ReadAllLines("store.json");
var movies = lines.Select(x => JSON.Deserialize<Movie>(x));
return movies;
}
}
public class Movie
{
public int Id { get; set; }
public string Name { get; set; }
public string Year { get; set; }
public float Rating { get; set; }
public int Votes { get; set; }
public List<int> Directors { get; set; }
public List<int> Writers { get; set; }
public List<int> Actors { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment