@@ -25,6 +25,10 @@ import {VmSafe} from "./Vm.sol";
2525library stdJson {
2626 VmSafe private constant vm = VmSafe (address (uint160 (uint256 (keccak256 ("hevm cheat code " )))));
2727
28+ function keyExists (string memory json , string memory key ) internal view returns (bool ) {
29+ return vm.keyExistsJson (json, key);
30+ }
31+
2832 function parseRaw (string memory json , string memory key ) internal pure returns (bytes memory ) {
2933 return vm.parseJson (json, key);
3034 }
@@ -85,6 +89,106 @@ library stdJson {
8589 return vm.parseJsonBytesArray (json, key);
8690 }
8791
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+
88192 function serialize (string memory jsonKey , string memory rootObject ) internal returns (string memory ) {
89193 return vm.serializeJson (jsonKey, rootObject);
90194 }
0 commit comments