Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document that key must start with a dot in JSON cheats #394

Open
PaulRBerg opened this issue May 29, 2023 · 0 comments
Open

Document that key must start with a dot in JSON cheats #394

PaulRBerg opened this issue May 29, 2023 · 0 comments

Comments

@PaulRBerg
Copy link
Contributor

PaulRBerg commented May 29, 2023

The current documentation for the JSON cheats does not explain that the key parameter must be provided with a leading dot:

forge-std/src/Vm.sol

Lines 213 to 239 in e8a047e

//
// parseJson
//
// ----
// In case the returned value is a JSON object, it's encoded as a ABI-encoded tuple. As JSON objects
// don't have the notion of ordered, but tuples do, they JSON object is encoded with it's fields ordered in
// ALPHABETICAL order. That means that in order to successfully decode the tuple, we need to define a tuple that
// encodes the fields in the same order, which is alphabetical. In the case of Solidity structs, they are encoded
// as tuples, with the attributes in the order in which they are defined.
// For example: json = { 'a': 1, 'b': 0xa4tb......3xs}
// a: uint256
// b: address
// To decode that json, we need to define a struct or a tuple as follows:
// struct json = { uint256 a; address b; }
// If we defined a json struct with the opposite order, meaning placing the address b first, it would try to
// decode the tuple in that order, and thus fail.
// ----
// Given a string of JSON, return it as ABI-encoded
function parseJson(string calldata json, string calldata key) external pure returns (bytes memory abiEncodedData);
function parseJson(string calldata json) external pure returns (bytes memory abiEncodedData);
// The following parseJson cheatcodes will do type coercion, for the type that they indicate.
// For example, parseJsonUint will coerce all values to a uint256. That includes stringified numbers '12'
// and hex numbers '0xEF'.
// Type coercion works ONLY for discrete values or arrays. That means that the key must return a value or array, not
// a JSON object.
function parseJsonUint(string calldata, string calldata) external returns (uint256);

I was trying to access the key by its simple name, like so:

string memory json = '{"bar":"Hello World"}';
string memory bar = vm.parseJsonString(json, "bar");

And that didn't work until I added the leading dot, i.e. .bar.

Side note: the parameters for the parseJsonSOMETHING cheats have been left unnamed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant