NAME

JSON::Jsonnet - XS bindings to libjsonnet (C++ Jsonnet)

SYNOPSIS

use JSON::Jsonnet;

my $vm = JSON::Jsonnet->new(
    jpathdir => ["./libsonnet"],
    ext_vars => { ENV => "prod" },
    tla_vars => { env => "prod" },
);

my $json = $vm->evaluate_snippet("snippet", '{ x: 1 }');
print $json;

DESCRIPTION

This module provides a thin XS interface to the official Jsonnet C API (libjsonnet). It lets you evaluate Jsonnet snippets/files, use multi/stream outputs, and register import and native callbacks from Perl.

METHODS

new(%options)

Create a new Jsonnet VM.

All options are optional:

  • max_stack, gc_min_objects, gc_growth_trigger, max_trace

    VM tuning parameters.

  • string_output => 0|1

    If true, a top-level Jsonnet string is returned as raw text without JSON quotes.

  • jpathdir => $dir | \@dirs

    Import search paths (like Jsonnet -J).

  • ext_vars / ext_codes

    External variables/codes (available via std.extVar()).

  • tla_vars / tla_codes

    Top-level arguments (TLA). In Jsonnet they are passed as parameters to the top-level function.

  • import_callback => sub { ... }

    Custom importer. Must return:

    ( $found_here, $content )   # success
    ( undef, $error_text )      # failure
  • native_callbacks => { name => { cb => sub{...}, params => \@p }, ... }

    Register std.native(...) callbacks.

evaluate_snippet($filename, $snippet)

Evaluate a Jsonnet snippet and return resulting JSON text (pretty-printed by libjsonnet). On error this method throws an exception with the Jsonnet error message.

evaluate_file($filename)

Evaluate a Jsonnet file and return resulting JSON text. Dies on Jsonnet errors.

evaluate_snippet_multi($filename, $snippet)

Evaluate a snippet in "multi" mode. Returns a hashref:

{ "file1.json" => $json_text, "file2.json" => $json_text, ... }

Dies on Jsonnet errors.

evaluate_file_multi($filename)

Same as evaluate_snippet_multi, but evaluates a file.

evaluate_snippet_stream($filename, $snippet)

Evaluate a snippet in "stream" mode. Returns an arrayref of JSON texts, one per element in the top-level array. Dies on Jsonnet errors.

evaluate_file_stream($filename)

Same as evaluate_snippet_stream, but evaluates a file.

ext_var($key, $value)

Set an external variable for std.extVar($key).

ext_code($key, $code)

Set an external code snippet for std.extVar($key).

tla_var($key, $value)

Set a top-level argument (TLA) variable. TLAs are passed as parameters to the top-level function.

tla_code($key, $code)

Set a top-level argument (TLA) code snippet.

jpath_add($dir)

Add an import search directory (like Jsonnet -J).

import_callback($coderef)

Register a custom importer. The callback is called as:

my ($base, $rel) = @_;
return ($found_here, $content);   # success
return (undef, $error_text);      # failure

If the callback dies, evaluation fails with that error.

native_callback($name, $coderef, \@params)

Register a std.native($name) callback.

@params is the list of parameter names as used by Jsonnet.

max_stack($n) =head2 gc_min_objects($n) =head2 gc_growth_trigger($factor) =head2 max_trace($n) =head2 string_output($bool)

VM tuning options. See libjsonnet documentation for details.

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Sergey Kovalev <info@neolite.ru>