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 ; } } }