CertainLach / jrsonnet

Rust implementation of Jsonnet language
MIT License
304 stars 33 forks source link

Add example for Serde deserialisation #142

Open Timmmm opened 9 months ago

Timmmm commented 9 months ago

I think most people using the Rust library version of this would want to just take serde-json code and switch it to jsonnet. Unfortunately I can't figure out how to do that. An example would be great! (I assume it is possible.)

bruno-delfino1995 commented 9 months ago

@Timmmm, in the meantime, you can look at this snippet from a project using this library. I hope it helps!

Timmmm commented 9 months ago

I was more thinking something like the example in json5's readme. Is something like that possible?

CertainLach commented 9 months ago

It is possible, but jsonnet is much more powerful than json5, and should not be/not intended to be used like serde_json.

Configuration may include other files (and assuming current directory to be CWD is bad and insecure), then there is more types available than serde-json supports, I.e functions. Also, most of the time you want to supply jsonnet with tla/ext vars and so on.

Timmmm commented 9 months ago

My understanding was that the result of executing a Jsonnet file would be an ordinary JSON data object. So there must be a way to execute it and then load that with serde? For my current use case ("better JSON") I don't want to supply tla/ext vars or any custom functions or whatever.

and assuming current directory to be CWD is bad and insecure

Agreed, I'd say any API should require explicitly setting the current directory and error on loading external files if you didn't. Otherwise it's not fail-safe (c.f. yaml.safe_load()!)