Skip to content

Parse functions as object properties #132

Closed

Description

@YerkoPalma

The following string throws an error when parsed

`{
  local: { active: true },
  effects: {
    toggle: function (data, state, send, done)   {
      this.local = !this.local
    }
  }
}`

it throws

Uncaught SyntaxError: Expected 'a' instead of 'u' at line 4 column 14 of the JSON5 data. Still to read: "unction (data, state"
      at JSON5.parse.error (node_modules/json5/lib/json5.js:56:25)
      at JSON5.parse.next (node_modules/json5/lib/json5.js:72:17)
      at JSON5.parse.word (node_modules/json5/lib/json5.js:366:17)
      at JSON5.parse.value (node_modules/json5/lib/json5.js:493:56)
      at JSON5.parse.object (node_modules/json5/lib/json5.js:459:35)
      at JSON5.parse.value (node_modules/json5/lib/json5.js:482:20)
      at JSON5.parse.object (node_modules/json5/lib/json5.js:459:35)
      at JSON5.parse.value (node_modules/json5/lib/json5.js:482:20)
      at Object.parse (node_modules/json5/lib/json5.js:508:18)

The problem is that this switch-case statement doesn't expect a function. In fact in the comments says

// Parse a JSON value. It could be an object, an array, a string, a number,
// or a word.

Now, as far as I know that is a valid json value, I tested in chrome console, so, the parser should also accept functions as values for json properties.

Activity

  1. jordanbtucker commented on Oct 7, 2016

    @jordanbtucker
    Member

    I think you're the first person to ask for support for functions. What is your use case for this?

    Also, the result of the string you're trying to parse is not a valid JSON value. Just run the following code:

    JSON.stringify({
      local: { active: true },
      effects: {
        toggle: function (data, state, send, done)   {
          this.local = !this.local
        }
      }
    }, null, '  ')

    And you'll get:

    {
      "local": {
        "active": true
      },
      "effects": {}
    }

    Note that the function is missing from the JSON result.

  2. YerkoPalma commented on Oct 7, 2016

    @YerkoPalma
    Author

    Mmm it seems that's not a valid json but it is a valid js object.
    I'm working in a browserify transform, so I have to parse some js code as a string. I don't know if your library is supposed to handle js objects or jsons, in fact I started using it because of native JSON.parse and JSON.stringify limitations. I guess I'll look for a regex for now.
    If you think this use case is out of scope, just close this issue, if don't I'll wait for a solution.

    Thanks for your time anyway :)

  3. jordanbtucker commented on Oct 7, 2016

    @jordanbtucker
    Member

    If you're looking to parse JavaScript, you should look into Esprima, Acorn, and UglifyJS.

    Parsing functions is out of the scope of JSON5. Thanks.

  4. crapthings commented on Oct 21, 2019

    @crapthings

    i have same issue as @YerkoPalma

    my string is like this

    $$(issues) filter({ createdAt: { $gte: moment(new Date('2018')).endOf('year').toDate() } }) pickBy(createdAt) as(issues)
    output(issues)
    

    after parse each line will turn into an array item, but the nested kv pair include function that is not string

    { createdAt: { $gte: moment(new Date('2018')).endOf('year').toDate() } }

    but i think you're right this is not a valid json, but any idea i can add JSON5 to solve this temporary ? then i might try using a lex analyzer

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions