Last active
December 26, 2015 19:08
-
-
Save NiclasOlofsson/b2538ac412d3c7310d04 to your computer and use it in GitHub Desktop.
MiNET configuration using JSON
This file contains 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
{ | |
"Message":"Hello world of configurations!", | |
"Rules":[ | |
"All rules apply", | |
"No rules apply", | |
"The only rule that apply, is no rules", | |
"JSON rule!" | |
] | |
} |
This file contains 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
<?xml version="1.0"?> | |
<MyPluginConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
<Message>Hello world of configurations!</Message> | |
<Rules> | |
<string>All rules apply</string> | |
<string>No rules apply</string> | |
<string>The only rule that apply, is no rules</string> | |
<string>JSON rule!</string> | |
</Rules> | |
</MyPluginConfiguration> |
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Reflection; | |
using System.Runtime.Serialization; | |
using System.Runtime.Serialization.Json; | |
using System.Xml.Serialization; | |
using log4net; | |
using MiNET.Plugins; | |
namespace TestPlugin | |
{ | |
public class ConfigSamplePlugin : IPlugin | |
{ | |
private static readonly ILog Log = LogManager.GetLogger(typeof (ConfigSamplePlugin)); | |
private PluginContext _context; | |
[DataContract] | |
public class MyPluginConfiguration | |
{ | |
[DataMember] | |
public string Message { get; set; } | |
[DataMember] | |
public List<string> Rules { get; set; } | |
} | |
public void OnEnable(PluginContext context) | |
{ | |
_context = context; | |
// Build a sample config | |
var config = new MyPluginConfiguration | |
{ | |
Message = "Hello world of configurations!", Rules = new List<string> | |
{ | |
"All rules apply", | |
"No rules apply", | |
"The only rule that apply, is no rules", | |
"JSON rule!" | |
} | |
}; | |
string pluginDirectory = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath); | |
// JSON FIRST | |
// Write it | |
using (FileStream stream = new FileStream(Path.Combine(pluginDirectory, "config.json"), FileMode.Create)) | |
{ | |
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof (MyPluginConfiguration)); | |
ser.WriteObject(stream, config); | |
} | |
// Read it | |
using (FileStream stream = new FileStream(Path.Combine(pluginDirectory, "config.txt"), FileMode.Open)) | |
{ | |
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof (MyPluginConfiguration)); | |
MyPluginConfiguration readConfig = (MyPluginConfiguration) ser.ReadObject(stream); | |
Log.InfoFormat("Message read is {0}", readConfig.Message); | |
} | |
// NOW XML | |
// Write it | |
using (FileStream stream = new FileStream(Path.Combine(pluginDirectory, "config.xml"), FileMode.Create)) | |
{ | |
var ser = new XmlSerializer(typeof (MyPluginConfiguration)); | |
ser.Serialize(stream, config); | |
} | |
// Read it | |
using (FileStream stream = new FileStream(Path.Combine(pluginDirectory, "config.xml"), FileMode.Open)) | |
{ | |
XmlSerializer ser = new XmlSerializer(typeof (MyPluginConfiguration)); | |
MyPluginConfiguration readConfig = (MyPluginConfiguration) ser.Deserialize(stream); | |
Log.InfoFormat("Message read is {0}", readConfig.Message); | |
} | |
} | |
public void OnDisable() | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Gurun - small fix : line 56 should be "config.json" to match line 49