Skip to content

Instantly share code, notes, and snippets.

@keithn
Last active November 14, 2018 09:22
Show Gist options
  • Save keithn/bb03b6de07e8deada05e5eb87fcc30ed to your computer and use it in GitHub Desktop.
Save keithn/bb03b6de07e8deada05e5eb87fcc30ed to your computer and use it in GitHub Desktop.
Simple Diary to Json
using System;
using System.Collections.Generic;
using System.IO;
using DemoDiary;
using Newtonsoft.Json;
namespace DemoDiary
{
public class Diary
{
public List<Entry> Entries { get; set; } = new List<Entry>();
public void Add(string v)
{
Entries.Add(new Entry(){Text = v, Time = DateTimeOffset.Now});
}
}
public class Entry
{
public DateTimeOffset Time { get; set; }
public string Text { get; set; }
}
class Program
{
static void Main(string[] args)
{
var diary = new Diary();
var quit = false;
while (!quit)
{
var entry = Console.ReadLine();
if (entry?.ToLower() == "bye")
{
quit = true;
}
else
{
diary.Add(entry);
}
}
File.WriteAllText("mydiary.json", JsonConvert.SerializeObject(diary));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment