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

Parsing JSON in Skylark #1813

Closed
rockwotj opened this issue Sep 22, 2016 · 13 comments
Closed

Parsing JSON in Skylark #1813

rockwotj opened this issue Sep 22, 2016 · 13 comments

Comments

@rockwotj
Copy link
Contributor

rockwotj commented Sep 22, 2016

I'm trying to write a Skylark Extension and need to work with JSON data. Is there a way to load a JSON string into a struct or dict without implementing a json parser in my .bzl file?

@rockwotj rockwotj changed the title Parsing JSON into a struct in Skylark Parsing JSON in Skylark Sep 22, 2016
@pcj
Copy link
Member

pcj commented Sep 22, 2016

I don't think so. I would consider farming that task out to a helper python script. Implementing a JSON parser would be pretty difficult without recursion.

@rockwotj
Copy link
Contributor Author

Yeah but then I need to use certain values from that JSON to pass onto other build steps, as it gets very messy. So farming out to helpers would be non ideal as a bunch of work would get duplicated in each helper function. Would there be a chance this could get added to the built in functions for Skylark? I feel this could be a pretty common task.

@laszlocsomor
Copy link
Contributor

@rockwotj : What's in the JSON data that you need to process in Skylark? How would the Skylark code use this data?

In principle the only way to process this data is creating Skylark structures from it using a tool pre-build that converts the data, as @pcj suggested, then load'ing that .bzl file.

I don't think we have JSON parsing on the roadmap, but I'm looping in @laurentlb to confirm.

@rockwotj
Copy link
Contributor Author

Similar to maven_jar for java I want to add support for Cocoapods on iOS. So I'm working on a bazel extension to convert podspec.json files into BUILD files.

@kchodorow
Copy link
Contributor

cc @aj-michael, who's interested in Cocoapod support.

@aj-michael
Copy link
Contributor

Haha, I had been asking around on behalf of @mcdonamp who I'm sure is already aware of this effort. Hi Tyler!

@rockwotj
Copy link
Contributor Author

Haha great! Hey Michael, long time no talk. @mcdonamp has talked about it enough I figured it'd be a good side project.

@asciimike
Copy link

In case anyone else is curious, I think this can be accomplished manually in today's world by doing something like this: https://gist.github.com/mcdonamp/1f0799e38d1e96a993319da4645fa0b4

I think @rockwotj has made it a little cleaner (autogening the BUILD file) and added support for pulling particular versions of a pod, more like @cocoapod//sdwebimage:X.Y.Z

As Tyler said, we need the ability to parse the podspec.json file, map specific properties to their BUILD equivalents, and autogenerate that BUILD file.

@pcj
Copy link
Member

pcj commented Sep 22, 2016

@rockwotj fyi you may already be aware of this tool in rules_go named "gazelle" which autogenerates BUILD files for golang. You may or may not be interested in using go but then you can use buildifier as well.

@rockwotj
Copy link
Contributor Author

@pcj I was not. Thanks for the link! I'll probably look into that.

@damienmg
Copy link
Contributor

damienmg commented Oct 6, 2016

Seems like this was resolved, closing :)

@jin
Copy link
Member

jin commented Dec 28, 2018

For future readers: there doesn't yet exist a JSON parser in Starlark (or even Skylib), the discussion is continuing here: #3732

bazel-io pushed a commit that referenced this issue Oct 21, 2020
This change predeclares 'json', the new Starlark module
for JSON encoding/decoding/indenting, in all Bazel Starlark
environments (alongside depset, select, etc).

The new function works for any legal value, not just struct,
and avoids polluting the struct field namespace.

struct.to_json is deprecated, along with struct.to_proto,
and will be disabled by the new flag --incompatible_struct_has_no_methods.
(The replacement for to_proto will be added shortly.)

Updates bazelbuild/starlark#83
Updates #3732
Updates #1813

RELNOTES: The Starlark json module is now available.
Use json.encode(x) to encode a Starlark value as JSON.
struct.to_json(x) is deprecated and will be disabled by
the --incompatible_struct_has_no_methods flag.
PiperOrigin-RevId: 338259618
@jlaxson
Copy link
Contributor

jlaxson commented Apr 16, 2021

More explicitly: this now exists: https://docs.bazel.build/versions/master/skylark/lib/json.html

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

9 participants