using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Text; using System.Threading.Tasks; namespace SCJMapper_V2.Layout { /// /// Reads a DeviceFile from a file or stream /// class ControllerJson { /// /// Reads from a file one Controller entry /// /// The Json Filename /// A Controller obj or null for errors public static DeviceFile FromJson( string jFilename ) { DeviceFile c = null; string fn = jFilename; if ( !File.Exists( jFilename ) ) { fn = Path.Combine( TheUser.LayoutsDir, fn ); // if it is not found use the default path } if ( File.Exists( fn ) ) { using ( var ts = File.OpenRead( fn ) ) { c = FromJson( ts ); } } return c; } /// /// Reads from the open stream one Controller entry /// /// An open stream at position /// A Controller obj or null for errors public static DeviceFile FromJson( Stream jStream ) { try { var jsonSerializer = new DataContractJsonSerializer( typeof( DeviceFile ) ); object objResponse = jsonSerializer.ReadObject( jStream ); var jsonResults = objResponse as DeviceFile; return jsonResults; } #pragma warning disable CS0168 // Variable is declared but never used catch ( Exception e ) { #pragma warning restore CS0168 // Variable is declared but never used return null; } } } }