Skip to content

Consider serializing DOM objects to strings #249

Closed

Description

@EskelCz

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.

Activity

  1. jordanbtucker commented on Jul 19, 2021

    @jordanbtucker
    Member

    I'm not sure what you're asking. Can you explain your use case.

  2. EskelCz commented on Jul 20, 2021

    @EskelCz
    Author

    @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 value function: https://github.com/json5/json5/blob/master/lib/parse.js#L216

    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?

  3. HolgerJeromin commented on Jul 21, 2021

    @HolgerJeromin

    The suggestion does not work conceptionally.

    json5.stringify(document.body);
    let bodyElem = document.body;
    json5.stringify(bodyElem);

    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.

  4. EskelCz commented on Jul 21, 2021

    @EskelCz
    Author

    @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?

  5. HolgerJeromin commented on Jul 21, 2021

    @HolgerJeromin

    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?

  6. EskelCz commented on Jul 21, 2021

    @EskelCz
    Author

    @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.

  7. jordanbtucker commented on Jul 21, 2021

    @jordanbtucker
    Member

    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:

    { target: document }                // { target: 'document' }
    { target: document.body }           // { target: 'document.body' }
    { target: document.body.innerHTML } // { target: 'document.body.innerHTML' }
    { target: anyValidIdentifier }      // { target: 'anyValidIdentifier' }

    It will not work on more complex, but valid, expressions like:

    { target: document['body'] }
    { target: document . body }

    If you supply a built-in JSON5 value, then it will not be parsed as a string:

    { target: null }
    { target: true }
    { target: false }
    { target: Infinity }
    { target: NaN }
  8. EskelCz commented on Jul 22, 2021

    @EskelCz
    Author

    @jordanbtucker You're awesome, thank you very much!

  9. EskelCz commented on Jul 23, 2021

    @EskelCz
    Author

    @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.
    Screenshot 2021-07-23 at 18 33 36
    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

  10. EskelCz commented on Aug 5, 2021

    @EskelCz
    Author

    Figured out a workaround, closing again. (Wrapped the js expression in brackets and then stringified them).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions