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

FR: Add a json function in template language for serializing strings #5648

Open
HKalbasi opened this issue Feb 10, 2025 · 2 comments
Open

FR: Add a json function in template language for serializing strings #5648

HKalbasi opened this issue Feb 10, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@HKalbasi
Copy link

HKalbasi commented Feb 10, 2025

Is your feature request related to a problem? Please describe.
It is currently impossible for a tool to reliably use output of commands jj log and jj op log. By using template we can impose a structure on the output, but some fields, like commit messages or commands that contain template can have arbitrary strings in them, and there is no way to split them and detect when an entry ends.

Describe the solution you'd like
Add a function json which receives a string and serialize it as a json string, replace its problematic characters like end line with escapes, and surround it with ". If/when template language gets dynamic objects in future, this function can be extended to convert them to json too.

Describe alternatives you've considered
A dedicated jj api command like the one proposed in #3219 can solve this problem, but it needs massive design and maintenance. This function is a small addition that can coexist with future solutions.

@PhilipMetzger PhilipMetzger added the enhancement New feature or request label Feb 10, 2025
@PhilipMetzger
Copy link
Contributor

This was initially brought here and then @davidbarsky asked about it in this Discord discussion.

Here is what was talked about then:

@davidbarsky — 8/21/2024 6:20 PM
(unrelated: is there any consensus around a json() template formatter? the issues were somewhat unclear on that front, but i imagine it's an easy-on ramp for tooling; whereas the API would be for something more long-lived, like sapling's ISL)
@PhilipMetzger — 8/21/2024 6:58 PM
you can find some discussion on it, in the template language bikeshed issue
as long as we're not running into hyrum's law and provide no guarantees on the output format, I don't see a large issue with it

There's also #3262 (comment) which contains a relevant point from Yuya.

@yuja
Copy link
Contributor

yuja commented Feb 11, 2025

I think it's good idea to add some escape method to String type, something like description.escape(), .escape_json(), or .serialize_json()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants