-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backend(,Tests): implement basic exception deserialization
JSON deserialization of exceptions is extremely broken in JSON.NET [1] (and even in System.Text.Json [2]), so let's use a hybrid between binary serialization and JSON serialization: it will have a JSON part that hints about the most important human-readable details, but it will use a JSON element to marshall in binary-to-default-encoding-based-string format. (I could have written a JsonConverter for this, but I was having trouble finding one out there that someone else had written already, then I decided to go for the simplest solution that I can write myself. I hate JSON type converters anyway.) [1] JamesNK/Newtonsoft.Json#801 [2] dotnet/runtime#43026 [3] https://stackoverflow.com/a/13674508/544947
- Loading branch information
Showing
6 changed files
with
127 additions
and
50 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
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,18 +1,13 @@ | ||
{ | ||
"Version": "{version}", | ||
"TypeName": "System.Exception", | ||
"TypeName": "GWallet.Backend.MarshalledException", | ||
"Value": { | ||
"ClassName": "System.Exception", | ||
"Message": "msg", | ||
"Data": null, | ||
"InnerException": null, | ||
"HelpURL": null, | ||
"StackTraceString": null, | ||
"RemoteStackTraceString": null, | ||
"RemoteStackIndex": 0, | ||
"ExceptionMethod": null, | ||
"HResult": -2146233088, | ||
"Source": null, | ||
"WatsonBuckets": null | ||
"HumanReadableSummary": { | ||
"ExceptionType": "System.Exception", | ||
"Message": "msg", | ||
"StackTrace": "", | ||
"InnerException": null | ||
}, | ||
"FullBinaryForm": "{binaryMarshalledException}" | ||
} | ||
} |
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 @@ | ||
namespace GWallet.Backend | ||
|
||
open System | ||
open System.IO | ||
open System.Text | ||
open System.Runtime.Serialization.Formatters.Binary | ||
|
||
module BinaryMarshalling = | ||
|
||
let private binFormatter = BinaryFormatter() | ||
|
||
let Serialize obj: array<byte> = | ||
use memStream = new MemoryStream() | ||
binFormatter.Serialize(memStream, obj) | ||
memStream.ToArray() | ||
|
||
let Deserialize (buffer: array<byte>) = | ||
use memStream = new MemoryStream(buffer) | ||
memStream.Position <- 0L | ||
binFormatter.Deserialize memStream | ||
|
||
let SerializeToString obj: string = | ||
let byteArray = Serialize obj | ||
Convert.ToBase64String byteArray | ||
|
||
let DeserializeFromString (byteArrayString: string) = | ||
let byteArray = Convert.FromBase64String byteArrayString | ||
Deserialize byteArray |
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