Serialize the AST
The CLI can export JSON or XML via the parse command (see Usage). From library code you can serialize the AST the same way — useful for golden tests, feeding external tools, or caching parse results.
Add Dependencies
- Kotlin
- Java
repositories {
mavenLocal()
mavenCentral()
flatDir {
dirs("deps")
}
}
dependencies {
implementation(files("deps/clparser-with-dependencies-1.0.1-all.jar"))
}
repositories {
mavenLocal()
mavenCentral()
flatDir {
dirs("deps")
}
}
dependencies {
implementation(files("deps/clparser-with-dependencies-1.0.1-all.jar"))
implementation "com.strumenta.kolasu:kolasu-javalib:1.5.107"
}
Export JSON
Kolasu provides JsonGenerator to write the full AST tree, including node types and properties discovered by introspection. This is the same mechanism used by the CLI.
- Kotlin
- Java
import com.strumenta.cl.parser.CLKolasuParser
import com.strumenta.kolasu.commercial.LicenseManager
import com.strumenta.kolasu.serialization.JsonGenerator
import java.io.File
fun exportJson(clFile: File, license: File, outputJson: File) {
LicenseManager.registerLicense(license)
val result = CLKolasuParser().parse(clFile)
if (result.root == null) {
error("No AST for ${clFile.name}")
}
val json = JsonGenerator().generateString(result, null)
outputJson.writeText(json)
println("Wrote ${outputJson.absolutePath}")
}
import com.strumenta.cl.model.CLCompilationUnit;
import com.strumenta.cl.parser.CLKolasuParser;
import com.strumenta.kolasu.commercial.LicenseManager;
import com.strumenta.kolasu.parsing.ParsingResult;
import com.strumenta.kolasu.serialization.JsonGenerator;
import java.io.File;
import java.nio.file.Files;
public class ExportJson {
public static void exportJson(File clFile, File license, File outputJson) throws Exception {
LicenseManager.INSTANCE.registerLicense(license);
ParsingResult<CLCompilationUnit> result = new CLKolasuParser().parse(clFile);
if (result.getRoot() == null) {
throw new IllegalStateException("No AST for " + clFile.getName());
}
String json = new JsonGenerator().generateString(result, null);
Files.writeString(outputJson.toPath(), json);
System.out.println("Wrote " + outputJson.getAbsolutePath());
}
}
The JSON format matches what the CLI parse -f json command writes. You can load it into the interactive AST viewer by placing files under static/parser-examples/.
Export XML
For tools that prefer XML, use XMLGenerator with the same API (matching the CLI -f xml option):
- Kotlin
- Java
import com.strumenta.kolasu.serialization.XMLGenerator
fun exportXml(result: com.strumenta.kolasu.parsing.ParsingResult<*>, outputXml: File) {
val xml = XMLGenerator().generateString(result.toResult())
outputXml.writeText(xml)
}
import com.strumenta.kolasu.parsing.ParsingResult;
import com.strumenta.kolasu.serialization.XMLGenerator;
import java.io.File;
import java.nio.file.Files;
public class ExportXml {
public static void exportXml(ParsingResult<?> result, File outputXml) throws Exception {
String xml = new XMLGenerator().generateString(result.toResult());
Files.writeString(outputXml.toPath(), xml);
}
}
Serialize when you need a snapshot for storage, diffing, or non-JVM consumers. Walk when you need structured extraction (inventory, call sites, file operations) without persisting the entire tree.