Skip to main content
Version: Next

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

repositories {
mavenLocal()
mavenCentral()
flatDir {
dirs("deps")
}
}

dependencies {
implementation(files("deps/sas-parser-with-dependencies-1.6.5-all.jar"))
}

Export JSON

Kolasu provides JsonGenerator to write the full AST tree, including node types and properties discovered by introspection.

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}")
}

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:

import com.strumenta.kolasu.serialization.XMLGenerator

fun exportXml(root: com.strumenta.kolasu.model.Node, outputXml: File) {
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:

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>")
}
}
}

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.