Skip to content

Commit

Permalink
Merge pull request #2975 from eugene-doobu/feature/fix-serialize-exce…
Browse files Browse the repository at this point in the history
…ption

Feature/fix serialize exception
  • Loading branch information
eugene-doobu authored Nov 5, 2024
2 parents 3d4905b + fed7c9b commit c14e0f4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
41 changes: 41 additions & 0 deletions Lib9c.MessagePack/Formatters/CurrencyFormatter.cs
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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Libplanet.Types.Assets;
using MessagePack.Formatters;
using Nekoyume.Action;
using Nekoyume.Model.State;

namespace Lib9c.Formatters
{
Expand All @@ -22,6 +23,7 @@ public static class NineChroniclesResolverGetFormatterHelper
{typeof(Dictionary), new BencodexFormatter<Dictionary>()},
{typeof(IValue), new BencodexFormatter<IValue>()},
{typeof(ActionBase), new NCActionFormatter()},
{typeof(Currency), new CurrencyFormatter()},
// add more your own custom serializers.
};

Expand Down
3 changes: 2 additions & 1 deletion Lib9c/Action/InvalidSignatureException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ StreamingContext context
)
{
base.GetObjectData(info, context);
info.AddValue(nameof(Pending), new Codec().Encode(Pending.Serialize()));
var pendingData = Pending is null ? null : new Codec().Encode(Pending.Serialize());
info.AddValue(nameof(Pending), pendingData);
info.AddValue(nameof(Signature), Signature);
}
}
Expand Down

0 comments on commit c14e0f4

Please sign in to comment.