Skip to content

convert pkl to json in java code #504

Answered by bioball
OZOOOOOH asked this question in Q&A
Discussion options

You must be logged in to vote

There's two ways to do this.

The first (and the recommended) approach is to use the Evaluator#evaluateOutputText method. This will behave just like the CLI does when it renders, and will use the in-language renderer.

This means that it applies any conversions that are defined in-language.

try (var evaluator = EvaluatorBuilder.preconfigured().setOutputFormat(OutputFormat.JSON).build()) {
  var text = evaluator.evaluateOutputText(ModuleSource.path("/foo/bar")); 
  System.out.println(text);
}

The second approach is to use the Java-side JSON renderer. The usefulness of this approach is limited, but can be helpful if you don't need to copy the Pkl CLI, and already have the evaluated Java object.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@OZOOOOOH
Comment options

Answer selected by OZOOOOOH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants