/// <summary> /// Serializes an object instance to a JSON file. /// </summary> /// <param name="value">the value to serialize</param> /// <param name="fileName">Full path to the file to write out with JSON.</param> /// <param name="throwExceptions">Determines whether exceptions are thrown or false is returned</param> /// <param name="formatJsonOutput">if true pretty-formats the JSON with line breaks</param> /// <returns>true or false</returns> public static bool SerializeToFile(object value, string fileName, bool throwExceptions = false, bool formatJsonOutput = false) { dynamic writer = null; FileStream fs = null; try { Type type = value.GetType(); dynamic json = CreateJsonNet(throwExceptions); if (json == null) { return(false); } fs = new FileStream(fileName, FileMode.Create); var sw = new StreamWriter(fs, Encoding.UTF8); //JsonTextWriter writer = new JsonTextWriter(sw); writer = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.JsonTextWriter", sw); if (formatJsonOutput) { writer.Formatting = (dynamic)ReflectionUtils.GetStaticProperty("Newtonsoft.Json.Formatting", "Indented"); } else { writer.Formatting = (dynamic)ReflectionUtils.GetStaticProperty("Newtonsoft.Json.Formatting", "None"); } writer.QuoteChar = '"'; json.Serialize(writer, value); } catch (Exception ex) { Debug.WriteLine("JsonSerializer Serialize error: " + ex.Message); if (throwExceptions) { throw; } return(false); } finally { if (writer != null) { writer.Close(); } if (fs != null) { fs.Close(); } } return(true); }
/// <summary> /// Dynamically creates an instance of JSON.NET /// </summary> /// <param name="throwExceptions">If true throws exceptions otherwise returns null</param> /// <returns>Dynamic JsonSerializer instance</returns> public static dynamic CreateJsonNet(bool throwExceptions = true) { if (JsonNet != null) { return(JsonNet); } lock (SyncLock) { if (JsonNet != null) { return(JsonNet); } // Try to create instance dynamic json = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.JsonSerializer"); if (json == null) { try { json = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.JsonSerializer"); } catch { if (throwExceptions) { throw; } return(null); } } if (json == null) { return(null); } if (FormattingType == null) { FormattingType = ReflectionUtils.GetTypeFromName("Newtonsoft.Json.Formatting"); } JsonTextReaderType = ReflectionUtils.GetTypeFromName("Newtonsoft.Json.JsonTextReader"); JsonTextWriterType = ReflectionUtils.GetTypeFromName("Newtonsoft.Json.JsonTextWriter"); json.ReferenceLoopHandling = (dynamic)ReflectionUtils.GetStaticProperty("Newtonsoft.Json.ReferenceLoopHandling", "Ignore"); // Enums as strings in JSON dynamic enumConverter = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.Converters.StringEnumConverter"); json.Converters.Add(enumConverter); JsonNet = json; } return(JsonNet); }
/// <summary> /// Dynamically creates an instance of JSON.NET /// </summary> /// <param name="throwExceptions">If true throws exceptions otherwise returns null</param> /// <returns>Dynamic JsonSerializer instance</returns> public static dynamic CreateJsonNet(bool throwExceptions = true) { if (JsonNet != null) { return(JsonNet); } lock (SyncLock) { if (JsonNet != null) { return(JsonNet); } // Try to create instance dynamic json = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.JsonSerializer"); if (json == null) { try { var ass = AppDomain.CurrentDomain.Load("Newtonsoft.Json"); json = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.JsonSerializer"); } catch (Exception ex) { if (throwExceptions) { throw; } return(null); } } if (json == null) { return(null); } json.ReferenceLoopHandling = (dynamic)ReflectionUtils.GetStaticProperty("Newtonsoft.Json.ReferenceLoopHandling", "Ignore"); // Enums as strings in JSON dynamic enumConverter = ReflectionUtils.CreateInstanceFromString("Newtonsoft.Json.Converters.StringEnumConverter"); json.Converters.Add(enumConverter); JsonNet = json; } return(JsonNet); }
/// <summary> /// This method loads various providers dynamically similar to the /// way that DbProviderFactories.GetFactory() works except that /// this API is not available on .NET Standard 2.0 /// </summary> /// <param name="type"></param> /// <returns></returns> public static DbProviderFactory GetSqlProviderFactory(DataAccessProviderTypes type) { if (type == DataAccessProviderTypes.SqlServer) { return(SqlClientFactory.Instance); } if (type == DataAccessProviderTypes.SqLite) { #if NETCORE var instance = ReflectionUtils.GetStaticProperty("Microsoft.Data.Sqlite.SqliteFactory", "Instance"); if (instance == null) { var a = ReflectionUtils.LoadAssembly("Microsoft.Data.Sqlite"); if (a != null) { instance = ReflectionUtils.GetStaticProperty("Microsoft.Data.Sqlite.SqliteFactory", "Instance"); } } if (instance == null) { throw new InvalidOperationException("Couldn't load SqLite Provider factory. Please make sure the Microsoft.Data.Sqlite package has been added to your project"); } return(instance as DbProviderFactory); #else var instance = ReflectionUtils.GetStaticProperty("System.Data.Sqlite.SQLiteFactory", "Instance"); if (instance == null) { var a = ReflectionUtils.LoadAssembly("System.Data.SQLite"); if (a != null) { instance = ReflectionUtils.GetStaticProperty("System.Data.SQLite.SQLiteFactory", "Instance"); } } if (instance == null) { throw new InvalidOperationException( "Couldn't load SqLite Provider factory. Please make sure the System.Data.SQLite reference has been added to your project"); } return(instance as DbProviderFactory); #endif } throw new InvalidOperationException("Unsupported Provider Factory specified: " + type); }
/// <summary> /// Loads a SQL Provider factory based on the DbFactory type name and assembly. /// </summary> /// <param name="dbProviderFactoryTypename">Type name of the DbProviderFactory</param> /// <param name="assemblyName">Short assembly name of the provider factory. Note: Host project needs to have a reference to this assembly</param> /// <returns></returns> public static DbProviderFactory GetDbProviderFactory(string dbProviderFactoryTypename, string assemblyName) { var instance = ReflectionUtils.GetStaticProperty(dbProviderFactoryTypename, "Instance"); if (instance == null) { var a = ReflectionUtils.LoadAssembly(assemblyName); if (a != null) { instance = ReflectionUtils.GetStaticProperty(dbProviderFactoryTypename, "Instance"); } } if (instance == null) { throw new InvalidOperationException(string.Format(Resources.UnableToRetrieveDbProviderFactoryForm, dbProviderFactoryTypename)); } return(instance as DbProviderFactory); }