contracts/lib/forge-std/src/StdJson.sol 9.5 K raw
1
// SPDX-License-Identifier: MIT
2
pragma solidity >=0.6.0 <0.9.0;
3
4
pragma experimental ABIEncoderV2;
5
6
import {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_path>");
13
// json.readUint("<json_path>");
14
// ```
15
// To write:
16
// ```
17
// using stdJson for string;
18
// string memory json = "json";
19
// json.serialize("a", uint256(123));
20
// string memory semiFinal = json.serialize("b", string("test"));
21
// string memory finalJson = json.serialize("c", semiFinal);
22
// finalJson.write("<some_path>");
23
// ```
24
25
library stdJson {
26
    VmSafe private constant vm = VmSafe(address(uint160(uint256(keccak256("hevm cheat code")))));
27
28
    function keyExists(string memory json, string memory key) internal view returns (bool) {
29
        return vm.keyExistsJson(json, key);
30
    }
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 vm.parseJsonUint(json, key);
38
    }
39
40
    function readUintArray(string memory json, string memory key) internal pure returns (uint256[] memory) {
41
        return vm.parseJsonUintArray(json, key);
42
    }
43
44
    function readInt(string memory json, string memory key) internal pure returns (int256) {
45
        return vm.parseJsonInt(json, key);
46
    }
47
48
    function readIntArray(string memory json, string memory key) internal pure returns (int256[] memory) {
49
        return vm.parseJsonIntArray(json, key);
50
    }
51
52
    function readBytes32(string memory json, string memory key) internal pure returns (bytes32) {
53
        return vm.parseJsonBytes32(json, key);
54
    }
55
56
    function readBytes32Array(string memory json, string memory key) internal pure returns (bytes32[] memory) {
57
        return vm.parseJsonBytes32Array(json, key);
58
    }
59
60
    function readString(string memory json, string memory key) internal pure returns (string memory) {
61
        return vm.parseJsonString(json, key);
62
    }
63
64
    function readStringArray(string memory json, string memory key) internal pure returns (string[] memory) {
65
        return vm.parseJsonStringArray(json, key);
66
    }
67
68
    function readAddress(string memory json, string memory key) internal pure returns (address) {
69
        return vm.parseJsonAddress(json, key);
70
    }
71
72
    function readAddressArray(string memory json, string memory key) internal pure returns (address[] memory) {
73
        return vm.parseJsonAddressArray(json, key);
74
    }
75
76
    function readBool(string memory json, string memory key) internal pure returns (bool) {
77
        return vm.parseJsonBool(json, key);
78
    }
79
80
    function readBoolArray(string memory json, string memory key) internal pure returns (bool[] memory) {
81
        return vm.parseJsonBoolArray(json, key);
82
    }
83
84
    function readBytes(string memory json, string memory key) internal pure returns (bytes memory) {
85
        return vm.parseJsonBytes(json, key);
86
    }
87
88
    function readBytesArray(string memory json, string memory key) internal pure returns (bytes[] memory) {
89
        return vm.parseJsonBytesArray(json, key);
90
    }
91
92
    function readUintOr(string memory json, string memory key, uint256 defaultValue) internal view returns (uint256) {
93
        return keyExists(json, key) ? readUint(json, key) : defaultValue;
94
    }
95
96
    function readUintArrayOr(string memory json, string memory key, uint256[] memory defaultValue)
97
        internal
98
        view
99
        returns (uint256[] memory)
100
    {
101
        return keyExists(json, key) ? readUintArray(json, key) : defaultValue;
102
    }
103
104
    function readIntOr(string memory json, string memory key, int256 defaultValue) internal view returns (int256) {
105
        return keyExists(json, key) ? readInt(json, key) : defaultValue;
106
    }
107
108
    function readIntArrayOr(string memory json, string memory key, int256[] memory defaultValue)
109
        internal
110
        view
111
        returns (int256[] memory)
112
    {
113
        return keyExists(json, key) ? readIntArray(json, key) : defaultValue;
114
    }
115
116
    function readBytes32Or(string memory json, string memory key, bytes32 defaultValue)
117
        internal
118
        view
119
        returns (bytes32)
120
    {
121
        return keyExists(json, key) ? readBytes32(json, key) : defaultValue;
122
    }
123
124
    function readBytes32ArrayOr(string memory json, string memory key, bytes32[] memory defaultValue)
125
        internal
126
        view
127
        returns (bytes32[] memory)
128
    {
129
        return keyExists(json, key) ? readBytes32Array(json, key) : defaultValue;
130
    }
131
132
    function readStringOr(string memory json, string memory key, string memory defaultValue)
133
        internal
134
        view
135
        returns (string memory)
136
    {
137
        return keyExists(json, key) ? readString(json, key) : defaultValue;
138
    }
139
140
    function readStringArrayOr(string memory json, string memory key, string[] memory defaultValue)
141
        internal
142
        view
143
        returns (string[] memory)
144
    {
145
        return keyExists(json, key) ? readStringArray(json, key) : defaultValue;
146
    }
147
148
    function readAddressOr(string memory json, string memory key, address defaultValue)
149
        internal
150
        view
151
        returns (address)
152
    {
153
        return keyExists(json, key) ? readAddress(json, key) : defaultValue;
154
    }
155
156
    function readAddressArrayOr(string memory json, string memory key, address[] memory defaultValue)
157
        internal
158
        view
159
        returns (address[] memory)
160
    {
161
        return keyExists(json, key) ? readAddressArray(json, key) : defaultValue;
162
    }
163
164
    function readBoolOr(string memory json, string memory key, bool defaultValue) internal view returns (bool) {
165
        return keyExists(json, key) ? readBool(json, key) : defaultValue;
166
    }
167
168
    function readBoolArrayOr(string memory json, string memory key, bool[] memory defaultValue)
169
        internal
170
        view
171
        returns (bool[] memory)
172
    {
173
        return keyExists(json, key) ? readBoolArray(json, key) : defaultValue;
174
    }
175
176
    function readBytesOr(string memory json, string memory key, bytes memory defaultValue)
177
        internal
178
        view
179
        returns (bytes memory)
180
    {
181
        return keyExists(json, key) ? readBytes(json, key) : defaultValue;
182
    }
183
184
    function readBytesArrayOr(string memory json, string memory key, bytes[] memory defaultValue)
185
        internal
186
        view
187
        returns (bytes[] memory)
188
    {
189
        return keyExists(json, key) ? readBytesArray(json, key) : defaultValue;
190
    }
191
192
    function serialize(string memory jsonKey, string memory rootObject) internal returns (string memory) {
193
        return vm.serializeJson(jsonKey, rootObject);
194
    }
195
196
    function serialize(string memory jsonKey, string memory key, bool value) internal returns (string memory) {
197
        return vm.serializeBool(jsonKey, key, value);
198
    }
199
200
    function serialize(string memory jsonKey, string memory key, bool[] memory value)
201
        internal
202
        returns (string memory)
203
    {
204
        return vm.serializeBool(jsonKey, key, value);
205
    }
206
207
    function serialize(string memory jsonKey, string memory key, uint256 value) internal returns (string memory) {
208
        return vm.serializeUint(jsonKey, key, value);
209
    }
210
211
    function serialize(string memory jsonKey, string memory key, uint256[] memory value)
212
        internal
213
        returns (string memory)
214
    {
215
        return vm.serializeUint(jsonKey, key, value);
216
    }
217
218
    function serialize(string memory jsonKey, string memory key, int256 value) internal returns (string memory) {
219
        return vm.serializeInt(jsonKey, key, value);
220
    }
221
222
    function serialize(string memory jsonKey, string memory key, int256[] memory value)
223
        internal
224
        returns (string memory)
225
    {
226
        return vm.serializeInt(jsonKey, key, value);
227
    }
228
229
    function serialize(string memory jsonKey, string memory key, address value) internal returns (string memory) {
230
        return vm.serializeAddress(jsonKey, key, value);
231
    }
232
233
    function serialize(string memory jsonKey, string memory key, address[] memory value)
234
        internal
235
        returns (string memory)
236
    {
237
        return vm.serializeAddress(jsonKey, key, value);
238
    }
239
240
    function serialize(string memory jsonKey, string memory key, bytes32 value) internal returns (string memory) {
241
        return vm.serializeBytes32(jsonKey, key, value);
242
    }
243
244
    function serialize(string memory jsonKey, string memory key, bytes32[] memory value)
245
        internal
246
        returns (string memory)
247
    {
248
        return vm.serializeBytes32(jsonKey, key, value);
249
    }
250
251
    function serialize(string memory jsonKey, string memory key, bytes memory value) internal returns (string memory) {
252
        return vm.serializeBytes(jsonKey, key, value);
253
    }
254
255
    function serialize(string memory jsonKey, string memory key, bytes[] memory value)
256
        internal
257
        returns (string memory)
258
    {
259
        return vm.serializeBytes(jsonKey, key, value);
260
    }
261
262
    function serialize(string memory jsonKey, string memory key, string memory value)
263
        internal
264
        returns (string memory)
265
    {
266
        return vm.serializeString(jsonKey, key, value);
267
    }
268
269
    function serialize(string memory jsonKey, string memory key, string[] memory value)
270
        internal
271
        returns (string memory)
272
    {
273
        return vm.serializeString(jsonKey, key, value);
274
    }
275
276
    function write(string memory jsonKey, string memory path) internal {
277
        vm.writeJson(jsonKey, path);
278
    }
279
280
    function write(string memory jsonKey, string memory path, string memory valueKey) internal {
281
        vm.writeJson(jsonKey, path, valueKey);
282
    }
283
}