Skip to content

Conversation

asopenag
Copy link

@asopenag asopenag commented Dec 28, 2018

In current version, you can enable unquoted keys by using JsonOptions.AllowUnquotedKeys = True, but that doesn’t capture unquoted values (except, of course, for reserved values like numbers or null). But it may happen that you find unquoted values for strings with just one word (I think that’s called relaxed json).

Example: {firstName: John, lastName: Smith}

I have update the code to cover those cases.

How to enable:
Just set the setting JsonOptions.AllowUnquotedKeys to True, it will now also capture those 1-word unquoted String values.

Implementation notes:

  • I assume unquoted String values end once we find a space, a coma, a "}" or a "]" (longer/complex strings are expected to come quoted).
  • For this to work, I assume first that all values are strings (including true/false/null/numbers), then I check, and...
    • If IsNumeric() I convert the value back to number using VBA.Val().
    • If = "null" -> Null
    • If = "true" / "false" -> True / False

Next steps:
We should clarify the setting JsonOptions.AllowUnquotedKeys : (a) rename it to clarify it works for both key and values or (b) create a new setting to allow unquote key/values separately.

Thanks!

I've found lately JSONs with unquoted String values (it seems to be called [relaxed json](http://www.relaxedjson.org/)), so I've updated the code to support those:

In this version, if you set the setting `JsonOptions.AllowUnquotedKeys` to `True` it will also allow unquoted Strings. Unquoted Strings (in this version) end once we find a space, a coma, a "}" or a "]".

Notice that this will capture the true/false/null/numbers as strings (not having the quotes to identify the strings, all are a potential Strings), so we check them afterwards and:
- If `IsNumeric()` we convert the value back to number using `VBA.Val()`.
- If  = "null" -> `Null`
- If = "true" / "false" -> `True` / `False`

**Next steps:** We should clarify the setting `JsonOptions.AllowUnquotedKeys` : (a) rename it to clarify it works for both key and values or (b) create a new setting to allow unquote key/values separately.
Added support for unquoted String Values
If json_ParseValue gets converted to boolean, then it returns isNumeric() -> true, leading to errors (since then true/false get converted to 0 through VBA.Val().
Now IsNumeric() check runs first, skipping that use case.
@asopenag asopenag mentioned this pull request Jan 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant