Skip to content
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

Closed
wants to merge 6 commits into from
Closed

Conversation

elcritch
Copy link

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.

@elcritch
Copy link
Author

@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:
Copy link
Contributor

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

Copy link

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.

@IainNZ
Copy link
Contributor

IainNZ commented Jul 11, 2015

So, what is the reason you'd want to have a JSON literal in Julia code?

@elcritch
Copy link
Author

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

@elcritch
Copy link
Author

json"" and json""" ... """ would be fine with me. I'll update the code. @kmsquire thanks for the tip re: the postfix modifier. I thought there might be something like that but couldn't figure it out. There is an option in the parser for using '. Currently it's either/or as in the parser will only look for double or single quotes.

@ScottPJones
Copy link
Contributor

I don't see the need for single quotes, because with triple quotes """ you get what you want, double quotes " without having to do \"

@elcritch
Copy link
Author

That's a good point. I was trying to go for succinctness originally using J"{'a':1}" notation but the json"..." is cleaner. Leaving the single quote option but not documenting it would allow flexibility for those who might need it.

@tshort
Copy link

tshort commented Nov 27, 2015

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

@hayd
Copy link

hayd commented Nov 27, 2015

RE json"{'a':1}". That's not valid json and should raise, json strings must be "-quoted.

It may be useful to have a function that parses python printed dicts but they're not json.

@tshort
Copy link

tshort commented Nov 27, 2015

I agree, @hayd. The triple-quoted version json""" ... """ seems most useful to me, anyway.

@TotalVerb
Copy link
Collaborator

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.

@elcritch
Copy link
Author

Triple quoted macro is useful for test cases and quick conversion of broken code which uses the deprecated literal dictionary syntax foo = { "bar": 1 }. For most any other code, the difference between JSON.parse(""" ... """) and json""" ... """ would be trivial as several mention, but in this case it'd help make up for the loss of built in literal dictionary syntax. Converting existing code is much easier if it's surrounded by just """ and not also wrapped in a function call and the trailing ). An example situation one would run into this would be the problem StaffJoy folks had where you suddenly have to convert thousands of lines of code to a different syntax, of course string interpolation would be most helpful in that case.

@TotalVerb
Copy link
Collaborator

TotalVerb commented Jun 19, 2016

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

@TotalVerb
Copy link
Collaborator

TotalVerb commented Apr 25, 2017

This doesn't actually address the loss of literal dict syntax, because you can't do something like

bar = 1
json"""
{
    "foo": bar
}"""

and as such json"x" is just a different way of spelling JSON.parse("x") right now. I think there are better places/packages for dealing with a nicer literal dict syntax.

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.

@TotalVerb TotalVerb closed this Apr 25, 2017
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.

None yet

7 participants