-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Confusion about Neo.Json.Serialize #1191
Comments
I prefer option 2
My reasoning is that byte arrays should be represented as byte arrays, not with some strange encoding in front of it. If the byte array data happens to be decodable to a human readable string that's an extra and up to the user. |
I'm in favor of option 2 too. |
I prefer option 2 too |
Looks like option 2 is better indeed, since string will be representing a bytearray as an explicit hexstring... it takes more space, but it's much more readable. And we cannot affort to have crazy chars inside json anyway. Good idea. |
Usually in json is used base64 instead of Hex, did you consider the use of base64? |
Base64 has a higher compression ratio. I like it. |
Base64 adds overhead both on processing and size (ref)
In [1]: import base64
In [2]: array = b'\x11'*64
In [3]: array
Out[3]: b'\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11\x11'
In [4]: base64.b64encode(array)
Out[5]: b'EREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREREQ=='
In [5]: len(base64.b64encode(array))
Out[5]: 88
In [6]: len(array)
Out[6]: 64 |
Base64 encodes 6bit into one char. Hex encodes 4 bits into one char. There is no byte array in JSON, so we have to encode byte array into string. Either use base64 or hex. |
oh right forgot the output has to be a string, still early morning 😅 |
In InteropService, there are two SYSCALLs about JSON,
Neo.Json.Serialize
andNeo.Json.Deserialize
.Neo.Json.Serialize
convertsStackItem
s to JSON objects. Everything looks great, the only problem is how to convertByteArray
.We know in NeoVM, there is no differece between byte array and string. They are both stored in a
ByteArray
. Currently we useByteArray.GetString()
, and convert it to aJString
. That means we have the following conversion:The first conversion is what we need, but we prefer the second one to be:
If we use
ByteArray.GetByteArray()
and then useToHexString()
, we get the following conversion:This time, we are pleased with the second conversion, but the first conversion for
test
is not good.We need to decide which way to use.
Option 1:
ByteArray.GetString()
Option 2:
ByteArray.GetByteArray()
and thenToHexString()
Option 3: Create new JSON APIs. Such as
Neo.Json.NewJArray
,Neo.Json.AddItem
,Neo.Json.NewJObject
,Neo.Json.AddProperty
.The text was updated successfully, but these errors were encountered: