Manage your ASL charts, play aids and other documents.
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.
 
 
 
 
asl-charts/src/JsonConfig.cs

49 lines
1.4 KiB

using System ;
using System.Text ;
using System.IO ;
using System.Runtime.Serialization.Json ;
using System.Xml ;
using System.Xml.Linq ;
using System.Xml.XPath ;
// --------------------------------------------------------------------
public class JsonConfig
{
private XElement mRootElem ;
public JsonConfig( string caption, string fname )
{
// load the JSON config
string data ;
if ( File.Exists( fname ) ) {
Program.logTraceMsg( String.Format( "Loading {0}: {1}", caption, Path.GetFullPath(fname) ) ) ;
data = File.ReadAllText( fname ) ;
} else
data ="{}" ;
var jsonReader = JsonReaderWriterFactory.CreateJsonReader(
Encoding.UTF8.GetBytes( data ),
new XmlDictionaryReaderQuotas()
) ;
mRootElem = XElement.Load( jsonReader ) ;
}
public string getStringVal( string xpath, string defaultVal="" )
{
// return the specified value
var elem = mRootElem.XPathSelectElement( xpath ) ;
if ( elem == null )
return defaultVal ;
return elem.Value ;
}
public int getIntVal( string xpath, int defaultVal=0 )
{
// return the specified value
try {
return Int32.Parse( getStringVal( xpath ) ) ;
} catch( System.FormatException ) {
return defaultVal ;
}
}
}