solidity-json-writer
v1.2.0
Published
A library to aid in the generation and construction of JSON for smart contract development.
Downloads
22
Readme
Solidity JSON Writer
A library to aid in the generation and construction of JSON for smart contract development.
Use the library to generate RFC-7159 compliant JSON from within a smart contract.
Requirements
- Solidity 0.8+
Installation
$ npm install solidity-json-writer
Usage
Once installed, you can use the library by importing it:
pragma solidity ^0.8.0;
import "solidity-json-writer/contracts/JsonWriter.sol";
contract ExampleContract {
using JsonWriter for JsonWriter.Json;
function generateJSON() external pure returns (string memory) {
JsonWriter.Json memory writer;
writer = writer.writeStartObject();
writer = writer.writeStringProperty("Product", "PC");
writer = writer.writeUintProperty("YearsOld", 5);
writer = writer.writeIntProperty("LowestTemp", -30);
writer = writer.writeStringProperty("CPU", "Intel");
writer = writer.writeStartArray("Drives");
writer = writer.writeStringValue("500 gigabyte SSD");
writer = writer.writeStringValue("2 terabyte hard drive");
writer = writer.writeEndArray();
writer = writer.writeEndObject();
return writer.value;
}
}
Output:
{"Product": "PC","YearsOld": 5,"LowestTemp": -30,"CPU": "Intel","Drives": ["500 gigabyte SSD","2 terabyte hard drive"]}
Note: In order to optimize gas, the JSON generated within the smart contract is not pretty-printed.
Pretty-printed output:
{
"Product": "PC",
"YearsOld": 5,
"LowestTemp": -30,
"CPU": "Intel",
"Drives": [
"500 gigabyte SSD",
"2 terabyte hard drive"
]
}
ERC-721 Metadata Implementation
When using the library to generate JSON for ERC-721 metadata, there are a few things to take into account:
- If the generated JSON for metadata does not contain RFC-3986 reserved characters,
data:application/json,
should be prepended to that JSON. - If the metadata does contain reserved characters, prepend
data:application/json;base64
and then encode the generated JSON as base64.
JsonWriter supports the following Solidity primitives:
address
bool
int
string
uint
Although the concept of null
does not exist within Solidity, JsonWriter is capable of generating properties and values of null
.
The full API documentation for JsonWriter can be found in the docs.
License
JsonWriter is released under the MIT License.