-
Notifications
You must be signed in to change notification settings - Fork 18
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 #13 from Atralupus/feat/raw-hex
[PDX-390] Add IStateJsonConverter for raw hex value
- Loading branch information
Showing
4 changed files
with
48 additions
and
3 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
File renamed without changes.
46 changes: 46 additions & 0 deletions
46
NineChroniclesUtilBackend.Store/Util/StateJsonConverter.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,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); | ||
} | ||
} |