npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

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

ERC-721 Metadata Implementation

When using the library to generate JSON for ERC-721 metadata, there are a few things to take into account:

  1. If the generated JSON for metadata does not contain RFC-3986 reserved characters, data:application/json, should be prepended to that JSON.
  2. 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.