61 lines
1.6 KiB
C#
61 lines
1.6 KiB
C#
using ln.json.mapping;
|
|
using System;
|
|
using System.IO;
|
|
|
|
namespace ln.json
|
|
{
|
|
public abstract class JSONMappedObject
|
|
{
|
|
FileSystemWatcher watcher;
|
|
string jsonFileName;
|
|
object o;
|
|
|
|
public JSONMappedObject() { }
|
|
public JSONMappedObject(String jsonFile)
|
|
{
|
|
this.jsonFileName = jsonFile;
|
|
this.o = this;
|
|
|
|
watcher = new FileSystemWatcher(Path.GetDirectoryName(jsonFile));
|
|
watcher.Filter = Path.GetFileName(jsonFile);
|
|
watcher.Changed += (s, e) => mapJSON();
|
|
watcher.Created += (s, e) => mapJSON();
|
|
|
|
watcher.EnableRaisingEvents = true;
|
|
|
|
mapJSON();
|
|
}
|
|
public JSONMappedObject(String jsonFile,object o)
|
|
{
|
|
this.jsonFileName = jsonFile;
|
|
this.o = o;
|
|
|
|
watcher = new FileSystemWatcher(Path.GetDirectoryName(jsonFile));
|
|
watcher.Filter = Path.GetFileName(jsonFile);
|
|
watcher.Changed += (s, e) => mapJSON();
|
|
watcher.Created += (s, e) => mapJSON();
|
|
|
|
watcher.EnableRaisingEvents = true;
|
|
|
|
mapJSON();
|
|
}
|
|
|
|
void mapJSON()
|
|
{
|
|
//Logging.Log(LogLevel.DEBUG, "File {0} changed. Remapping...", jsonFileName);
|
|
|
|
if (File.Exists(jsonFileName))
|
|
{
|
|
JSONValue jsonValue = JSONParser.ParseFile(jsonFileName);
|
|
if (jsonValue is JSONObject jsonObject)
|
|
{
|
|
JSONMapper mapper = new JSONMapper();
|
|
mapper.Apply(jsonObject, this.o);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|