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

writeJson for Array of Objects #244

Open
Tracked by #3801
vapopov opened this issue Nov 28, 2022 · 5 comments
Open
Tracked by #3801

writeJson for Array of Objects #244

vapopov opened this issue Nov 28, 2022 · 5 comments

Comments

@vapopov
Copy link

vapopov commented Nov 28, 2022

Test script:

pragma solidity 0.8.17;

import "forge-std/StdJson.sol";
import "forge-std/Script.sol";

contract JSONScript is Script {
    using stdJson for string;

    function run() public {

        string memory obj1 = "";
        vm.serializeUint(obj1, "number", uint256(342));
        string memory obj2 = "";
        vm.serializeUint(obj2, "number", uint256(342));

        string[] memory st2 = new string[](2);
        st2[0] = vm.serializeUint(obj1, "number2", uint256(55555));
        st2[1] = vm.serializeUint(obj2, "number2", uint256(55555));

        string memory json1 = "some key";
        vm.serializeBool(json1, "boolean", true);
        vm.serializeUint(json1, "number", uint256(342));
        vm.serializeUint(json1, "number2", uint256(55555));
        vm.serializeString(json1, "st2", st2);

        string memory json2 = "some other key";
        string memory output = vm.serializeString(json2, "title", "finally json serialization");
        string memory finalJson = vm.serializeString(json1, "object", output);

        console.logString(finalJson);

        string memory rt = vm.projectRoot();
        finalJson.write(string(abi.encodePacked(rt,"/export/exp.json")));
    }
}

which gives me current results:

{"object":{"title":"finally json serialization"},"number2":55555,"boolean":true,"number":342,"st2":"[\"{\"number\":342,\"number2\":55555}\",\"{\"number\":342,\"number2\":55555}\"]"}

so all objects which is added to array is serialized as escaped strings

@mds1
Copy link
Collaborator

mds1 commented Nov 28, 2022

cc @odyslam

@odyslam
Copy link
Contributor

odyslam commented Nov 28, 2022

Ooof, I thought I ironed out all the kinks. Will look into it, thanks!

@mds1
Copy link
Collaborator

mds1 commented Nov 28, 2022

Also should we transfer this issue to the main foundry repo? cc @mattsse

@vapopov
Copy link
Author

vapopov commented Dec 1, 2022

@odyslam another one issue:
if i try to convert number to string

vm.serializeString(result, "totalUsdgAmount", Strings.toString(TotalUsdgAmount));

as a result in json I still get numeric value

"usdgAmount":3.3428815122454733e24

@odyslam
Copy link
Contributor

odyslam commented Dec 1, 2022

Irrelevant note: You can use vm.toString()

Interesting. Will create a master issue to keep track of it in forge

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

3 participants