Skip to content

Commit

Permalink
Fix #1714 (#1715)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored Jun 23, 2020
1 parent 8f509db commit 2047892
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
12 changes: 7 additions & 5 deletions src/neo/SmartContract/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Text.Json;
using Array = Neo.VM.Types.Array;
using Boolean = Neo.VM.Types.Boolean;
using Buffer = Neo.VM.Types.Buffer;

namespace Neo.SmartContract
{
Expand All @@ -27,9 +28,10 @@ public static JObject Serialize(StackItem item)
{
return array.Select(p => Serialize(p)).ToArray();
}
case ByteString buffer:
case ByteString _:
case Buffer _:
{
return Convert.ToBase64String(buffer.GetSpan());
return item.GetString();
}
case Integer num:
{
Expand Down Expand Up @@ -89,8 +91,8 @@ public static byte[] SerializeToByteArray(StackItem item, uint maxSize)
case JsonTokenType.EndArray:
writer.WriteEndArray();
break;
case ByteString buffer:
writer.WriteStringValue(Convert.ToBase64String(buffer.GetSpan()));
case StackItem buffer when buffer is ByteString || buffer is Buffer:
writer.WriteStringValue(buffer.GetString());
break;
case Integer num:
{
Expand Down Expand Up @@ -118,7 +120,7 @@ public static byte[] SerializeToByteArray(StackItem item, uint maxSize)
writer.WriteEndObject();
break;
case JsonTokenType.PropertyName:
writer.WritePropertyName(((ByteString)stack.Pop()).GetString());
writer.WritePropertyName(((StackItem)stack.Pop()).GetString());
break;
case Null _:
writer.WriteNullValue();
Expand Down
4 changes: 2 additions & 2 deletions tests/neo.UnitTests/SmartContract/UT_JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public void Serialize_Array_Bool_Str_Num()

var json = JsonSerializer.Serialize(entry).ToString();

Assert.AreEqual(json, "[true,\"dGVzdA==\",123]");
Assert.AreEqual(json, "[true,\"test\",123]");
}

[TestMethod]
Expand Down Expand Up @@ -306,7 +306,7 @@ public void Serialize_Array_OfArray()

var json = JsonSerializer.Serialize(entry).ToString();

Assert.AreEqual(json, "[[true,\"dGVzdDE=\",123],[true,\"dGVzdDI=\",321]]");
Assert.AreEqual(json, "[[true,\"test1\",123],[true,\"test2\",321]]");
}

[TestMethod]
Expand Down
21 changes: 8 additions & 13 deletions tests/neo.UnitTests/SmartContract/UT_Syscalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,14 @@ public void System_Blockchain_GetBlock()

height.Index = block.Index;

script.EmitSysCall(ApplicationEngine.System_Json_Serialize);
engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);
engine.LoadScript(script.ToArray());

Assert.AreEqual(engine.Execute(), VMState.HALT);
Assert.AreEqual(1, engine.ResultStack.Count);
Assert.IsInstanceOfType(engine.ResultStack.Peek(), typeof(ByteString));
Assert.AreEqual(engine.ResultStack.Pop().GetSpan().ToHexString(),
"5b2261564e62466b35384f51717547373870747154766561762f48677941566a72634e41434d4e59705c7530303242366f6f3d222c332c22414141414141414141414141414141414141414141414141414141414141414141414141414141414141413d222c22414141414141414141414141414141414141414141414141414141414141414141414141414141414141413d222c322c302c224141414141414141414141414141414141414141414141414141413d222c315d");
Assert.AreEqual(0, engine.ResultStack.Count);

var array = engine.ResultStack.Pop<VM.Types.Array>();
Assert.AreEqual(block.Hash, new UInt256(array[0].GetSpan()));

// Clean

Expand Down Expand Up @@ -194,9 +192,9 @@ public void Json_Serialize()
Assert.AreEqual(engine.Execute(), VMState.HALT);
Assert.AreEqual(5, engine.ResultStack.Count);

Assert.IsTrue(engine.ResultStack.Pop<ByteString>().GetString() == "{\"key\":\"dmFsdWU=\"}");
Assert.IsTrue(engine.ResultStack.Pop<ByteString>().GetString() == "{\"key\":\"value\"}");
Assert.IsTrue(engine.ResultStack.Pop<ByteString>().GetString() == "null");
Assert.IsTrue(engine.ResultStack.Pop<ByteString>().GetString() == "\"dGVzdA==\"");
Assert.IsTrue(engine.ResultStack.Pop<ByteString>().GetString() == "\"test\"");
Assert.IsTrue(engine.ResultStack.Pop<ByteString>().GetString() == "true");
Assert.IsTrue(engine.ResultStack.Pop<ByteString>().GetString() == "5");
}
Expand Down Expand Up @@ -238,8 +236,6 @@ public void System_ExecutionEngine_GetScriptContainer()

// With tx

script.EmitSysCall(ApplicationEngine.System_Json_Serialize);

var tx = new Transaction()
{
Script = new byte[] { 0x01 },
Expand All @@ -258,10 +254,9 @@ public void System_ExecutionEngine_GetScriptContainer()

Assert.AreEqual(engine.Execute(), VMState.HALT);
Assert.AreEqual(1, engine.ResultStack.Count);
Assert.IsInstanceOfType(engine.ResultStack.Peek(), typeof(ByteString));
Assert.AreEqual(engine.ResultStack.Pop().GetSpan().ToHexString(),
@"5b224435724a376f755c753030324256574845456c5c75303032426e74486b414a424f614c4a6737496776303356337a4953646d6750413d222c362c342c222f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f383d222c332c322c352c2241513d3d225d");
Assert.AreEqual(0, engine.ResultStack.Count);

var array = engine.ResultStack.Pop<VM.Types.Array>();
Assert.AreEqual(tx.Hash, new UInt256(array[0].GetSpan()));
}
}

Expand Down

0 comments on commit 2047892

Please sign in to comment.