-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Updates to working with json * Add FromJsonExtension
- Loading branch information
1 parent
6526a71
commit a64aca3
Showing
18 changed files
with
299 additions
and
113 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/BitzArt.Flux.Json/Builder/Exceptions/FluxJsonDeserializationException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace BitzArt.Flux; | ||
|
||
internal class FluxJsonDeserializationException<TModel> : Exception | ||
{ | ||
public FluxJsonDeserializationException() | ||
: base($"Failed to deserialize JSON to {typeof(TModel).Name}") | ||
{ } | ||
} |
8 changes: 8 additions & 0 deletions
8
src/BitzArt.Flux.Json/Builder/Exceptions/FluxJsonFileReadException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace BitzArt.Flux; | ||
|
||
internal class FluxJsonFileReadException : Exception | ||
{ | ||
public FluxJsonFileReadException(string path, Exception innerException) | ||
: base($"Error reading JSON from file '{path}'. See inner exception for details", innerException) | ||
{ } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/BitzArt.Flux.Json/Builder/Extensions/ConfigureJsonExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Text.Json; | ||
|
||
namespace BitzArt.Flux; | ||
|
||
public static class ConfigureJsonExtension | ||
{ | ||
public static IFluxJsonServiceBuilder ConfigureJson(this IFluxJsonServiceBuilder builder, Action<JsonSerializerOptions> configure) | ||
{ | ||
configure(builder.ServiceOptions.SerializerOptions); | ||
|
||
return builder; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/BitzArt.Flux.Json/Builder/Extensions/FromJsonExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Text.Json; | ||
|
||
namespace BitzArt.Flux; | ||
|
||
public static class FromJsonExtension | ||
{ | ||
public static IFluxJsonSetBuilder<TModel> FromJson<TModel>(this IFluxJsonSetBuilder<TModel> builder, | ||
string json) | ||
where TModel : class | ||
{ | ||
builder.SetOptions.Items = | ||
JsonSerializer.Deserialize<List<TModel>>(json, builder.ServiceOptions.SerializerOptions) | ||
?? throw new FluxJsonDeserializationException<List<TModel>>(); | ||
|
||
return builder; | ||
} | ||
|
||
public static IFluxJsonSetBuilder<TModel, TKey> FromJson<TModel, TKey>(this IFluxJsonSetBuilder<TModel, TKey> builder, | ||
string json) | ||
where TModel : class | ||
{ | ||
builder.SetOptions.Items = | ||
JsonSerializer.Deserialize<List<TModel>>(json, builder.ServiceOptions.SerializerOptions) | ||
?? throw new FluxJsonDeserializationException<List<TModel>>(); | ||
|
||
return builder; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/BitzArt.Flux.Json/Builder/Extensions/FromJsonFileExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System.Text.Json; | ||
|
||
namespace BitzArt.Flux; | ||
|
||
public static class FromJsonFileExtension | ||
{ | ||
public static IFluxJsonSetBuilder<TModel> FromJsonFile<TModel>(this IFluxJsonSetBuilder<TModel> builder, | ||
string filePath) | ||
where TModel : class | ||
{ | ||
var path = GetFilePath(filePath, builder.ServiceOptions.BaseFilePath); | ||
builder.SetOptions.Items = TryGetItemsFromJsonFile<TModel>(path, builder.ServiceOptions.SerializerOptions); | ||
|
||
return builder; | ||
} | ||
|
||
public static IFluxJsonSetBuilder<TModel, TKey> FromJsonFile<TModel, TKey>(this IFluxJsonSetBuilder<TModel, TKey> builder, | ||
string filePath) | ||
where TModel : class | ||
{ | ||
var path = GetFilePath(filePath, builder.ServiceOptions.BaseFilePath); | ||
builder.SetOptions.Items = TryGetItemsFromJsonFile<TModel>(path, builder.ServiceOptions.SerializerOptions); | ||
|
||
return builder; | ||
} | ||
|
||
private static ICollection<TModel> TryGetItemsFromJsonFile<TModel>(string path, JsonSerializerOptions options) | ||
{ | ||
try | ||
{ | ||
return GetItemsFromJsonFile<TModel>(path, options); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new FluxJsonFileReadException(path, ex); | ||
} | ||
} | ||
|
||
private static ICollection<TModel> GetItemsFromJsonFile<TModel>(string path, JsonSerializerOptions options) | ||
{ | ||
var jsonString = File.ReadAllText(path); | ||
var items = JsonSerializer.Deserialize<List<TModel>>(jsonString, options) | ||
?? throw new FluxJsonDeserializationException<List<TModel>>(); | ||
|
||
return items; | ||
} | ||
|
||
private static string GetFilePath(string filePath, string? basePath = null) | ||
{ | ||
if (basePath is not null) filePath = Path.Combine(basePath, filePath); | ||
|
||
return filePath; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/BitzArt.Flux.Json/Builder/Extensions/WithBaseFilePathExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace BitzArt.Flux; | ||
|
||
public static class WithBaseFilePathExtension | ||
{ | ||
public static IFluxJsonServiceBuilder WithBaseFilePath(this IFluxJsonServiceBuilder builder, string baseFilePath) | ||
{ | ||
builder.ServiceOptions.BaseFilePath = baseFilePath; | ||
|
||
return builder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.