-
Notifications
You must be signed in to change notification settings - Fork 242
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
Options for preserving comments when editing a JSON5 file by script #177
Comments
A JSON5 reader is a good idea. For version 3 of this library, I'm working on moving the stateful lexer to its own class. This will help facilitate an XML reader style class for JSON5. I don't have an ETA on when version 3 will be out. Version 2 is mostly internal changes and drop for support for Node.js 4. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@jordanbtucker I was wondering, is there any update on v3 of this library? I'm still available to work on the kind of parser I mentioned. |
I'm thinking maybe there could be a
So that preserving comments when updating an object could be implemented this way:
Does that sound reasonable? Being able to get an AST from existing objects/strings may enable other features as well, and it's a pretty generic solution. Maybe merging the two ASTs wouldn't be that easy though 🤔 What do you guys think? |
Hey there, seems like we hit exactly the same need to programmatically update/merge JSON5 (and similar) files. I took a look to TS and Angular custom parsers implementation. The first one is only meant to be used to read stuff, the second one is really too much complex. |
Thanks for the suggestions. While I like your ideas, I don't plan on adding any more features to the JSON5 parser at this time. In an effort to keep JSON5 simple, this library will be a reference parser rather than a full-featured, production-ready implementation of a JSON5 parser. |
That's a shame, but understandable. I wouldn't mind helping out with a JSON5 AST parser as a separate project, if anybody else here is interested, but I've personally never worked on such a project before. I assume a good way to write one would be to use a library that allows a formal grammar to be specified for parsing and stringifying. Maybe someone else who was interested in this has a better idea of what the options are for such a project. |
Golden Fleece provides the patch function for editing JSON5 while preserving comments. Would it be appreciated to transfer that implementation to JSON5? |
@giacomorebonato Thanks. I've added golden-fleece to the Wiki. I plan on releasing JSON5 v3 after April, when Node.js v12 is no longer supported, however it will mostly include performance improvements and refactors. |
I really like the improvements that JSON5 have over regular JSON. Out of personal interest I've been looking into whether I can use this for configuration files that are touched both manually and automatically. An important requirement is that it's possible for everything, e.g. comments and lines split by escaping the newline, to be preserved on save losslessly.
As far as I can tell, this cannot be done with this library. When I run JSON5.parse(), it returns a plain object. What I want would probably require a parser more similar to how XML parsers work, iterating with more complex objects that contain metadata. That way I could iterate past everything I need, add a new node somewhere without disturbing the rest of the data, and write back the file without any of the formatting or manual choices being altered.
Are there any options for this at the moment? Or is anyone working on such a parser? Or better yet, maybe I'm mistaken and there is a way to do this with this library.
The text was updated successfully, but these errors were encountered: