book icon indicating copy to clipboard operation
book copied to clipboard

Create json IO guide

Open odyslam opened this issue 2 years ago • 3 comments
trafficstars

People seem to have issues (e.g this).

The page should be a guide on how to do input and output to json, unifying in essence, the parseJson and writeJson reference pages.

It should also mention stdJson helpers and the functions for forge script artifact parsing #618

odyslam avatar Feb 13 '23 12:02 odyslam

It there a way with the current system to write json arrays? (not only objects)

On thing I currently find quite confusing is the design choice behind this behavior:

vm.writeJson('{ "foo": "bar" }', path);
vm.writeJson('foo', path, '.bar');

Which would result in:

{
  "foo": "bar"
}

While i guess one(at least me :sweat_smile:) would expect:

{
  "foo": "bar",
  "bar": "foo"
}

Was this due to a conscious choice/limitation of how this works under the hood?

Would imagine this to be a lot more useful & usable if i could do sth like vm.writeJson('foo', path, '.bar.foo.1'); to write arbitrary nested objects without having to previously ensure the keys exist.

sakulstra avatar Feb 15 '23 17:02 sakulstra

Unfortunately, that's tedious as there aren't any libraries to lean on. The way to do it currently would be to serialize the json and then write it.

Have you read the reference pages for the cheatcodes in the docs? @sakulstra

odyslam avatar Feb 15 '23 23:02 odyslam

@odyslam yes i checked the pages and used the existing utilities quite a bit.

That said, we wrote a helper library which will read the whole configuration of an aave pool and writes it to a file. As this is a rather complex task we eventually opted to writing md/csv as it seems less cumbersome so "just add a line" to an existing file than working with jsons. In practice a json would be far more readable and consumer friendly i suppose (that's why i commented here).


In js i guess one would use sth like https://github.com/lodash/lodash/blob/master/.internal/baseSet.js Sadly i'm not experienced in rust at all. There seems to be an incomplete lodash port, but not containing _set

sakulstra avatar Feb 16 '23 09:02 sakulstra