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

std.parseJson #479

Closed
jwise opened this issue Mar 28, 2018 · 9 comments
Closed

std.parseJson #479

jwise opened this issue Mar 28, 2018 · 9 comments

Comments

@jwise
Copy link

jwise commented Mar 28, 2018

I have recently found myself with cause to parse some ill-formed partial JSON -- a library that we are incrementally converting to Jsonnet was originally parsed by the C preprocessor, and had files like this:

main.jh:

{
  "unit_1": {
#include "unit_1.jh"
  },
  "unit_2": {
#include "unit_2.jh
  },
  ...
}

unit_1.jh:

  "out": {
    ... data ...
  }

I'd like to be able to load a unit_1.jh directly into Jsonnet without invoking an external pass. Obviously, I can't hand it to import, since it's ill-formed. importstr can load it so that I can do extra work on it, but once I have a string, I can't convert it to an object. Really, I'd like to be able to do something like:

eval ("{" + importstr "unit_1.jh" + "}")

Or, even better,

local importjh(f) = eval("{" + importstr f + "}")

though that would require changes along the lines of #196, too.

This is closely related to #460, but maybe more general. One key difference is that #460 wants to be "safe"; in this case, "everybody knows" that any time you type "eval", you deserve whatever happens next 😄 We intend to use this only on trusted JSON databases.

@sbarzowski
Copy link
Collaborator

If unit_1.jh is json without outer braces, parseJson (mentioned in #196) would be enough. Or is there some reason to treat it as jsonnet?

@sparkprime
Copy link
Contributor

Yeah I had that thought too.

@jwise
Copy link
Author

jwise commented Apr 2, 2018

parseJson would be okay too, but I imagine that eval is probably lower effort to implement -- and works equally well for my application (trusted input).

@sparkprime
Copy link
Contributor

eval is easy to implement but 1) code using it is hard to read 2) any language forcing you to use it for trivial things probably isn't very good. parseJson seems to come up fairly frequently though.

@sparkprime sparkprime changed the title RFE: eval keyword std.parseJson Apr 14, 2018
@glerchundi
Copy link

Any update on this? Would love to see this implemented!

@sbarzowski
Copy link
Collaborator

sbarzowski commented Sep 18, 2018

Funny you ask, yesterday I've just started looking into this again. In go-jsonnet adding it is trivial, but in cpp-jsonnet it requires some effort. I also have a partially implemented pure-jsonnet JSON parser, but it's going to be really slow compared to anything native.

@glerchundi
Copy link

Good to hear, thanks for the update!

@sparkprime
Copy link
Contributor

I think vendoring a small json parser into the C++ implementation would be reasonable.

@sbarzowski
Copy link
Collaborator

I think we can close it now (06c1ce1).

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

No branches or pull requests

4 participants