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

Add ability to produce minified JSON (and perhaps YAML) output by typst query #4157

Closed
Andrew15-5 opened this issue May 16, 2024 · 5 comments · Fixed by #4161
Closed

Add ability to produce minified JSON (and perhaps YAML) output by typst query #4157

Andrew15-5 opened this issue May 16, 2024 · 5 comments · Fixed by #4161
Labels
cli About Typst's command line interface. feature request New feature or request introspection Related to introspection.

Comments

@Andrew15-5
Copy link
Contributor

Description

I want to be able to write typst query --field value --minified file.typ metadata and get:

[false,true]

instead of:

[
  false,
  true
]

which is very verbose IMO (7 bytes more). Maybe even print minified version by default (this command is probably only ever used in scripts anyway) and instead add --prittify for the verbose version.

Same can be done for the YAML version: from

- false
- true

to

[false,true]

(https://yaml.org/spec/1.0/, paragraph 2.1, example 2.5)

I personally haven't heard about YAML minification, but in some cases it is indeed possible (2 bytes saved), so if possible, why not affect both formats?

Use Case

The minified version outputs a smaller result, which if saved as a file, can reduce the size of that file (or reduce the size of piped data if used in a POSIX-compliant shell). If typst query outputs a lot of data, then the size difference becomes more noticeable. And just in general, it's better to not waste any resources if it is possible. Also, since the command is mainly used in scripts, the output doesn't have to be readable.

@Andrew15-5 Andrew15-5 added the feature request New feature or request label May 16, 2024
@istudyatuni
Copy link
Contributor

Yaml is a superset of JSON, so minified yaml could probably just be minified json

@Andrew15-5
Copy link
Contributor Author

This is true only for YAML 1.2: https://yaml.org/spec/1.2.0/. So if the target YAML parser supports v1.2, then you might as well just choose the JSON output instead of YAML. But this means that for v1.0 and v1.1 this is not true and that YAML minification can have its place in some niche cases (I don't know how widespread each YAML version is).

@istudyatuni
Copy link
Contributor

It turns out, serde_yaml doesn't have a to_string_pretty method, only to_string

@Enter-tainer
Copy link
Contributor

Enter-tainer commented May 17, 2024

I don't see why this is useful in any real case other than "I think it's very verbose". For passing json around using pipes, json parsers will skip all white space characters as if they are not exist. It there an example that makes a json file faster to process after minifying it? IIRC people will typically do this when serving them. But i think they already have a good packer to do such thing,

@Andrew15-5
Copy link
Contributor Author

I already mentioned an overlooked problem — waste of resources (size of the output).

Now that I think about it, performance can be affected too because you can't prettify an output without wasting more time. For tiny output like that this will be negligible, but when it gets big enough I'm sure the difference is measurable.

Documents with hundreds of pages do exist and if you query something big in size and/or frequent, then this is how you get big JSON data.

@Enivex Enivex added cli About Typst's command line interface. introspection Related to introspection. labels May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli About Typst's command line interface. feature request New feature or request introspection Related to introspection.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants