Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
MiNET configuration using JSON
{
"Message":"Hello world of configurations!",
"Rules":[
"All rules apply",
"No rules apply",
"The only rule that apply, is no rules",
"JSON rule!"
]
}
<?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>
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()
{
}
}
}

Hi Gurun - small fix : line 56 should be "config.json" to match line 49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment