jsmnSol icon indicating copy to clipboard operation
jsmnSol copied to clipboard

Feature request: generate JSON from a Solidity struct

Open zlatinov opened this issue 8 years ago • 3 comments

This would make your library cover both parsing and generation and will make it a full JSON solution for Solidity.

The generation of JSON will help with the communication between smart contracts and the DAPPs. For example:

struct Book {
	uint id;
	string name;
}

struct Artist {
	uint id;
	string name;
	Book[] books;
}

Artist[] artists;

It will make reading variables like "artists" a lot easier from the JavaScript front-end, if you can read it fully as JSON instead of looping separate calls and having to define special getters in Solidity.

zlatinov avatar Apr 04 '17 09:04 zlatinov

While I really like the idea it is unfortunately impractical right now: Solidity does not allow for reflection, i.e. it is impossible to retrieve a variable's name or type during runtime. This makes it impossible to generate useful JSON strings.

As such, unless I find a really clever way to provide all that meta data, this feature request is nothing that I can do at the moment. I will leave it open for the meantime, because the idea is obviously useful. However, I'll label it with «maybe» to indicate that it's not on my list right now.

chrisdotn avatar Apr 26 '17 13:04 chrisdotn

You can refer to this protobuf implements in soldity: , don't know if it helps you, it can use the protobuf encoding and decoding of the Soldity struct type.

mindcarver avatar Sep 02 '21 04:09 mindcarver

Thanks @mindcarver . Hi from 2023. here is the perm link of your protobuf implements in solidity: https://github.com/hyperledger-labs/yui-ibc-solidity/blob/05047b73f12020ed671b8ca9d9e3d188d5befbd5/contracts/core/types/App.sol

xinbenlv avatar Oct 04 '23 19:10 xinbenlv