-
Is it possible to convert pkl to json in java code not using CLI? I want to convert the pkl to json in a java code as I did in cli and get json as a variable. CLI Java |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
There's two ways to do this. The first (and the recommended) approach is to use the 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. try (var evaluator = EvaluatorBuilder.preconfigured().build()) {
var module = evaluator.evaluate(ModuleSource.path("/foo/bar.pkl"));
var writer = new StringWriter();
var renderer = ValueRenderers.json(writer, " ", true);
renderer.renderDocument(module);
System.out.println(writer.toString());
} |
Beta Was this translation helpful? Give feedback.
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.
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.