paint-brush
Serializing string arrays in solidityby@vasa
9,693 reads
9,693 reads

Serializing string arrays in solidity

by Vaibhav SainiJune 2nd, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Solidity is still in development and that’s why it still lacks in a lot of features that a good programming language should have. One of the features which solidity is missing is that it cannot return or take multi-dimensional arrays as input. Well, that does not seem important but when we talk about strings, we realize that they are just arbitrary-length byte array(i.e. <code class="markup--code markup--p-code">byte[]</code>). So when you create a string array, you are creating a two-dimensional byte array(i.e. <code class="markup--code markup--p-code">byte[][]</code>). This is where we all get stuck. Kinda like a trap…

Companies Mentioned

Mention Thumbnail
Mention Thumbnail

Coins Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Serializing string arrays in solidity
Vaibhav Saini HackerNoon profile picture

TL;DR This GitHub repository is all you need.

Solidity is still in development and that’s why it still lacks in a lot of features that a good programming language should have. One of the features which solidity is missing is that it cannot return or take multi-dimensional arrays as input. Well, that does not seem important but when we talk about strings, we realize that they are just arbitrary-length byte array(i.e. byte[]). So when you create a string array, you are creating a two-dimensional byte array(i.e. byte[][]). This is where we all get stuck. Kinda like a trap…

it’s a trap!

That’s enough of talking, let’s get to work. The easiest and the most efficient way to get over this problem is to serialize the strings in the array(converting the strings in the array into byte form) and send all of them in one go. After we receive the string array in the byte form we can either pass the byte array as input to another solidity function or send it to user-end(node.js api) where we will de-serialize it back to the string array. Let’s see how it works.

Step 1: Serialize & De-serialize using solidity

For serializing/de-serializing in solidity we will use Seriality which can be used to serialize and de-serialize any data type in solidity. Thanks to pouladzade.

You will get some output like this (for startindex=1, endindex=2).

0x737472696e6731000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007737472696e6732000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007737472696e6733000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007

Now, pass this bytes buffer into getString()and you will get your strings back!

Note: You have to include Seriality files to your project to make the contract working. You can do that from here.

Step 2: De-serialize the byte buffer using javascript

Now, using javascript we can get our string array back.

Putting the above byte output in the js function we get the following output.

[“string1”, “string2”]

Voila, you successfully got your array back!

easy as that!

This was just about the strings. You can do a lot of stuff using Seriality.

Learned something? Click the 👏 to say “thanks!” and help others find this article.

Hold down the clap button if you liked the content! It helps me gain exposure .

Want to learn more? Checkout my previous articles.


Quantum Computing: Is it the end of blockchain?_Experts are suggesting quantum computing may render blockchain obsolete. As the tech giants such as Google and IBM are…_hackernoon.com


EOS 101: Getting started with EOS, Part 1_The only blockchain which has blocktime of less than a second: 0.5 sec!_hackernoon.com


Difference between SideChains and State Channels_A complete comparison of the two scaling methods._hackernoon.com


Setting up your first distributed private storage network on IPFS: Part 1_IPFS Private Storage Network Series_medium.com


13 sidechain projects every blockchain developer should know about_The whole world is going through the blockchain revolution. But wait…is this really what we dreamed of? Present…_medium.com

Clap 50 times and follow me on Twitter: @vasa_develop