-
Notifications
You must be signed in to change notification settings - Fork 521
Expand file tree
/
Copy pathStdJson.sol
More file actions
331 lines (281 loc) · 14.5 KB
/
Copy pathStdJson.sol
File metadata and controls
331 lines (281 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.13 <0.9.0;
import {VmSafe} from "./Vm.sol";
// Helpers for parsing and writing JSON files.
// `key` parameters use the same selector syntax as the `vm.parseJson*` cheatcodes,
// for example `.a` for a nested field or `$` for the root object.
// To parse:
// ```
// using stdJson for string;
// string memory json = vm.readFile("<some_path>");
// uint256 value = json.readUint(".a");
// bytes memory encoded = json.parseRaw("$");
// ```
// To write:
// ```
// using stdJson for string;
// string memory json = "json";
// json.serialize("a", uint256(123));
// string memory semiFinal = json.serialize("b", string("test"));
// string memory finalJson = json.serialize("c", semiFinal);
// finalJson.write("<some_path>");
// ```
library stdJson {
VmSafe private constant vm = VmSafe(address(uint160(uint256(keccak256("hevm cheat code")))));
/// @notice Returns whether `key` exists in `json`.
/// @dev `key` uses the same selector syntax as `vm.parseJson*`, such as `.a` or `$`.
function keyExists(string memory json, string memory key) internal view returns (bool) {
return vm.keyExistsJson(json, key);
}
/// @notice ABI-encodes the JSON value selected by `key`.
/// @dev `key` uses the same selector syntax as `vm.parseJson*`, such as `.a` or `$`.
/// @dev The value's type is inferred, not declared. A hex string of exactly 20 bytes is encoded
/// as an `address` and one of exactly 32 bytes as a `bytes32`; every other length is encoded as
/// dynamic `bytes`. Decoding those two lengths as `bytes` therefore reverts, so prefer
/// `readBytes` when the value is meant to be byte data.
function parseRaw(string memory json, string memory key) internal pure returns (bytes memory) {
return vm.parseJson(json, key);
}
/// @notice Reads a uint256 value at `key` from `json`.
function readUint(string memory json, string memory key) internal pure returns (uint256) {
return vm.parseJsonUint(json, key);
}
/// @notice Reads a uint256 array at `key` from `json`.
function readUintArray(string memory json, string memory key) internal pure returns (uint256[] memory) {
return vm.parseJsonUintArray(json, key);
}
/// @notice Reads an int256 value at `key` from `json`.
function readInt(string memory json, string memory key) internal pure returns (int256) {
return vm.parseJsonInt(json, key);
}
/// @notice Reads an int256 array at `key` from `json`.
function readIntArray(string memory json, string memory key) internal pure returns (int256[] memory) {
return vm.parseJsonIntArray(json, key);
}
/// @notice Reads a bytes32 value at `key` from `json`.
function readBytes32(string memory json, string memory key) internal pure returns (bytes32) {
return vm.parseJsonBytes32(json, key);
}
/// @notice Reads a bytes32 array at `key` from `json`.
function readBytes32Array(string memory json, string memory key) internal pure returns (bytes32[] memory) {
return vm.parseJsonBytes32Array(json, key);
}
/// @notice Reads a string value at `key` from `json`.
function readString(string memory json, string memory key) internal pure returns (string memory) {
return vm.parseJsonString(json, key);
}
/// @notice Reads a string array at `key` from `json`.
function readStringArray(string memory json, string memory key) internal pure returns (string[] memory) {
return vm.parseJsonStringArray(json, key);
}
/// @notice Reads an address value at `key` from `json`.
function readAddress(string memory json, string memory key) internal pure returns (address) {
return vm.parseJsonAddress(json, key);
}
/// @notice Reads an address array at `key` from `json`.
function readAddressArray(string memory json, string memory key) internal pure returns (address[] memory) {
return vm.parseJsonAddressArray(json, key);
}
/// @notice Reads a bool value at `key` from `json`.
function readBool(string memory json, string memory key) internal pure returns (bool) {
return vm.parseJsonBool(json, key);
}
/// @notice Reads a bool array at `key` from `json`.
function readBoolArray(string memory json, string memory key) internal pure returns (bool[] memory) {
return vm.parseJsonBoolArray(json, key);
}
/// @notice Reads a bytes value at `key` from `json`.
function readBytes(string memory json, string memory key) internal pure returns (bytes memory) {
return vm.parseJsonBytes(json, key);
}
/// @notice Reads a bytes array at `key` from `json`.
function readBytesArray(string memory json, string memory key) internal pure returns (bytes[] memory) {
return vm.parseJsonBytesArray(json, key);
}
/// @notice Reads a uint256 value at `key` from `json`, returning `defaultValue` if the key does not exist.
function readUintOr(string memory json, string memory key, uint256 defaultValue) internal view returns (uint256) {
return keyExists(json, key) ? readUint(json, key) : defaultValue;
}
/// @notice Reads a uint256 array at `key` from `json`, returning `defaultValue` if the key does not exist.
function readUintArrayOr(string memory json, string memory key, uint256[] memory defaultValue)
internal
view
returns (uint256[] memory)
{
return keyExists(json, key) ? readUintArray(json, key) : defaultValue;
}
/// @notice Reads an int256 value at `key` from `json`, returning `defaultValue` if the key does not exist.
function readIntOr(string memory json, string memory key, int256 defaultValue) internal view returns (int256) {
return keyExists(json, key) ? readInt(json, key) : defaultValue;
}
/// @notice Reads an int256 array at `key` from `json`, returning `defaultValue` if the key does not exist.
function readIntArrayOr(string memory json, string memory key, int256[] memory defaultValue)
internal
view
returns (int256[] memory)
{
return keyExists(json, key) ? readIntArray(json, key) : defaultValue;
}
/// @notice Reads a bytes32 value at `key` from `json`, returning `defaultValue` if the key does not exist.
function readBytes32Or(string memory json, string memory key, bytes32 defaultValue)
internal
view
returns (bytes32)
{
return keyExists(json, key) ? readBytes32(json, key) : defaultValue;
}
/// @notice Reads a bytes32 array at `key` from `json`, returning `defaultValue` if the key does not exist.
function readBytes32ArrayOr(string memory json, string memory key, bytes32[] memory defaultValue)
internal
view
returns (bytes32[] memory)
{
return keyExists(json, key) ? readBytes32Array(json, key) : defaultValue;
}
/// @notice Reads a string value at `key` from `json`, returning `defaultValue` if the key does not exist.
function readStringOr(string memory json, string memory key, string memory defaultValue)
internal
view
returns (string memory)
{
return keyExists(json, key) ? readString(json, key) : defaultValue;
}
/// @notice Reads a string array at `key` from `json`, returning `defaultValue` if the key does not exist.
function readStringArrayOr(string memory json, string memory key, string[] memory defaultValue)
internal
view
returns (string[] memory)
{
return keyExists(json, key) ? readStringArray(json, key) : defaultValue;
}
/// @notice Reads an address value at `key` from `json`, returning `defaultValue` if the key does not exist.
function readAddressOr(string memory json, string memory key, address defaultValue)
internal
view
returns (address)
{
return keyExists(json, key) ? readAddress(json, key) : defaultValue;
}
/// @notice Reads an address array at `key` from `json`, returning `defaultValue` if the key does not exist.
function readAddressArrayOr(string memory json, string memory key, address[] memory defaultValue)
internal
view
returns (address[] memory)
{
return keyExists(json, key) ? readAddressArray(json, key) : defaultValue;
}
/// @notice Reads a bool value at `key` from `json`, returning `defaultValue` if the key does not exist.
function readBoolOr(string memory json, string memory key, bool defaultValue) internal view returns (bool) {
return keyExists(json, key) ? readBool(json, key) : defaultValue;
}
/// @notice Reads a bool array at `key` from `json`, returning `defaultValue` if the key does not exist.
function readBoolArrayOr(string memory json, string memory key, bool[] memory defaultValue)
internal
view
returns (bool[] memory)
{
return keyExists(json, key) ? readBoolArray(json, key) : defaultValue;
}
/// @notice Reads a bytes value at `key` from `json`, returning `defaultValue` if the key does not exist.
function readBytesOr(string memory json, string memory key, bytes memory defaultValue)
internal
view
returns (bytes memory)
{
return keyExists(json, key) ? readBytes(json, key) : defaultValue;
}
/// @notice Reads a bytes array at `key` from `json`, returning `defaultValue` if the key does not exist.
function readBytesArrayOr(string memory json, string memory key, bytes[] memory defaultValue)
internal
view
returns (bytes[] memory)
{
return keyExists(json, key) ? readBytesArray(json, key) : defaultValue;
}
/// @notice Serializes a JSON object `rootObject` under `jsonKey` and returns the serialized JSON string.
function serialize(string memory jsonKey, string memory rootObject) internal returns (string memory) {
return vm.serializeJson(jsonKey, rootObject);
}
/// @notice Serializes a bool `value` under `key` within `jsonKey` and returns the serialized JSON string.
function serialize(string memory jsonKey, string memory key, bool value) internal returns (string memory) {
return vm.serializeBool(jsonKey, key, value);
}
/// @notice Serializes a bool array `value` under `key` within `jsonKey` and returns the serialized JSON string.
function serialize(string memory jsonKey, string memory key, bool[] memory value) internal returns (string memory) {
return vm.serializeBool(jsonKey, key, value);
}
/// @notice Serializes a uint256 `value` under `key` within `jsonKey` and returns the serialized JSON string.
function serialize(string memory jsonKey, string memory key, uint256 value) internal returns (string memory) {
return vm.serializeUint(jsonKey, key, value);
}
/// @notice Serializes a uint256 array `value` under `key` within `jsonKey` and returns the serialized JSON string.
function serialize(string memory jsonKey, string memory key, uint256[] memory value)
internal
returns (string memory)
{
return vm.serializeUint(jsonKey, key, value);
}
/// @notice Serializes an int256 `value` under `key` within `jsonKey` and returns the serialized JSON string.
function serialize(string memory jsonKey, string memory key, int256 value) internal returns (string memory) {
return vm.serializeInt(jsonKey, key, value);
}
/// @notice Serializes an int256 array `value` under `key` within `jsonKey` and returns the serialized JSON string.
function serialize(string memory jsonKey, string memory key, int256[] memory value)
internal
returns (string memory)
{
return vm.serializeInt(jsonKey, key, value);
}
/// @notice Serializes an address `value` under `key` within `jsonKey` and returns the serialized JSON string.
function serialize(string memory jsonKey, string memory key, address value) internal returns (string memory) {
return vm.serializeAddress(jsonKey, key, value);
}
/// @notice Serializes an address array `value` under `key` within `jsonKey` and returns the serialized JSON string.
function serialize(string memory jsonKey, string memory key, address[] memory value)
internal
returns (string memory)
{
return vm.serializeAddress(jsonKey, key, value);
}
/// @notice Serializes a bytes32 `value` under `key` within `jsonKey` and returns the serialized JSON string.
function serialize(string memory jsonKey, string memory key, bytes32 value) internal returns (string memory) {
return vm.serializeBytes32(jsonKey, key, value);
}
/// @notice Serializes a bytes32 array `value` under `key` within `jsonKey` and returns the serialized JSON string.
function serialize(string memory jsonKey, string memory key, bytes32[] memory value)
internal
returns (string memory)
{
return vm.serializeBytes32(jsonKey, key, value);
}
/// @notice Serializes a bytes `value` under `key` within `jsonKey` and returns the serialized JSON string.
function serialize(string memory jsonKey, string memory key, bytes memory value) internal returns (string memory) {
return vm.serializeBytes(jsonKey, key, value);
}
/// @notice Serializes a bytes array `value` under `key` within `jsonKey` and returns the serialized JSON string.
function serialize(string memory jsonKey, string memory key, bytes[] memory value)
internal
returns (string memory)
{
return vm.serializeBytes(jsonKey, key, value);
}
/// @notice Serializes a string `value` under `key` within `jsonKey` and returns the serialized JSON string.
function serialize(string memory jsonKey, string memory key, string memory value) internal returns (string memory) {
return vm.serializeString(jsonKey, key, value);
}
/// @notice Serializes a string array `value` under `key` within `jsonKey` and returns the serialized JSON string.
function serialize(string memory jsonKey, string memory key, string[] memory value)
internal
returns (string memory)
{
return vm.serializeString(jsonKey, key, value);
}
/// @notice Writes the serialized JSON object `jsonKey` to `path`.
function write(string memory jsonKey, string memory path) internal {
vm.writeJson(jsonKey, path);
}
/// @notice Writes the value at `valueKey` from the serialized JSON object `jsonKey` to `path`.
function write(string memory jsonKey, string memory path, string memory valueKey) internal {
vm.writeJson(jsonKey, path, valueKey);
}
}