foundry-rs / foundry-rs/foundry
Add pure JSON serialization cheatcodes
- Dominant language
- Rust
- Stars
- 10.6k
- Forks
- 2.6k
- Avg merge
- 18h 20m
- Merged PRs (30d)
- 510
Description
### Component
Forge
### Describe the feature you would like
Hi everyone!
The current approach to handling JSON feels somewhat suboptimal. Working with the id -> json map feels like an extra step, which, in my opinion, isn't entirely necessary. If the JSON serialization methods were made "pure", they could operate directly on a JSON string, which has the advantages of not modifying state and being more intuitive.
Consider the current method:
```solidity
string memory id = "myObj";
vm.serializeString(id, ".foo", "bar");
vm.serializeString(id, ".hello", "world");
string memory json = vm.serializeUint(id, ".someUint", 123);
```
With a direct JSON string approach, it could be simplified to:
```solidity
string memory json = '{ "foo": "bar" }';
json = vm.serializeString(json, ".hello", "world");
json = vm.serializeUint(json, ".someUint", 123);
```
This method offers more clarity in the way we work with JSON. Also, by the convention used by `forge-std`, in this case the functions could be declared as `pure`. Of course this would break a lot of things... an alternative would be to add these as new methods (not ideal as the vm interface is already pretty big).
Thoughts?
### Additional context
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.