-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # src/Sitko.Core.App/Json/JsonHelper.cs # src/Sitko.Core.Db.Postgres/ModelBuilderExtensions.cs # src/Sitko.Core.Repository.Remote/Sitko.Core.Repository.Remote.csproj # tests/Sitko.Core.Repository.Remote.Tests/RemoteRepositoryTests.cs
- Loading branch information
Showing
12 changed files
with
162 additions
and
75 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,38 @@ | ||
using Newtonsoft.Json; | ||
using Serilog; | ||
|
||
namespace Sitko.Core.App.Json; | ||
|
||
public class JsonHelper | ||
namespace Sitko.Core.App.Json | ||
{ | ||
private static JsonSerializerSettings GetJsonSettings(bool throwOnError) => | ||
new() | ||
{ | ||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore, | ||
DateParseHandling = DateParseHandling.DateTimeOffset, | ||
TypeNameHandling = TypeNameHandling.Auto, | ||
MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead, | ||
Error = (_, e) => | ||
public class JsonHelper | ||
{ | ||
private static JsonSerializerSettings GetJsonSettings(bool throwOnError, bool prettify = false) => | ||
new() | ||
{ | ||
if (!throwOnError) | ||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore, | ||
DateParseHandling = DateParseHandling.DateTimeOffset, | ||
TypeNameHandling = TypeNameHandling.Auto, | ||
MetadataPropertyHandling = MetadataPropertyHandling.ReadAhead, | ||
Formatting = prettify ? Formatting.Indented : Formatting.None, | ||
Error = (_, e) => | ||
{ | ||
Log.Logger.Error(e.ErrorContext.Error, "Error deserializing json content: {ErrorText}", | ||
e.ErrorContext.Error.ToString()); | ||
e.ErrorContext.Handled = true; | ||
if (!throwOnError) | ||
{ | ||
Log.Logger.Error(e.ErrorContext.Error, "Error deserializing json content: {ErrorText}", | ||
e.ErrorContext.Error.ToString()); | ||
e.ErrorContext.Handled = true; | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
public static string SerializeWithMetadata(object obj, bool throwOnError = true) => | ||
JsonConvert.SerializeObject(obj, GetJsonSettings(throwOnError)); | ||
public static string SerializeWithMetadata(object obj, bool throwOnError = true, bool prettify = false) => | ||
JsonConvert.SerializeObject(obj, GetJsonSettings(throwOnError, prettify)); | ||
|
||
public static T? DeserializeWithMetadata<T>(string json, bool throwOnError = true) => | ||
JsonConvert.DeserializeObject<T>(json, GetJsonSettings(throwOnError)); | ||
public static T? DeserializeWithMetadata<T>(string json, bool throwOnError = true, bool prettify = false) => | ||
JsonConvert.DeserializeObject<T>(json, GetJsonSettings(throwOnError, prettify)); | ||
|
||
public static T? Clone<T>(T? obj, bool throwOnError = true) where T : class => | ||
obj is null ? null : DeserializeWithMetadata<T>(SerializeWithMetadata(obj, throwOnError), throwOnError)!; | ||
public static T? Clone<T>(T? obj, bool throwOnError = true, bool prettify = false) where T : class => | ||
obj is null | ||
? null | ||
: DeserializeWithMetadata<T>(SerializeWithMetadata(obj, throwOnError, prettify), throwOnError)!; | ||
} | ||
} | ||
|
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
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.