blakeembrey / javascript-stringify

Stringify is to `eval` as `JSON.stringify` is to `JSON.parse`
MIT License
139 stars 16 forks source link

javascript-parse (create object from string generated with javascript-stringify) #43

Closed drschwabe closed 1 year ago

drschwabe commented 2 years ago

Just wanted to A) see if there is an existing way to do this without being too hacky (see hacky solution below) and if not, B) suggest the feature or a supplementary module to do parsing of javascript-stringified output so that we can create an object from a string generated with javascript-stringify.

Regarding potential existing way, I tried this lib but does not seem to be able to parse an object output with javascript-stringify

https://github.com/greenpioneersolutions/auto-parse

(note for my test I wrote the javascript-stringify output to a file and then performed a fs.readFile(objString, 'utf8') on it and then attempted to read the resulting string with auto-parse)

The way that I found which actually works is a bit hacky and requires Node:

you can write the javascript-stringify output to a string, append module.exports=, write to disk, and perform a require on the resulting file.

related: https://github.com/blakeembrey/javascript-stringify/issues/32

mezhanglei commented 1 year ago

me too,why author can't do it? i think it need a parse way will be beautifull

mezhanglei commented 1 year ago

We are silly, the string can be executed directly with eval

drschwabe commented 1 year ago

We are silly, the string can be executed directly with eval

oh cool, thx that seems less hacky than my original tek - will try that !