Browse Source

Swap out Newtonsoft.Json for XML, as it wasn't working right.

master
Starbeamrainbowlabs 4 years ago
parent
commit
a28845d2bd
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
  1. 5
      RhinoReminds/ClientListener.cs
  2. 2
      RhinoReminds/Program.cs
  3. 46
      RhinoReminds/Reminder.cs
  4. 46
      RhinoReminds/ReminderList.cs
  5. 4
      RhinoReminds/RhinoReminds.csproj
  6. 1
      RhinoReminds/packages.config

5
RhinoReminds/ClientListener.cs

@ -6,7 +6,6 @@ using System.Text; @@ -6,7 +6,6 @@ using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using S22.Xmpp;
using S22.Xmpp.Client;
using S22.Xmpp.Im;
@ -23,7 +22,7 @@ namespace RhinoReminds @@ -23,7 +22,7 @@ namespace RhinoReminds
public string Hostname => Jid.Split('@')[1];
private readonly string password;
public string ReminderFilePath { get; set; } = "./Reminders.json";
public string ReminderFilePath { get; set; } = "./reminders.xml";
private ReminderList reminderList = new ReminderList();
private XmppClient client;
@ -39,7 +38,7 @@ namespace RhinoReminds @@ -39,7 +38,7 @@ namespace RhinoReminds
if (File.Exists(ReminderFilePath))
{
Console.WriteLine($"[Rhino/Startup] Loading reminders list from {ReminderFilePath}");
reminderList = JsonConvert.DeserializeObject<ReminderList>(File.ReadAllText(ReminderFilePath));
reminderList = ReminderList.FromXmlFile(ReminderFilePath);
}
client = new XmppClient(Hostname, Username, password);

2
RhinoReminds/Program.cs

@ -9,7 +9,7 @@ namespace RhinoReminds @@ -9,7 +9,7 @@ namespace RhinoReminds
{
public class ProgramSettings
{
public string Filepath = "./reminders.json";
public string Filepath = "./reminders.xml";
public string Jid = null;
public string Password = null;

46
RhinoReminds/Reminder.cs

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
using System;
using Newtonsoft.Json;
using System.Net;
using System.Xml;
using S22.Xmpp;
namespace RhinoReminds
@ -9,7 +10,6 @@ namespace RhinoReminds @@ -9,7 +10,6 @@ namespace RhinoReminds
{
public int Id { get; }
public string Jid { get; }
[JsonIgnore]
public Jid JidObj => new Jid(Jid);
public DateTime Time { get; }
public string Message { get; }
@ -26,5 +26,47 @@ namespace RhinoReminds @@ -26,5 +26,47 @@ namespace RhinoReminds
{
return $"[Reminder Id={Id}, Jid={Jid}, Time={Time}, Message={Message}";
}
#region XML
public void WriteToXml(XmlWriter xml)
{
xml.WriteStartElement("Reminder");
xml.WriteElementString("Id", Id.ToString());
xml.WriteElementString("Jid", Jid);
xml.WriteElementString("Time", Time.ToString());
xml.WriteElementString("Message", WebUtility.HtmlEncode(Message));
xml.WriteEndElement();
}
public static Reminder FromXml(XmlNode xml)
{
int id = -1; string jid = string.Empty, message = string.Empty; DateTime dateTime = DateTime.Now;
foreach (XmlNode subNode in xml.ChildNodes) {
switch (subNode.Name) {
case "Id":
id = int.Parse(subNode.InnerText);
break;
case "Jid":
jid = subNode.InnerText;
break;
case "Time":
dateTime = DateTime.Parse(subNode.InnerText);
break;
case "Message":
message = WebUtility.HtmlDecode(subNode.InnerText);
break;
}
}
return new Reminder(id, jid, dateTime, message);
}
#endregion
}
}

46
RhinoReminds/ReminderList.cs

@ -1,21 +1,19 @@ @@ -1,21 +1,19 @@
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using System.Linq;
using System.Xml;
using S22.Xmpp;
namespace RhinoReminds
{
public delegate void OnReminderListUpdateHandler(object sender, Reminder newReminder);
[JsonObject(MemberSerialization.OptIn)]
public class ReminderList
{
[JsonProperty]
public int NextId { get; private set; } = 0;
private object saveLock = new object();
private int nextId = 0;
private readonly object saveLock = new object();
[JsonProperty]
public SortedList<DateTime, Reminder> Reminders = new SortedList<DateTime, Reminder>();
public event OnReminderListUpdateHandler OnReminderListUpdate;
@ -26,7 +24,7 @@ namespace RhinoReminds @@ -26,7 +24,7 @@ namespace RhinoReminds
}
public Reminder CreateReminder(Jid inJid, DateTime time, string message) {
Reminder result = new Reminder(NextId++, $"{inJid.Node}@{inJid.Domain}", time, message);
Reminder result = new Reminder(nextId++, $"{inJid.Node}@{inJid.Domain}", time, message);
Console.WriteLine($"[Rhino/ReminderList] Created reminder {result}");
Reminders.Add(time, result);
OnReminderListUpdate(this, result);
@ -38,7 +36,24 @@ namespace RhinoReminds @@ -38,7 +36,24 @@ namespace RhinoReminds
// Make sure that the reminder thread doesn't try to save the reminders at the exact same time
// we receive a request for a new reminder
lock (saveLock) { // FUTURE: We could go lockless here with some work, but it's not worth it for the teeny chance & low overhead
File.WriteAllText(filename, JsonConvert.SerializeObject(this));
XmlWriter xml = XmlWriter.Create(
filename,
new XmlWriterSettings() { Indent = true }
);
xml.WriteStartDocument();
xml.WriteStartElement("ReminderList");
xml.WriteElementString("NextId", nextId.ToString());
xml.WriteStartElement("Reminders");
foreach (Reminder nextReminder in Reminders.Values)
nextReminder.WriteToXml(xml);
xml.WriteEndElement();
xml.WriteEndElement();
xml.WriteEndDocument();
xml.Close();
}
}
@ -53,5 +68,20 @@ namespace RhinoReminds @@ -53,5 +68,20 @@ namespace RhinoReminds
public void DeleteReminder(Reminder nextReminder) {
Reminders.Remove(nextReminder.Time);
}
public static ReminderList FromXmlFile(string filepath)
{
XmlDocument xml = new XmlDocument();
xml.Load(filepath);
ReminderList result = new ReminderList();
result.nextId = int.Parse(xml.GetElementsByTagName("NextId")[0].InnerText);
foreach (XmlNode reminderXML in xml.GetElementsByTagName("Reminders")[0].ChildNodes) {
Reminder nextReminder = Reminder.FromXml(reminderXML);
result.Reminders.Add(nextReminder.Time, nextReminder);
}
return result;
}
}
}

4
RhinoReminds/RhinoReminds.csproj

@ -34,9 +34,6 @@ @@ -34,9 +34,6 @@
<Reference Include="S22.Xmpp">
<HintPath>..\packages\S22.Xmpp.1.0.0.0\lib\net45\S22.Xmpp.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="mscorlib" />
<Reference Include="Microsoft.Recognizers.Definitions">
<HintPath>..\packages\Microsoft.Recognizers.Text.1.1.2\lib\net462\Microsoft.Recognizers.Definitions.dll</HintPath>
@ -59,6 +56,7 @@ @@ -59,6 +56,7 @@
<Reference Include="System.ValueTuple">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />

1
RhinoReminds/packages.config

@ -4,7 +4,6 @@ @@ -4,7 +4,6 @@
<package id="Microsoft.Recognizers.Text.DateTime" version="1.1.2" targetFramework="net47" />
<package id="Microsoft.Recognizers.Text.Number" version="1.1.2" targetFramework="net47" />
<package id="Microsoft.Recognizers.Text.NumberWithUnit" version="1.1.2" targetFramework="net47" />
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net47" />
<package id="S22.Xmpp" version="1.0.0.0" targetFramework="net47" />
<package id="System.Collections.Immutable" version="1.5.0" targetFramework="net47" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net47" />

Loading…
Cancel
Save