partiql / partiql-ir-generator

PartiQL I.R. Generator (P.I.G.)
Apache License 2.0
24 stars 7 forks source link

Custom JSON serialization with Ion-Jackson possible, for interop with languages that don't have Ion SDK's? #119

Open GavinRay97 opened 2 years ago

GavinRay97 commented 2 years ago

Taking the example of the Calculator AST from the docs, serializing a simple plus(3, 5) expression as JSON:

fun example() {
    val ast = EnhancedCalculatorAst.build {
        binary(plus(), lit(3), lit(5))
    }
    val sexpr = ast.toIonElement()

    val astString = StringBuilder()
    val ionWriter = IonTextWriterBuilder.json().build(astString)

    sexpr.writeTo(ionWriter)
    println("Serialized AST $astString")
}

You get:

Serialized AST ["binary",["plus"],["lit",3],["lit",5]]

For languages that don't have an Ion SDK, the object ["binary",["plus"],["lit",3],["lit",5]] isn't very easily constructed or comprehended.

Is there any way to define or write custom (de)serializers to allow serializing it as something like:

{
  type: "binary",
  operator: "plus",
  left: {
    type: "literal",
    value: 3
  },
  right: {
   type: "literal",
   value: 5
  }
}

Thank you =)

dlurton commented 2 years ago

HI! Thanks for the question and sorry for the delay in a response.

There isn't something built-in currently. Options are:

dlurton commented 2 years ago

I should also add, to get the data classes to work with Jackson, it would be necessary to add a command-line option --enable-jackson (that applies only to the Kotlin target) and then modify the Kotlin template here to emit the appropriate Jackson annotations. I'm not sure if the current maintainers of this package would want this feature, though.

GavinRay97 commented 2 years ago

Ah awesome, looks like it's fairly straightforward to do custom codegen via templates!

Thanks, when I get some free time I'll play around with this and see if I can get it to spit out a wire-friendly JSON representation for sending to other services/languages 👌

I've been meaning to do a blogpost on building a query expression language + IR-translator with PartiQL anyways