handle edge case in JSON file read/write code
This commit is contained in:
parent
89221b8b2d
commit
e7606884ad
|
@ -31,9 +31,14 @@ namespace StardewModdingAPI.Framework.Serialisation
|
||||||
/// <typeparam name="TModel">The model type.</typeparam>
|
/// <typeparam name="TModel">The model type.</typeparam>
|
||||||
/// <param name="fullPath">The absolete file path.</param>
|
/// <param name="fullPath">The absolete file path.</param>
|
||||||
/// <returns>Returns the deserialised model, or <c>null</c> if the file doesn't exist or is empty.</returns>
|
/// <returns>Returns the deserialised model, or <c>null</c> if the file doesn't exist or is empty.</returns>
|
||||||
|
/// <exception cref="InvalidOperationException">The given path is empty or invalid.</exception>
|
||||||
public TModel ReadJsonFile<TModel>(string fullPath)
|
public TModel ReadJsonFile<TModel>(string fullPath)
|
||||||
where TModel : class
|
where TModel : class
|
||||||
{
|
{
|
||||||
|
// validate
|
||||||
|
if (string.IsNullOrWhiteSpace(fullPath))
|
||||||
|
throw new ArgumentException("The file path is empty or invalid.", nameof(fullPath));
|
||||||
|
|
||||||
// read file
|
// read file
|
||||||
string json;
|
string json;
|
||||||
try
|
try
|
||||||
|
@ -53,11 +58,18 @@ namespace StardewModdingAPI.Framework.Serialisation
|
||||||
/// <typeparam name="TModel">The model type.</typeparam>
|
/// <typeparam name="TModel">The model type.</typeparam>
|
||||||
/// <param name="fullPath">The absolete file path.</param>
|
/// <param name="fullPath">The absolete file path.</param>
|
||||||
/// <param name="model">The model to save.</param>
|
/// <param name="model">The model to save.</param>
|
||||||
|
/// <exception cref="InvalidOperationException">The given path is empty or invalid.</exception>
|
||||||
public void WriteJsonFile<TModel>(string fullPath, TModel model)
|
public void WriteJsonFile<TModel>(string fullPath, TModel model)
|
||||||
where TModel : class
|
where TModel : class
|
||||||
{
|
{
|
||||||
|
// validate
|
||||||
|
if (string.IsNullOrWhiteSpace(fullPath))
|
||||||
|
throw new ArgumentException("The file path is empty or invalid.", nameof(fullPath));
|
||||||
|
|
||||||
// create directory if needed
|
// create directory if needed
|
||||||
string dir = Path.GetDirectoryName(fullPath);
|
string dir = Path.GetDirectoryName(fullPath);
|
||||||
|
if (dir == null)
|
||||||
|
throw new ArgumentException("The file path is invalid.", nameof(fullPath));
|
||||||
if (!Directory.Exists(dir))
|
if (!Directory.Exists(dir))
|
||||||
Directory.CreateDirectory(dir);
|
Directory.CreateDirectory(dir);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue