-
Notifications
You must be signed in to change notification settings - Fork 103
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
Adding JSON macro strings #113
Conversation
@ScottPJones , do you know how to get this to run / test on the 0.3 release branch? The 0.4 branch has lot's of deprecation warnings and other issues so I didn't get it tested/working there quite yet. |
@@ -28,6 +28,26 @@ JSON.json(j) | |||
# "{\"an_array\":[\"string\",9],\"a_number\":5.0}" | |||
``` | |||
|
|||
### Macro Strings | |||
|
|||
`JSON` and `J` can be used to embed JSON data directly into source code: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't just json"""
be more Julian? Either way, 👎 to two ways to do same thing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation here needs to be updated to reflect the code changes.
So, what is the reason you'd want to have a JSON literal in Julia code? |
@IainNZ , basically to allow interoperability with common JavaScript / Python / Ruby code. It's really useful for embedding example configuration examples in Jupyter Notebooks or communicating with Jupyter notebooks. |
|
I don't see the need for single quotes, because with triple quotes |
That's a good point. I was trying to go for succinctness originally using |
+1 for this feature. See JuliaEditorSupport/atom-language-julia#52 for a PR for support in Atom. If we also add support in https://github.com/JuliaLang/Julia.tmbundle (looks easy), we'll get Sublime and GitHub markup covered. |
RE It may be useful to have a function that parses python printed dicts but they're not json. |
I agree, @hayd. The triple-quoted version |
As I mentioned on #130 (comment), it's kind of pointless that have a string macro that's just a synonym for JSON.parse. It would be nice to have interpolation with this syntax, which can't be done using JSON.parse. |
Triple quoted macro is useful for test cases and quick conversion of broken code which uses the deprecated literal dictionary syntax |
@elcritch I don't understand why json"""{
...
}""" is much easier to write than JSON.parse("""{
...
}""") The difference is only 8 additional characters. In any event, if someone wants this, they can just write macro json_str(ex) Meta.quot(JSON.parse(ex)) end I think, personally, that this is not worth adding until/unless string interpolation is supported. |
This doesn't actually address the loss of literal dict syntax, because you can't do something like
and as such For those who want this, defining macro json_str(ex) Meta.quot(JSON.parse(ex)) end in your package gives you the functionality offered here. |
Basic implementation of JSON macro strings in addition to allowing parsing of non-standard JSON using single quotes
'
around string values. Only single or double quote chars can be used in a single JSON string.This is based on the discussion towards the end of Julia Issue #6739.
I've added tests and updated the documentation as well.