Serialize the AST
The CLI can export JSON via the jsonast and model commands (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/sas-parser-with-dependencies-1.6.5-all.jar"))
}
repositories {
mavenLocal()
mavenCentral()
flatDir {
dirs("deps")
}
}
dependencies {
implementation(files("deps/sas-parser-with-dependencies-1.6.5-all.jar"))
implementation "com.strumenta.kolasu:kolasu-javalib:1.5.96"
}
Export JSON
Kolasu provides JsonGenerator to write the full AST tree, including node types and properties discovered by introspection.
- Kotlin
- Java
import com.strumenta.kolasu.serialization.JsonGenerator
import com.strumenta.kolasu.commercial.LicenseManager
import com.strumenta.sas.parser.SASLanguage
import java.io.File
fun exportJson(sasFile: File, license: File, outputJson: File) {
LicenseManager.registerLicense(license)
val sas = SASLanguage()
sas.parseNativeSQL = true
val result = sas.parse(sasFile)
val root = result.root ?: error("No AST for ${sasFile.name}")
val json = JsonGenerator().generateString(result, null)
outputJson.writeText(json)
println("Wrote ${outputJson.absolutePath}")
}
import com.strumenta.kolasu.parsing.ParsingResult;
import com.strumenta.kolasu.serialization.JsonGenerator;
import com.strumenta.sas.ast.SourceFile;
import com.strumenta.kolasu.commercial.LicenseManager;
import com.strumenta.sas.parser.SASLanguage;
import java.io.File;
import java.nio.file.Files;
public class ExportJson {
public static void exportJson(File sasFile, File license, File outputJson) throws Exception {
LicenseManager.INSTANCE.registerLicense(license);
SASLanguage sas = new SASLanguage();
sas.setParseNativeSQL(true);
ParsingResult<SourceFile> result = sas.parse(sasFile);
if (result.getRoot() == null) {
throw new IllegalStateException("No AST for " + sasFile.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 parse CLI subcommand prints to stdout. 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:
- Kotlin
- Java
import com.strumenta.kolasu.serialization.XMLGenerator
fun exportXml(root: com.strumenta.kolasu.model.Node, outputXml: File) {
XMLGenerator().generateFile(root, outputXml)
}
import com.strumenta.kolasu.model.Node;
import com.strumenta.kolasu.serialization.XMLGenerator;
import java.io.File;
public class ExportXml {
public static void exportXml(Node root, File outputXml) {
XMLGenerator xmlGenerator = new XMLGenerator();
xmlGenerator.generateFile(root, outputXml);
}
}
Debug a single node
When exploring the AST interactively, print simpleNodeType / getSimpleNodeType() and inspect node properties without serializing the whole subtree. In Kotlin use node.properties; in Java use Processing.processProperties:
- Kotlin
- Java
fun debugNode(node: com.strumenta.kolasu.model.Node) {
println(node.simpleNodeType)
node.properties.forEach { property ->
if (!property.providesNodes) {
println(" ${property.name} = ${property.value}")
} else {
println(" ${property.name} = <Nodes>")
}
}
}
import com.strumenta.kolasu.model.Node;
import com.strumenta.kolasu.model.Processing;
public class DebugNode {
public static void debugNode(Node node) {
System.out.println(node.getSimpleNodeType());
Processing.processProperties(node, property -> {
if (!property.getProvideNodes()) {
System.out.println(" " + property.getName() + " = " + property.getValue());
} else {
System.out.println(" " + property.getName() + " = <Nodes>");
}
return null;
});
}
}
When to serialize vs. walk
Serialize when you need a snapshot for storage, diffing, or non-JVM consumers. Walk or visit when you need structured extraction (lineage, inventory) without persisting the entire tree.