jprichardson / node-jsonfile

Easily read/write JSON files.
MIT License
1.2k stars 321 forks source link

Extra characters in written file #44

Closed gwc4github closed 8 years ago

gwc4github commented 8 years ago

Hello JP. Thanks for providing you jsonfile package. I have been trying to use your jsonfile package and I am having an interesting problem. This code: jsonfile.writeFileSync('/tmp/gregg.dat', '{ "type":"configuration/entityTypes/HCP", \n"attributes": {\n'); Writes this: "{ \"type\":\"configuration/entityTypes/HCP\", \n\"attributes\": {\n” instead of: { "type":"configuration/entityTypes/HCP”, “attributes”: {

Am I doing something wrong?

jprichardson commented 8 years ago

Remove the '... i.e. you're writing a string instead of an object. Please reopen if you're still unsure of what's going on.

gwc4github commented 8 years ago

Thanks JP. That does allow me to create some files but only complete objects. However, the JSON I am generating appears to be too big and kills writeFileSync(). Or maybe there is another issue I am not yet aware of. I can format the JSON in a text editor so I think it is ok. Here is the error: _stream_writable.js:260 var len = state.objectMode ? 1 : chunk.length; ^ TypeError: Cannot read property 'length' of undefined

Would you consider adding a flag so that we can use it to write text? That way I can build the JSON bit-by-bit. I suppose others might want to use if for text files too?

Thanks again JP

jprichardson commented 8 years ago

That does allow me to create some files but only complete objects.

What do you mean by "complete objects"? Do your objects contain a large amount of data? Are you trying to write out multiple objects, one per line?

gwc4github commented 8 years ago

I am building the objects from metadata and data that I am getting from other sources. So, this string is the start of a large object that I want to generate but is not yet complete: "'{ "type":"configuration/entityTypes/HCP", \n"attributes"" So, I could have tried to write the code to keep adding to objects and arrays instead of building a string but that was harder to do in some cases. Maybe I should have done it that way anyway but this seems easier to debug? the code currently creates a large object in memory as a string. I tried writing that out with your function as shown and that didn't work. I also tried the standard fs.createWriteStream() and right now that fails too. It's about 49k in size.

jprichardson commented 8 years ago

If you're writing large amounts of data, fs.createWriteStream() is what you want. If that's failing, there's some other issue going on.