Skip to content

Commit

Permalink
Merge pull request #13 from Atralupus/feat/raw-hex
Browse files Browse the repository at this point in the history
[PDX-390] Add IStateJsonConverter for raw hex value
  • Loading branch information
moreal authored Mar 6, 2024
2 parents 6d7a303 + 3c55f92 commit e4c0e6b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion NineChroniclesUtilBackend.Store/Models/State/ArenaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ public ArenaData(ArenaScore score, ArenaInformation information, ArenaSheet.Roun
RoundData = roundData;
AvatarAddress = avatarAddress;
}
}
}
3 changes: 1 addition & 2 deletions NineChroniclesUtilBackend.Store/Models/State/BaseData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ public class BaseData
{
protected static JsonSerializerSettings JsonSerializerSettings => new JsonSerializerSettings
{
Converters = new[] { new BigIntegerToStringConverter() },
Converters = { new StateJsonConverter() },
Formatting = Formatting.Indented,
// ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
};

Expand Down
46 changes: 46 additions & 0 deletions NineChroniclesUtilBackend.Store/Util/StateJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Nekoyume.Model.State;
using Libplanet.Common;
using Bencodex;
using Bencodex.Types;

namespace NineChroniclesUtilBackend.Store.Util;

public class StateJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(IState).IsAssignableFrom(objectType);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JObject jo = JObject.FromObject(value, JsonSerializer.CreateDefault(new JsonSerializerSettings {
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Converters = new List<JsonConverter>() { new BigIntegerToStringConverter() },
Formatting = Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore
}));

IValue? ivalue = value switch
{
AvatarState avatarState => avatarState.SerializeList(),
IState state => state.Serialize(),
_ => null
};

if (ivalue != null)
{
string rawValue = ByteUtil.Hex(new Codec().Encode(ivalue));
jo.Add("Raw", rawValue);
}

jo.WriteTo(writer);
}
}

0 comments on commit e4c0e6b

Please sign in to comment.