You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thank for this project, it's awesome. Except I have a corner case to figure out.
Similar to #91, I need a way to preserve information in a string, instead of throwing an error (Invalid character). In my case it's reference to a DOM node.
Example:
{
target: document.body
}
Wanted result of parsing:
{
target: 'document.body'
}
I know it's probably unusual but it makes perfect sense for my use case. I feel like finding those references beforehand to stringify would be similar to writing my own parsing function.
Do you think there might be a simple modification that would allow for this?
I would be ok with doing it in my own fork, it's just I'm not sure how hard would be to implement.
So I'm thinking of adding a default return to the switch, that would label anything unexpected as a string, instead of triggering invalidChar. Would that work?
Both calls results in exact the same object as a parameter. No possibility to find "the variable name".
And serializing target: document.body to target: 'document.body' is the most surprising thing I would expect. A string with the document.body.innerHTML is a bit less surprise, but would not solve many use cases, too.
@HolgerJeromin I don't understand, the issue is with parsing a string, not stringifying an object.
It's the same as the RegExp issue, you get the same thing with stringify: json5.stringify(/.*_tests\.js/). Still it's a requested feature to be able to somehow retain the information when parsing it.
I know it's a hard sell, it's a corner case. My argument is simply that the crash is not useful, helps no one.
Like I said, I can try implementing it on my own in a fork, was just hoping for a little guidence. Ideally an estimate of how hard would it be to do, in man-hours. Is it a one line change, or a large rework?
Perhaps I mixed up the suggested direction of conversion. (I am still unsure what you mean)
How do you want to differ if the generated property target should get the string 'document.body' and when to replace it with a reference to a Element in the DOM?
Edit: Oh, perhaps I understand. If you skip the ' around the object, this should be replaced by the reference from the current scope?
@HolgerJeromin Right, the reference from the current scope would be the most excepted outcome in my opinion, from conceptual perspective.
But still, that would not solve my current use case. :) I'm making an editor of settings that are used in other pages. So in my case the elements would not be present, the reference would not resolve. I'm looking for a fallback that would retain these "unparsable" things as strings.
Thanks for the explanation. I was going to explain the complexities of how to modify this library to do what you want, but I ended up writing a prototype in the process because it sounded like a fun challenge.
@jordanbtucker Sorry to bother again but there's a key expression it's failing at: document.getElementById('target')
Would it be possible to include it as well? Selectors are really crucial.
Maybe to treat all characters as part of the JS expression, until the next dash?
Activity
jordanbtucker commentedon Jul 19, 2021
I'm not sure what you're asking. Can you explain your use case.
EskelCz commentedon Jul 20, 2021
@jordanbtucker Simply put I have a js settings file (loaded from CDN) with an object, in which can be references to DOM elements on the page. I need to parse it from a string and that crashes on these DOM references. I’d like to keep them as strings, instead of the crash of parsing.
The crash occurs in the
valuefunction: https://github.com/json5/json5/blob/master/lib/parse.js#L216So I'm thinking of adding a default return to the switch, that would label anything unexpected as a string, instead of triggering invalidChar. Would that work?
HolgerJeromin commentedon Jul 21, 2021
The suggestion does not work conceptionally.
Both calls results in exact the same object as a parameter. No possibility to find "the variable name".
And serializing
target: document.bodytotarget: 'document.body'is the most surprising thing I would expect. A string with thedocument.body.innerHTMLis a bit less surprise, but would not solve many use cases, too.EskelCz commentedon Jul 21, 2021
@HolgerJeromin I don't understand, the issue is with parsing a string, not stringifying an object.
It's the same as the RegExp issue, you get the same thing with stringify:
json5.stringify(/.*_tests\.js/). Still it's a requested feature to be able to somehow retain the information when parsing it.I know it's a hard sell, it's a corner case. My argument is simply that the crash is not useful, helps no one.
Like I said, I can try implementing it on my own in a fork, was just hoping for a little guidence. Ideally an estimate of how hard would it be to do, in man-hours. Is it a one line change, or a large rework?
HolgerJeromin commentedon Jul 21, 2021
Perhaps I mixed up the suggested direction of conversion. (I am still unsure what you mean)
How do you want to differ if the generated property
targetshould get the string'document.body'and when to replace it with a reference to a Element in the DOM?Edit: Oh, perhaps I understand. If you skip the
'around the object, this should be replaced by the reference from the current scope?EskelCz commentedon Jul 21, 2021
@HolgerJeromin Right, the reference from the current scope would be the most excepted outcome in my opinion, from conceptual perspective.
But still, that would not solve my current use case. :) I'm making an editor of settings that are used in other pages. So in my case the elements would not be present, the reference would not resolve. I'm looking for a fallback that would retain these "unparsable" things as strings.
jordanbtucker commentedon Jul 21, 2021
Thanks for the explanation. I was going to explain the complexities of how to modify this library to do what you want, but I ended up writing a prototype in the process because it sounded like a fun challenge.
https://gist.github.com/jordanbtucker/441fee538bd222cd6e9be0c061a3792e
This modification should parse simple identifiers and dot-chained identifiers as strings. For example:
It will not work on more complex, but valid, expressions like:
If you supply a built-in JSON5 value, then it will not be parsed as a string:
EskelCz commentedon Jul 22, 2021
@jordanbtucker You're awesome, thank you very much!
EskelCz commentedon Jul 23, 2021
@jordanbtucker Sorry to bother again but there's a key expression it's failing at:

document.getElementById('target')Would it be possible to include it as well? Selectors are really crucial.
Maybe to treat all characters as part of the JS expression, until the next dash?
Btw I found your project JSONext, which is even more fitting to my needs (I'm using template literals), so I merged your changes there. Hopefully without any major blunder :) https://github.com/EskelCz/jsonext/blob/master/src/parse.js
EskelCz commentedon Aug 5, 2021
Figured out a workaround, closing again. (Wrapped the js expression in brackets and then stringified them).