Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
fix StackItem.ToJson
Browse files Browse the repository at this point in the history
  • Loading branch information
Luchuan committed Apr 16, 2020
1 parent 88a73fa commit d53c70d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
56 changes: 56 additions & 0 deletions neo-cli/CLI/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
using Neo.IO.Json;
using Neo.SmartContract;
using Neo.VM.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using Array = Neo.VM.Types.Array;
using Boolean = Neo.VM.Types.Boolean;
using Buffer = Neo.VM.Types.Buffer;

namespace Neo.CLI
{
internal static class Helper
Expand All @@ -19,5 +29,51 @@ public static bool IsYes(this string input)

return input == "yes" || input == "y";
}

public static JObject ToJson(this StackItem item)
{
return ToJson(item, null);
}

private static JObject ToJson(StackItem item, HashSet<StackItem> context)
{
JObject json = new JObject();
json["type"] = item.Type;
switch (item)
{
case Array array:
context ??= new HashSet<StackItem>(ReferenceEqualityComparer.Default);
if (!context.Add(array)) throw new InvalidOperationException();
json["value"] = new JArray(array.Select(p => ToJson(p, context)));
break;
case Boolean boolean:
json["value"] = boolean.ToBoolean();
break;
case Buffer buffer:
json["value"] = Convert.ToBase64String(buffer.InnerBuffer);
break;
case ByteString byteString:
json["value"] = Convert.ToBase64String(byteString.Span);
break;
case Integer integer:
json["value"] = integer.ToBigInteger().ToString();
break;
case Map map:
context ??= new HashSet<StackItem>(ReferenceEqualityComparer.Default);
if (!context.Add(map)) throw new InvalidOperationException();
json["value"] = new JArray(map.Select(p =>
{
JObject item = new JObject();
item["key"] = ToJson(p.Key, context);
item["value"] = ToJson(p.Value, context);
return item;
}));
break;
case Pointer pointer:
json["value"] = pointer.Position;
break;
}
return json;
}
}
}
2 changes: 1 addition & 1 deletion neo-cli/CLI/MainService.Contracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void OnInvokeCommand(UInt160 scriptHash, string operation, JArray contra
{
Console.WriteLine($"VM State: {engine.State}");
Console.WriteLine($"Gas Consumed: {new BigDecimal(engine.GasConsumed, NativeContract.GAS.Decimals)}");
Console.WriteLine($"Evaluation Stack: {new JArray(engine.ResultStack.Select(p => p.ToParameter().ToJson()))}");
Console.WriteLine($"Evaluation Stack: {new JArray(engine.ResultStack.Select(p => p.ToJson()))}");
Console.WriteLine();
if (engine.State.HasFlag(VMState.FAULT))
{
Expand Down
24 changes: 24 additions & 0 deletions neo-cli/CLI/ReferenceEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Neo.CLI
{
internal sealed class ReferenceEqualityComparer : IEqualityComparer, IEqualityComparer<object>
{
public static readonly ReferenceEqualityComparer Default = new ReferenceEqualityComparer();

private ReferenceEqualityComparer()
{
}

public new bool Equals(object x, object y)
{
return x == y;
}

public int GetHashCode(object obj)
{
return RuntimeHelpers.GetHashCode(obj);
}
}
}

0 comments on commit d53c70d

Please sign in to comment.