You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.7 KiB
65 lines
1.7 KiB
using System ;
|
|
using System.IO ;
|
|
|
|
using Newtonsoft.Json ;
|
|
using Newtonsoft.Json.Linq ;
|
|
using log4net ;
|
|
|
|
// --------------------------------------------------------------------
|
|
|
|
public class JsonConfig
|
|
{
|
|
private dynamic mData ;
|
|
|
|
public JsonConfig( string caption, string fname )
|
|
{
|
|
// initialize
|
|
ILog logger = LogManager.GetLogger( "startup" ) ;
|
|
|
|
// load the JSON config
|
|
string data ;
|
|
if ( File.Exists( fname ) ) {
|
|
logger.Info( $"Loading {caption}: {Path.GetFullPath(fname)}" ) ;
|
|
data = File.ReadAllText( fname ) ;
|
|
} else
|
|
data ="{}" ;
|
|
mData = JsonConvert.DeserializeObject( data ) ;
|
|
logger.Debug( mData.ToString() ) ;
|
|
}
|
|
|
|
public string getStringVal( string[] keys, string defaultVal="" )
|
|
{
|
|
// get the specified value
|
|
JToken curr = mData ;
|
|
foreach( string key in keys ) {
|
|
curr = curr[ key ] ;
|
|
if ( curr == null )
|
|
return defaultVal ;
|
|
}
|
|
return (string) curr ;
|
|
}
|
|
|
|
public int getIntVal( string[] keys, int defaultVal=0 )
|
|
{
|
|
// get the specified value
|
|
string val = getStringVal( keys ) ;
|
|
try {
|
|
return Int32.Parse( val ) ;
|
|
} catch( FormatException ) {
|
|
return defaultVal ;
|
|
}
|
|
}
|
|
|
|
public float getFloatVal( string[] keys, float defaultVal=0 )
|
|
{
|
|
// get the specified value
|
|
string val = getStringVal( keys ) ;
|
|
try {
|
|
return float.Parse( val ) ;
|
|
} catch( FormatException ) {
|
|
return defaultVal ;
|
|
}
|
|
}
|
|
|
|
public dynamic data { get { return mData ; } }
|
|
}
|
|
|