-
Notifications
You must be signed in to change notification settings - Fork 48
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 #2975 from eugene-doobu/feature/fix-serialize-exce…
…ption Feature/fix serialize exception
- Loading branch information
Showing
3 changed files
with
45 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Buffers; | ||
using MessagePack; | ||
using Libplanet.Types.Assets; | ||
using MessagePack.Formatters; | ||
using Bencodex; | ||
|
||
namespace Lib9c.Formatters | ||
{ | ||
public class CurrencyFormatter : IMessagePackFormatter<Currency> | ||
{ | ||
public void Serialize(ref MessagePackWriter writer, Currency value, MessagePackSerializerOptions options) | ||
{ | ||
if (value.Equals(default)) | ||
{ | ||
writer.WriteNil(); | ||
return; | ||
} | ||
|
||
writer.Write(new Codec().Encode(value.Serialize())); | ||
} | ||
|
||
public Currency Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) | ||
{ | ||
if (reader.TryReadNil()) | ||
{ | ||
return default; | ||
} | ||
|
||
options.Security.DepthStep(ref reader); | ||
|
||
var bytes = reader.ReadBytes()?.ToArray(); | ||
if (bytes is null) | ||
{ | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
return new Currency(new Codec().Decode(bytes)); | ||
} | ||
} | ||
} |
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