Jsonnet is a data templating language that extends JSON with variables, conditionals, functions, and imports for configuration management
—
Jsonnet provides powerful code formatting capabilities through both a C library API and the jsonnetfmt command-line tool. The formatter offers extensive customization options for indentation, spacing, comment styles, string literal preferences, and import organization.
Functions for configuring the formatter's style and behavior options.
/**
* Set indentation level when reformatting (number of spaces).
* @param vm VM instance
* @param n Number of spaces, must be > 0
*/
void jsonnet_fmt_indent(struct JsonnetVm *vm, int n);
/**
* Set maximum number of consecutive blank lines allowed.
* @param vm VM instance
* @param n Number of blank lines, must be > 0
*/
void jsonnet_fmt_max_blank_lines(struct JsonnetVm *vm, int n);
/**
* Set preferred style for string literals.
* @param vm VM instance
* @param c String style: 'd' (double quotes), 's' (single quotes), 'l' (leave as-is)
*/
void jsonnet_fmt_string(struct JsonnetVm *vm, int c);
/**
* Set preferred style for line comments.
* @param vm VM instance
* @param c Comment style: 'h' (hash #), 's' (slash //), 'l' (leave as-is)
*/
void jsonnet_fmt_comment(struct JsonnetVm *vm, int c);
/**
* Set whether to add extra space on the inside of arrays.
* @param vm VM instance
* @param v 1 to enable array padding, 0 to disable
*/
void jsonnet_fmt_pad_arrays(struct JsonnetVm *vm, int v);
/**
* Set whether to add extra space on the inside of objects.
* @param vm VM instance
* @param v 1 to enable object padding, 0 to disable
*/
void jsonnet_fmt_pad_objects(struct JsonnetVm *vm, int v);
/**
* Use syntax sugar where possible with field names.
* @param vm VM instance
* @param v 1 to enable pretty field names, 0 to disable
*/
void jsonnet_fmt_pretty_field_names(struct JsonnetVm *vm, int v);
/**
* Sort top-level imports in alphabetical order.
* @param vm VM instance
* @param v 1 to enable import sorting, 0 to disable
*/
void jsonnet_fmt_sort_imports(struct JsonnetVm *vm, int v);
/**
* Reformat after desugaring for debugging purposes.
* @param vm VM instance
* @param v 1 to enable debug desugaring output, 0 to disable
*/
void jsonnet_fmt_debug_desugaring(struct JsonnetVm *vm, int v);Primary functions for formatting Jsonnet code from files or strings.
/**
* Reformat a file containing Jsonnet code.
* @param vm VM instance
* @param filename Path to file containing Jsonnet code
* @param error Output parameter set to 1 on error, 0 on success
* @returns Formatted Jsonnet code on success, error message on failure (use jsonnet_realloc to free)
*/
char *jsonnet_fmt_file(struct JsonnetVm *vm, const char *filename, int *error);
/**
* Reformat a string containing Jsonnet code.
* @param vm VM instance
* @param filename Path to file (used in error messages)
* @param snippet Jsonnet code to format
* @param error Output parameter set to 1 on error, 0 on success
* @returns Formatted Jsonnet code on success, error message on failure (use jsonnet_realloc to free)
*/
char *jsonnet_fmt_snippet(struct JsonnetVm *vm, const char *filename, const char *snippet, int *error);// 2-space indentation (default)
jsonnet_fmt_indent(vm, 2);
// 4-space indentation
jsonnet_fmt_indent(vm, 4);
// Tab indentation not supported - use spaces onlyExample output with different indentation:
// 2-space indentation
{
name: "example",
config: {
port: 8080,
ssl: true
}
}
// 4-space indentation
{
name: "example",
config: {
port: 8080,
ssl: true
}
}// Double quotes (default)
jsonnet_fmt_string(vm, 'd');
// Single quotes
jsonnet_fmt_string(vm, 's');
// Leave as-is
jsonnet_fmt_string(vm, 'l');Example transformations:
// Original mixed style
{ name: 'Alice', message: "Hello World!" }
// After double quote formatting
{ name: "Alice", message: "Hello World!" }
// After single quote formatting
{ name: 'Alice', message: 'Hello World!' }// Hash comments (default)
jsonnet_fmt_comment(vm, 'h');
// Slash comments
jsonnet_fmt_comment(vm, 's');
// Leave as-is
jsonnet_fmt_comment(vm, 'l');Example transformations:
// Original mixed comments
{
// This is a slash comment
name: "app",
# This is a hash comment
version: "1.0"
}
// After hash comment formatting
{
# This is a slash comment
name: "app",
# This is a hash comment
version: "1.0"
}// Enable array padding
jsonnet_fmt_pad_arrays(vm, 1);
// Enable object padding
jsonnet_fmt_pad_objects(vm, 1);Example output with padding:
// Without padding (default)
{
items: [1, 2, 3],
config: {name: "app", port: 8080}
}
// With padding enabled
{
items: [ 1, 2, 3 ],
config: { name: "app", port: 8080 }
}// Enable pretty field names
jsonnet_fmt_pretty_field_names(vm, 1);Example transformations:
// Original quoted field names
{
"simple_name": "value1",
"name-with-dashes": "value2",
"123numeric": "value3"
}
// After pretty field name formatting
{
simple_name: "value1", // Quotes removed
"name-with-dashes": "value2", // Quotes kept (contains dashes)
"123numeric": "value3" // Quotes kept (starts with number)
}// Enable import sorting
jsonnet_fmt_sort_imports(vm, 1);Example transformation:
// Original unsorted imports
local utils = import "utils.jsonnet";
local config = import "config.jsonnet";
local base = import "base.jsonnet";
// After sorting
local base = import "base.jsonnet";
local config = import "config.jsonnet";
local utils = import "utils.jsonnet";#include "libjsonnet.h"
#include "libjsonnet_fmt.h"
int main() {
struct JsonnetVm *vm = jsonnet_make();
// Configure formatting options
jsonnet_fmt_indent(vm, 4);
jsonnet_fmt_string(vm, 's'); // Single quotes
jsonnet_fmt_comment(vm, 'h'); // Hash comments
jsonnet_fmt_pad_arrays(vm, 1); // Pad arrays
jsonnet_fmt_pad_objects(vm, 1); // Pad objects
jsonnet_fmt_pretty_field_names(vm, 1); // Pretty field names
jsonnet_fmt_sort_imports(vm, 1); // Sort imports
int error;
char *formatted = jsonnet_fmt_file(vm, "config.jsonnet", &error);
if (error) {
fprintf(stderr, "Formatting error: %s\n", formatted);
} else {
printf("%s", formatted);
}
jsonnet_realloc(vm, formatted, 0); // Free result
jsonnet_destroy(vm);
return error;
}struct JsonnetVm *vm = jsonnet_make();
jsonnet_fmt_indent(vm, 2);
const char* code = "{ name:'Alice',age:30,config:{port:8080,ssl:true} }";
int error;
char *formatted = jsonnet_fmt_snippet(vm, "snippet.jsonnet", code, &error);
if (!error) {
printf("Formatted code:\n%s\n", formatted);
// Output:
// {
// name: 'Alice',
// age: 30,
// config: {
// port: 8080,
// ssl: true
// }
// }
}
jsonnet_realloc(vm, formatted, 0);
jsonnet_destroy(vm);void configure_formatter(struct JsonnetVm *vm) {
// Indentation: 4 spaces
jsonnet_fmt_indent(vm, 4);
// Maximum 1 consecutive blank line
jsonnet_fmt_max_blank_lines(vm, 1);
// String style: double quotes
jsonnet_fmt_string(vm, 'd');
// Comment style: slash comments
jsonnet_fmt_comment(vm, 's');
// Enable padding for arrays and objects
jsonnet_fmt_pad_arrays(vm, 1);
jsonnet_fmt_pad_objects(vm, 1);
// Enable pretty field names and import sorting
jsonnet_fmt_pretty_field_names(vm, 1);
jsonnet_fmt_sort_imports(vm, 1);
}
int format_file(const char *filename) {
struct JsonnetVm *vm = jsonnet_make();
configure_formatter(vm);
int error;
char *result = jsonnet_fmt_file(vm, filename, &error);
if (!error) {
// Write to output file or stdout
FILE *out = fopen("formatted_output.jsonnet", "w");
fprintf(out, "%s", result);
fclose(out);
}
jsonnet_realloc(vm, result, 0);
jsonnet_destroy(vm);
return error;
}#include <dirent.h>
#include <string.h>
int format_directory(const char *dir_path) {
struct JsonnetVm *vm = jsonnet_make();
// Configure consistent style
jsonnet_fmt_indent(vm, 2);
jsonnet_fmt_string(vm, 'd');
jsonnet_fmt_sort_imports(vm, 1);
jsonnet_fmt_pretty_field_names(vm, 1);
DIR *dir = opendir(dir_path);
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (strstr(entry->d_name, ".jsonnet") != NULL) {
char filepath[256];
snprintf(filepath, sizeof(filepath), "%s/%s", dir_path, entry->d_name);
int error;
char *formatted = jsonnet_fmt_file(vm, filepath, &error);
if (!error) {
// Write back to original file
FILE *file = fopen(filepath, "w");
fprintf(file, "%s", formatted);
fclose(file);
printf("Formatted: %s\n", filepath);
} else {
fprintf(stderr, "Error formatting %s: %s\n", filepath, formatted);
}
jsonnet_realloc(vm, formatted, 0);
}
}
closedir(dir);
jsonnet_destroy(vm);
return 0;
}// Enable debug desugaring to see expanded syntax
struct JsonnetVm *vm = jsonnet_make();
jsonnet_fmt_debug_desugaring(vm, 1);
const char* code = "{ [x]: x for x in ['a', 'b', 'c'] }";
int error;
char *result = jsonnet_fmt_snippet(vm, "debug.jsonnet", code, &error);
if (!error) {
printf("Desugared output:\n%s\n", result);
// Shows the expanded form of comprehensions and other sugar
}Makefile example:
format:
@for file in $$(find . -name "*.jsonnet"); do \
echo "Formatting $$file"; \
jsonnetfmt -i $$file; \
done
check-format:
@for file in $$(find . -name "*.jsonnet"); do \
if ! jsonnetfmt --test $$file; then \
echo "$$file needs formatting"; \
exit 1; \
fi; \
donePre-commit hook:
#!/bin/bash
# .git/hooks/pre-commit
echo "Checking Jsonnet formatting..."
files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.jsonnet$')
if [ -n "$files" ]; then
for file in $files; do
if ! jsonnetfmt --test "$file"; then
echo "Error: $file is not properly formatted"
echo "Run: jsonnetfmt -i $file"
exit 1
fi
done
fi
echo "Jsonnet formatting check passed"Install with Tessl CLI
npx tessl i tessl/pypi-jsonnet