arnaucube
arnaucube/poseidon-solidity-examplePublic Self-hosted repository
poseidon-solidity-example/lib/forge-std/src/StdJson.sol
lib/forge-std/src/StdJson.solRaw
  1// SPDX-License-Identifier: MIT
  2pragma solidity >=0.6.0 <0.9.0;
  3
  4pragma experimental ABIEncoderV2;
  5
  6import {VmSafe} from "./Vm.sol";
  7
  8// Helpers for parsing and writing JSON files
  9// To parse:
 10// ```
 11// using stdJson for string;
 12// string memory json = vm.readFile("some_peth");
 13// json.parseUint("<json_path>");
 14// ```
 15// To write:
 16// ```
 17// using stdJson for string;
 18// string memory json = "deploymentArtifact";
 19// Contract contract = new Contract();
 20// json.serialize("contractAddress", address(contract));
 21// json = json.serialize("deploymentTimes", uint(1));
 22// // store the stringified JSON to the 'json' variable we have been using as a key
 23// // as we won't need it any longer
 24// string memory json2 = "finalArtifact";
 25// string memory final = json2.serialize("depArtifact", json);
 26// final.write("<some_path>");
 27// ```
 28
 29library stdJson {
 30    VmSafe private constant vm = VmSafe(address(uint160(uint256(keccak256("hevm cheat code")))));
 31
 32    function parseRaw(string memory json, string memory key) internal pure returns (bytes memory) {
 33        return vm.parseJson(json, key);
 34    }
 35
 36    function readUint(string memory json, string memory key) internal pure returns (uint256) {
 37        return abi.decode(vm.parseJson(json, key), (uint256));
 38    }
 39
 40    function readUintArray(string memory json, string memory key) internal pure returns (uint256[] memory) {
 41        return abi.decode(vm.parseJson(json, key), (uint256[]));
 42    }
 43
 44    function readInt(string memory json, string memory key) internal pure returns (int256) {
 45        return abi.decode(vm.parseJson(json, key), (int256));
 46    }
 47
 48    function readIntArray(string memory json, string memory key) internal pure returns (int256[] memory) {
 49        return abi.decode(vm.parseJson(json, key), (int256[]));
 50    }
 51
 52    function readBytes32(string memory json, string memory key) internal pure returns (bytes32) {
 53        return abi.decode(vm.parseJson(json, key), (bytes32));
 54    }
 55
 56    function readBytes32Array(string memory json, string memory key) internal pure returns (bytes32[] memory) {
 57        return abi.decode(vm.parseJson(json, key), (bytes32[]));
 58    }
 59
 60    function readString(string memory json, string memory key) internal pure returns (string memory) {
 61        return abi.decode(vm.parseJson(json, key), (string));
 62    }
 63
 64    function readStringArray(string memory json, string memory key) internal pure returns (string[] memory) {
 65        return abi.decode(vm.parseJson(json, key), (string[]));
 66    }
 67
 68    function readAddress(string memory json, string memory key) internal pure returns (address) {
 69        return abi.decode(vm.parseJson(json, key), (address));
 70    }
 71
 72    function readAddressArray(string memory json, string memory key) internal pure returns (address[] memory) {
 73        return abi.decode(vm.parseJson(json, key), (address[]));
 74    }
 75
 76    function readBool(string memory json, string memory key) internal pure returns (bool) {
 77        return abi.decode(vm.parseJson(json, key), (bool));
 78    }
 79
 80    function readBoolArray(string memory json, string memory key) internal pure returns (bool[] memory) {
 81        return abi.decode(vm.parseJson(json, key), (bool[]));
 82    }
 83
 84    function readBytes(string memory json, string memory key) internal pure returns (bytes memory) {
 85        return abi.decode(vm.parseJson(json, key), (bytes));
 86    }
 87
 88    function readBytesArray(string memory json, string memory key) internal pure returns (bytes[] memory) {
 89        return abi.decode(vm.parseJson(json, key), (bytes[]));
 90    }
 91
 92    function serialize(string memory jsonKey, string memory key, bool value) internal returns (string memory) {
 93        return vm.serializeBool(jsonKey, key, value);
 94    }
 95
 96    function serialize(string memory jsonKey, string memory key, bool[] memory value)
 97        internal
 98        returns (string memory)
 99    {
100        return vm.serializeBool(jsonKey, key, value);
101    }
102
103    function serialize(string memory jsonKey, string memory key, uint256 value) internal returns (string memory) {
104        return vm.serializeUint(jsonKey, key, value);
105    }
106
107    function serialize(string memory jsonKey, string memory key, uint256[] memory value)
108        internal
109        returns (string memory)
110    {
111        return vm.serializeUint(jsonKey, key, value);
112    }
113
114    function serialize(string memory jsonKey, string memory key, int256 value) internal returns (string memory) {
115        return vm.serializeInt(jsonKey, key, value);
116    }
117
118    function serialize(string memory jsonKey, string memory key, int256[] memory value)
119        internal
120        returns (string memory)
121    {
122        return vm.serializeInt(jsonKey, key, value);
123    }
124
125    function serialize(string memory jsonKey, string memory key, address value) internal returns (string memory) {
126        return vm.serializeAddress(jsonKey, key, value);
127    }
128
129    function serialize(string memory jsonKey, string memory key, address[] memory value)
130        internal
131        returns (string memory)
132    {
133        return vm.serializeAddress(jsonKey, key, value);
134    }
135
136    function serialize(string memory jsonKey, string memory key, bytes32 value) internal returns (string memory) {
137        return vm.serializeBytes32(jsonKey, key, value);
138    }
139
140    function serialize(string memory jsonKey, string memory key, bytes32[] memory value)
141        internal
142        returns (string memory)
143    {
144        return vm.serializeBytes32(jsonKey, key, value);
145    }
146
147    function serialize(string memory jsonKey, string memory key, bytes memory value) internal returns (string memory) {
148        return vm.serializeBytes(jsonKey, key, value);
149    }
150
151    function serialize(string memory jsonKey, string memory key, bytes[] memory value)
152        internal
153        returns (string memory)
154    {
155        return vm.serializeBytes(jsonKey, key, value);
156    }
157
158    function serialize(string memory jsonKey, string memory key, string memory value)
159        internal
160        returns (string memory)
161    {
162        return vm.serializeString(jsonKey, key, value);
163    }
164
165    function serialize(string memory jsonKey, string memory key, string[] memory value)
166        internal
167        returns (string memory)
168    {
169        return vm.serializeString(jsonKey, key, value);
170    }
171
172    function write(string memory jsonKey, string memory path) internal {
173        vm.writeJson(jsonKey, path);
174    }
175
176    function write(string memory jsonKey, string memory path, string memory valueKey) internal {
177        vm.writeJson(jsonKey, path, valueKey);
178    }
179}