[Ethereum] How to handle dynamic size string array in solidity

arrayscontract-designsoliditystoragestring

I have a shop owner and a shop owner can have multiple products. I want to keep the products id in an array.

Also, A public method can return all the product id list of a shop owner.

I tried the following code

pragma solidity 0.5.0;

contract Shop {

    struct ShopOwner {
        string id;
        string[] productList;
    }

    struct Product {
        string id;
        string name;
    }

    mapping(string => ShopOwner) private mapShopOwner;

    function getUserProductList(string _shopOwnerId) public view returns(string[]) {
        return (mapShopOwner[_shopOwnerId].productList);
    }

}

This seems does not support the solidity language.

I am getting the error

browser/Structure.sol:17:33: TypeError: Data location must be "memory"
for parameter in function, but none was given.
function getUserProductList(string _userId) public view returns(string[]) {
^————^

and

browser/Structure.sol:17:69: TypeError: Data location must be "memory"
for return parameter in function, but none was given.
function getUserProductList(string _userId) public view returns(string[]) {
^——^

And I do not want to use pragma experimental ABIEncoderV2;

I wonder, what is the correct procedure to handle this types of relation.

Best Answer

Solidity cannot return string arrays in current version. One workaround is to serialize the string array and then de-serialize it on client side.

An example :

https://hackernoon.com/serializing-string-arrays-in-solidity-db4b6037e520