-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from WalletConnect/feat/ensure-type-is-valid
feat: add type safe check when sending custom request / response types
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Core Modules/WalletConnectSharp.Common/Utils/TypeSafety.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,27 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace WalletConnectSharp.Common.Utils; | ||
|
||
public static class TypeSafety | ||
{ | ||
private static JsonSerializerSettings Settings = | ||
new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto }; | ||
|
||
public static void EnsureTypeSerializerSafe<T>(T testObject) | ||
{ | ||
// unwrapping and rewrapping the object | ||
// to / from JSON should tell us | ||
// if it's serializer safe, since | ||
// we are using the serializer to test | ||
UnsafeJsonRewrap<T, T>(testObject, Settings); | ||
} | ||
|
||
public static TR UnsafeJsonRewrap<T, TR>(this T source, JsonSerializerSettings settings = null) | ||
{ | ||
var json = settings == null ? | ||
JsonConvert.SerializeObject(source) : | ||
JsonConvert.SerializeObject(source, settings); | ||
|
||
return JsonConvert.DeserializeObject<TR>(json); | ||
} | ||
} |
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