1.1.0 • Published 3 years ago

solidity-json-writer v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Solidity JSON Writer

npm version status

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"
    ]
}

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.

1.1.0

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago